Serverless and containers are different cloud application development approaches that can enable faster and more reliable deployment of web applications.
In this article, we shall learn about container deployment, serverless deployment, and the steps & benefits of migrating containers to the Nimbella serverless platform.
Prerequisites:
- Knowledge of HTML, CSS & JavaScript
- Basic Knowledge of Serverless architecture
- Node & Docker installed on the local development machine
What are containers?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Thus, containers make sure that software runs correctly when it is moved from one computing environment to another. The image below depicts how apps get deployed in a containerized environment. Docker Container Documentation explains it in detail.
What is serverless?
Serverless computing is a cloud computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. Pricing is based on the actual amount of resources consumed by an application, rather than on pre-purchased units of capacity.
Why containers and serverless are related but different?
The below image illustrates how serverless and containers are related but different.
When is serverless better than containers?
Serverless offers the following over containers and is well suited in such cases.
- Fast-growing apps requiring quick deployments and changes
- Serverless is a pay-per-request model and can be inherited when traffic is unpredictable, results in cost-effectiveness
- There’s no worry about scalability since a serverless provider manages it
- Deployment simplicity is what makes Serverless incredible. There’s no administration of infrastructure needed
When containers are better than serverless?
Serverless has restrictions in terms of memory and size. Hence containers are preferred for long-running processes. Serverless provides a decreased start-up time and latency when compared to containers.
Let’s learn container deployment and then the steps to deploy the same app on Nimbella serverless platform by deploying a developer portfolio website on both with the below steps.
The below diagram depicts the architecture of our portfolio app.
Step 1: Creating a simple App (Developer Portfolio site with React.js & Node.js)
GitHub Source:
https://github.com/boltathi24/Dockerized_Developer_Portfolio_React_Node/
Please consider that we are going to create a developer portfolio website using React.js as front-end, Node.js as the back-end, and MongoDB as Data Storage.
React.js as Front-End:
Let's create our react app to bring the below to fruition.
- A clean UI to depict the skills and uniqueness of a Developer.
- A contact me form to collect visitor queries. We can create React boilerplate code by running below in the terminal.
npx create-react-app {appName}
Once created, let’s modify and design our front-end in the usual React.js way using React Components as below.
Let us create an API that stores the queries from visitors using Node.js as below.
Node.js Back-end:
Please consider that we are going to store the queries into a free cloud MongoDB instance.
Let’s initiate the project by running the below command in the terminal.
npm init -y
We need to install express (for creating server-side web applications), mongoose (to interact with MongoDB) by running the below command in the terminal.
npm install express mongoose
Let's code the logic to store the queries into DB as below.
https://gist.github.com/boltathi24/0c750f8e382051e6dcec184ea930e92b
Step 2: Running the app in Docker Container
We can run our app on docker with the below steps.
- Create a Dockerfile (contains command line to assemble an image) Dockerfile for Express API: https://gist.github.com/boltathi24/f0e30e2bbe54fdf632444408e7c2fb96 Dockerfile for React App: https://gist.github.com/boltathi24/54baf4ab6d338869176bddc2007b5a35
- Define services in a docker-compose.yml file (include above docker file inside this yml configuration) docker-compose.yml for Express API: https://gist.github.com/boltathi24/130aa62c27e70f795ed0f7ab2fe44678 docker-compose.yml for React App: https://gist.github.com/boltathi24/378412fd1a8ce9a40702295fd9a1df0f
- Build and run your app with the Docker Compose tool We can run our React & Express App in a container by running the below command from the respective docker-compose.yml file directory.
docker-compose up -d
We can see our react app live on the port specified in Dockerfile.
We can ensure the same by running the below docker command.
docker ps
Now, We have created the developer portfolio app and deployed it inside a container. Let's learn how we can do the same with Nimbella Serverless.
Running App on Serverless:
Step 3: Creating a new account on Nimbella
Let’s do the below steps to get started with the Nimbella platform for deployment in 2 mins.
- Create an account in Nimbella
- Install nim CLI
- Login to nim CLI with the below command
nim login
We are a step away from deploying our app in Nimbella. We need to modify the directory structure.
Step 4: Updating the app for Nimbella
As per Nimbella’s Deployment Standard, the back-end/actions code for API has to be placed under a directory named “packages” and front-end/static code into a directory named "web".
Please follow the below steps to migrate our *React App to Nimbella *
- Wrap the React/front-end code inside the directory named web
- Create build.sh and specify below commands inside to build the React App https://gist.github.com/boltathi24/023ccfe738961411e7740fc47d3e7c3d
- Create .include file and specify “build” inside as the Build directory of our React App is identified with this file https://gist.github.com/boltathi24/a0c35ee33f6f83d6a17335a8efcee6bf
Please follow the below steps to Migrate our Node.js based App to Nimbella
In brief, we would have coded our business logic inside the routes specified in Express.
To migrate to Nimbella, we just need to wrap the same business logic inside the main function as below.
https://gist.github.com/boltathi24/9ced4d6bd82b4498dbfa4ac89ad50029
Please note that in express, we have specified the desired URL path inside the Routes But in Nimbella, the directory name represents endpoints, once deployed. Hence the directory name needs to be created with the name of the endpoints we would like to have in our API.
To migrate the express app to Nimbella, create a directory named packages.
Inside packages, create a directory that represents the app name as “form”.
Inside the app directory, create another directory as “insert” that represents serverless action. Now copy the serverless function which we have written inside the serverless action directory.
The below diagram depicts the directory structure on the Nimbella platform.
Our app’s final directory after modifying the directory as specified looks as below.
Step 5: Running/Testing the app
Create a project.yml file and configure it for deployment as below.
Let’s make our app live by running the below command.
nim project deploy {application’s directory} --env={path of environment file}
Our App and API are deployed successfully into the Nimbella Serverless Platform as below.
As per the above execution log, we can access our app with the below URL→ https://dlrowtlo-jyam4452nxf-apigcp.Nimbella.io/
You can get your serverless URL of Serverless action/API by running the below.
nim action get {actionName} --url
We have successfully migrated a developer portfolio app from containers to Nimbella Serverless Platform and the app can be accessed at the link below.
https://dlrowtlo-jyam4452nxf-apigcp.Nimbella.io/
Github Source: https://github.com/boltathi24/Nimbella_Developer_Portfolio_React_Node
TL;DR
Serverless and containers came into existence as an evolution towards maximizing utilization of computational power. We can migrate from containers to serverless with the below steps.
- SignUp/Login to Nimbella account by installing nim CLI.
- Wrap the business logic used inside existing API methods as functions.
- Modify the directory of existing code as per serverless standards.
- Configure a project.yml file for serverless deployment.
- Deploy into Nimbella using deploy command. ## Benefits for Migrating Containers to Nimbella Serverless
- Zero administration yet provides high security to date.
- The aforementioned scalability problem of containers is avoided.
- By reducing the overhead complexity of infra, developers can focus more on business logic resulting in faster time to market with reliability.
- Delivering code changes more frequently and reliably using CI/CD Pipeline.
- No need to worry about scalability during unpredicted traffic.
- Developers do not need to worry about purchasing, provisioning, and managing backend servers.
- Pay per request model facilitates cost-effectiveness since you pay only when service is consumed by the user.
- Dedicated secure domain.
- Integrated key-value store.
- Integrated object-store.
- Integrated logging & monitoring.
- Global CDN.
- MultiCloud deployment and much more.
Nimbella is named a strong performer in the serverless world by Forrestor. Please feel free to check out how Nimbella spreads the power of serverless on CRN & Rishdot Research.
Author details: Athithan Raj P
Athithan started his career with Cognizant and experienced himself in the testing field primarily. Later he started getting involved in different fields like Development, Security, Content writing, etc. He now works with a reputed organization as a Software Development Engineer in Test where he is responsible for the product quality through Automation.
Follow him on Twitter: @raj_athithan
This blog was initially published on Nimbella. Keep an eye out for this space and follow us on Twitter for the latest updates.
Top comments (0)