Dockerization and Kubernetes Deployment of a Node.js Application

Containerization and Deployment with Ease: This tutorial guides you step-by-step through the Dockerization and Kubernetes deployment of a Node.js application.

From the basics to implementation, this post provides clear step-by-step instructions and valuable insights. Learn how to efficiently modernize and scale your application using these powerful tools.

Step 1 – Clone the Node.js Project Code

First, clone the Node.js project code from GitHub:

git clone https://github.com/your-username/your-nodejs-app.git

Then navigate to the directory of the cloned project code:

Step 2 – Dockerize the Application

The Node.js application needs to be prepared to run in a Docker container. Create a Dockerfile in the root directory of the application and a .dockerignore file to manage files and directories you want to exclude when building the image.

Create a Dockerfile:

Open the Dockerfile in your preferred text editor and insert the following content:

# Use the official Node.js 14 image as the base image
FROM node:14

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy dependency files to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the application code to the working directory
COPY . .

# Change the default port number
ENV PORT=8080

# The container listens on the port defined above
EXPOSE $PORT

# Start the application
CMD [ “node”, “index.js” ]

Save and close the file.

Step 3 – Translate Compose Services to Kubernetes Objects with Kompose

Now that you have cloned the Node.js project code and packaged the application, you can translate your Docker Compose service definitions to Kubernetes objects. You can use KOMPOSE to facilitate this translation. Start by creating a new YAML file named docker-compose.yaml in the application’s root directory:

Open the file in your preferred text editor and insert the following content:

version: ‘3’

services:
web:
build: .
image: nodejs-mongo-web
ports:
– “8080:8080”
environment:
– MONGODB_URI=mongodb://mongodb:27017/hai
depends_on:
– mongodb

mongodb:
image: mongo
ports:
– “27017:27017”
environment:
– MONGO_INITDB_DATABASE=hai

Save and close the file. This file defines two services: one for the Node.js application and one for the MongoDB database. The web service definition specifies that the application code is built using the Dockerfile in the current directory and specifies that the application listens on port 8080 in the container and port 8080 on the host. The mongodb service definition specifies that the official MongoDB container is used and that MongoDB listens on port 27017 in the container and port 27017 on the host.

Now compile the Docker Compose service definitions into Kubernetes objects with the following kompose command:

kompose convert -f docker-compose.yaml

This command reads the service definitions in docker-compose.yaml and outputs the corresponding Kubernetes objects in YAML format. By default, it will be split into four files: one for the Node.js application, one for the MongoDB database, one for the service exposing the Node.js application, and one for the secret containing the MongoDB credentials.

For more information on kompose commands and options, consult the KOMPOSE documentation.

Once the service definitions have been translated into Kubernetes objects, they will be created in a new directory named KOMPOSE. Navigate to this directory to review the files:

You should see four YAML files: nodejs-mongo-web-deployment.yaml, mongodb-deployment.yaml, nodejs-mongo-web-service.yaml, and mongodb-secret.yaml. These files correspond to the Kubernetes objects created by KOMPOSE: deployments for the Node.js application and MongoDB database, a service exposing the Node.js application, and a secret containing the MongoDB credentials.

Open each file in your preferred text editor and review the generated Kubernetes objects:

  • nodejs-mongo-web-deployment.yaml: Contains the deployment definition for the Node.js application, including container specs, environment variables, and pod labels.
  • mongodb-deployment.yaml: Contains the deployment definition for the MongoDB database, including container specs, pod labels, and persistent volume claims.
  • nodejs-mongo-web-service.yaml: Contains the service definition for the Node.js application, including service type and target port.
  • mongodb-secret.yaml: Contains the secret object with MongoDB credentials, encrypted as base64.

You can now use these YAML files to create the Kubernetes objects in your cluster. However, you may need to make some adjustments to ensure they meet your specific requirements. For example, you should adjust the environment variables and access permissions to suit your specific use case.

Step 4 – Creating Kubernetes Secrets

Before creating your Kubernetes objects, you must create the secret object with the MongoDB credentials. First, create a Kubernetes secret object from the mongodb-secret.yaml file:

kubectl apply -f mongodb-secret.yaml

This command creates the secret object in the current Kubernetes namespace. To create the secret object in a different namespace, use the -n flag.

Verify that the secret object was created successfully:

kubectl get secret mongodb

You should see output similar to the following:

NAME TYPE

DATA AGE
mongodb Opaque 2 10s

The secret object named mongodb has been successfully created. It contains two data fields: username and password, which hold the MongoDB credentials.

Step 5 – Customizing Kubernetes Objects

Before creating the Kubernetes objects, you should make some adjustments to ensure they meet your specific requirements. This includes setting environment variables, adjusting access permissions, and configuring resource limits.

First, open the nodejs-mongo-web-deployment.yaml file in your preferred text editor and make the following adjustments:

  • Set the environment variable MONGODB_URI to reference the secret object containing MongoDB credentials.
  • Add an init container that waits for the MongoDB database to be ready before starting the Node.js application.

First, add the init container to the deployment definition of the Node.js application. Open the nodejs-mongo-web-deployment.yaml file and add the following init container spec under spec.template.spec.containers:

initContainers:
– name: init-db
image: mongo
command: [‘sh’, ‘-c’, ‘until mongo –eval “db.adminCommand(‘ping’)”; do sleep 2; done’]

Save and close the file.

Then, create the service for the MongoDB database. Open the mongodb-service.yaml file and add the following service definition under spec:

apiVersion: v1
kind: Service
metadata:
name: mongodb
labels:
io.kompose.service: mongodb
spec:
ports:
– name: “27017”
port: 27017
targetPort: 27017
selector:
io.kompose.service: mongodb

Save and close the file.

Step 6 – Creating the Kubernetes Objects

Once you’ve made the adjustments, you can create your Kubernetes objects in the cluster. Use the kubectl apply command to deploy the definitions to your cluster:

kubectl apply -f nodejs-mongo-web-deployment.yaml
kubectl apply -f mongodb-deployment.yaml
kubectl apply -f nodejs-mongo-web-service.yaml
kubectl apply -f mongodb-service.yaml

After creating the Kubernetes objects, you can check the status of the pods to ensure they started successfully:

You should see output similar to the following:

NAME READY STATUS RESTARTS AGE
mongodb-6b789f4577-gj22z 1/1 Running 0 5m
nodejs-mongo-web-695b58d4d4-d8lqz 1/1 Running 0 5m

If the STATUS for both pods is “Running,” they have started successfully. If the STATUS is different, use the kubectl describe command to get more information and troubleshoot potential issues.

Step 7 – Accessing the Application

Once the pods have started, you can access the application using the service endpoint. Use the kubectl get service command to retrieve the service endpoint:

kubectl get service nodejs-mongo-web

You should see output similar to the following:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nodejs-mongo-web ClusterIP 10.245.159.26 8080/TCP 5m

The service endpoint is the CLUSTER-IP, followed by the PORT(S) number. You can access the application by sending an HTTP request to the service endpoint.

Open a web browser or use curl to access the application:

Replace “ and “ with the corresponding values for your service endpoint. When you open the application in the browser, you should see the home page of the application displaying a list of shark information.

Conclusion

In this tutorial, you learned how to translate Docker Compose service definitions into Kubernetes objects using KOMPOSE. You cloned and packaged a Node.js application code, translated Docker Compose service definitions into Kubernetes objects, created Kubernetes secrets, customized Kubernetes objects, and deployed the Kubernetes objects in your cluster. Finally, you accessed the application to verify it started successfully.

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

No results found.