Open In App

How to Deploy Node.js Application in Kubernetes?

Deploying the Node.js application on Kubernetes in a containerized manner makes it highly scalable, and fault-tolerant, and allows zero downtime. Kubernetes allows container orchestration that can be used in many ways during deployment. Load balancing is provided, which helps with automatic load transfer to multiple pods. So getting all these benefits from Kubernetes lets us understand how we can deploy Node.js applications to Kubernetes.

Primary Terminologies

Deploying The Node.js Application in Kubernetes

Step 1: Create and push the Docker image of the Node.js application

FROM node:20-alpine
RUN mkdir -p /home/app
WORKDIR /home/app
COPY /package.json /package-lock.json .
RUN npm install
COPY / .
EXPOSE 3000
CMD ["npm","start"]
docker build -t node-hello-world .



docker tag [OLD IMAGE NAME] [YOUR_USERNAME/REPOSITORY_NAME:TAG]



docker push IMAGE_NAME:TAG

Step 2: Create YAML Configuration File for Kubernetes Deployment

apiVersion: v1
kind: Service
metadata:
name: nodehelloworld
spec:
replicas: 1
selector:
matchLabels:
app: nodehelloworld
template:
metadata:
labels:
app: nodehelloworld
spec:
containers:
- name: nodehelloworld
image: deepcodr/node-hello-world:latest
ports:
- containerPort: 3000
imagePullPolicy: Always
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodehelloworld
spec:
replicas: 1
selector:
matchLabels:
app: nodehelloworld
template:
metadata:
labels:
app: nodehelloworld
spec:
containers:
- name: nodehelloworld
image: deepcodr/node-hello-world:latest
ports:
- containerPort: 3000
imagePullPolicy: Always

Step 3: Add Service Configuration to the File

apiVersion: v1
kind: Service
metadata:
name: nodehelloworld
spec:
selector:
app: nodehelloworld
type: LoadBalancer
ports:
- port: 3000
targetPort: 3000
---

Step 4: Apply the Deployment

kubectl apply -f filename

kubectl get services

Conclusion

Thus we have successfully deployed NodeJs application on Kubernetes. This has created pod and service for the application which can run with all features of kubernetes. We can use other kubernetes options to manage pods and update the application as required.

Deploy Node.js Application in Kubernetes – FAQs

What Kubernetes Resources Are Required To Deploy a Node.js Application?

The main Kubernetes resources needed to deploy a Node.js application include Deployment, Service, and optionally Ingress or LoadBalancer depending on your networking requirements.

How Do I Containerize My Node.js Application For Kubernetes?

Containerizing your Node.js application involves creating a Docker image that contains your application code, dependencies, and runtime environment. You then push this image to a container registry like Docker Hub or a private registry.

Can Kubernetes Auto-Scale My Node.js Application Based On Traffic?

Yes, Kubernetes supports horizontal autoscaling, which automatically adjusts the number of replicas (Pods) in a Deployment based on CPU utilization or custom metrics, allowing your Node.js application to scale up or down as needed.

How do I Expose My Node.js Application to External Traffic in Kubernetes?

You can expose your Node.js application to external traffic in Kubernetes using a Service of type LoadBalancer or Ingress. LoadBalancer provisions an external load balancer, while Ingress provides a more flexible way to manage external access and routing.

What Tools Can I Use to Manage Kubernetes Clusters and Deployments?

Tools like kubectl (Kubernetes command-line tool), Helm (package manager for Kubernetes), and Kubernetes dashboard (web-based UI) are commonly used to manage Kubernetes clusters and deployments.


Article Tags :