Deploying Node.js Application in Kubernetes

Deploying Node.js Application in Kubernetes

Create a NodeJS application

  • npm init

  • npm install express

  • Create index.js and paste the following code

Create Docker file

  • FROM node:17-alpine This line specifies the base image to use for the container.

  • WORKDIR /usr/src/app This line sets the working directory for the container to /usr/src/app This is where the application code and its dependencies will be copied to.

  • COPY package.json ./ This line copies the package.json file from the host machine to the /usr/src/app directory in the container. This file contains information about the application's dependencies, which will be installed in the next step.

  • RUN npm install This line runs the command npm install inside the container to install the dependencies listed in the package.json file.

  • COPY . . This line copies the rest of the application code from the host machine to the /usr/src/app directory in the container.

  • EXPOSE 3000 This line tells Docker that the container will listen on port 3000. This is the port that the application will be running on.

  • CMD ["node","index.js"] This line specifies the command to run when the container starts. In this case, it's running the command node index.js which starts the Node.js application.

Build your Docker Image

  • docker build -t nodeimage . This will build the docker image.

Push your image to Docker Hub

  • docker login Login to Dockerhub by entering your username and password.

  • Create a remote repository on Dockerhub. I named it examplerepo

  • Use the docker tag command to tag an image with a new name and push it to a remote repository. This will tag the image with a new name, and then push it to Docker Hub.

Create deployment.yaml file

A deployment is responsible for creating, updating, and scaling pods, and rolling back to a previous version in case of a failure.

  • apps/v1, which is the version of the Kubernetes API used to create the deployment.

  • kind specifies that this is a deployment resource.

  • The deployment is named "nodeapp-deployment" and is labeled with "app: nodeapp". A label is a key-value pair that can be added to Kubernetes objects (such as pods, services, and deployments) to organize and identify them.

  • one replica of the pod should be running at all times.

  • The selector is used to determine which pods should be included in the deployment. The pods are selected based on the label "app: nodeapp".

  • The template defines the pod template that the deployment will use to create new pods.

  • The spec field of the template describes the container that runs inside the pod.

  • The container is named nodeserver, it uses the zainabmirkar1/nodeimage:latest image and containerPort is exposed on 3000

Create service.yaml

Services enable communication between pods within a cluster and also provide a stable endpoint for external clients to access the pods.

  • The service type is set to LoadBalancer, which means that the service will be exposed externally using a load balancer.

  • The service listens on port 5000, which is the port exposed by the load balancer, and forwards traffic to targetPort 3000 on the pods.

  • The nodePort field is set to 31110, which means that the service will also be exposed on that port on each node in the cluster.

Deploy your application in Kubernetes

  • Ensure Minikube is running by minikube status

  • kubectl apply -f deployment.yaml Kubernetes will read the YAML file and create or update the deployment according to the configuration specified in the file.

  • kubectl apply -f service.yaml Kubernetes will read the YAML file and create or update the service according to the configuration specified in the file

  • minikube service nodeapp-service This will give the URL to access the application.

Result

Source Code

Learn Docker

Learn K8

More on DevOps