Sunday, 12 October 2025

Deploy a Node.js App on Kubernetes with NodePort Service

Our course you can check :-   Udemy course  


Ques:- 

Development team has completed development of one of the node applications, which they are planning to deploy on a Kubernetes cluster. They recently had a meeting with the DevOps team to share their requirements. Based on that, the DevOps team has listed out the exact requirements to deploy the app. Find below more details:

Create a deployment using gcr.io/kodekloud/centos-ssh-enabled:node image, replica count must be 2.

Create a service to expose this app, the service type must be NodePort, targetPort must be 8080 and nodePort should be 30012.

Make sure all the pods are in Running state after the deployment.

You can use any labels as per your choice.


Ans:-

Here’s the complete Kubernetes YAML configuration to deploy the Node.js application using the specified image and expose it via a NodePort service:

raj@jumphost ~$ cat pod.yaml 

---

apiVersion: apps/v1

kind: Deployment

metadata:

  name: node-app-deployment

  labels:

    app: node-app

spec:

  replicas: 2

  selector:

    matchLabels:

      app: node-app

  template:

    metadata:

      labels:

        app: node-app

    spec:

      containers:

        - name: node-container

          image: gcr.io/kodekloud/centos-ssh-enabled:node

          ports:

            - containerPort: 8080


---

apiVersion: v1

kind: Service

metadata:

  name: node-app-service

spec:

  type: NodePort

  selector:

    app: node-app

  ports:

    - port: 8080

      targetPort: 8080

      nodePort: 30012


raj@jumphost ~$ kubectl apply -f pod.yaml 

deployment.apps/node-app-deployment created

service/node-app-service created


raj@jumphost ~$ kubectl get all

NAME                                       READY   STATUS    RESTARTS   AGE

pod/node-app-deployment-57cd6cbd85-89svv   1/1     Running   0          52s

pod/node-app-deployment-57cd6cbd85-szln6   1/1     Running   0          52s


NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE

service/kubernetes         ClusterIP   10.96.0.1       <none>        443/TCP          20m

service/node-app-service   NodePort    10.96.150.200   <none>        8080:30012/TCP   52s


NAME                                  READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/node-app-deployment   2/2     2            2           52s


NAME                                             DESIRED   CURRENT   READY   AGE

replicaset.apps/node-app-deployment-57cd6cbd85   2         2         2       52s

raj@jumphost ~$ 


Ensure both pods are in Running state and the service is exposing port 30012.

Access the app in your browser at:

http://<NodeIP>:30012

Replace <NodeIP> with your Kubernetes node’s external IP.


Conclusion:-

In this hands-on tutorial, you’ll learn how to deploy a Node.js application on a Kubernetes cluster using a real-world DevOps workflow. We’ll walk through creating a deployment with multiple replicas and exposing the app using a NodePort service.

What You’ll Learn:

  • Deploying a Node.js app using a custom Docker image
  • Creating a Kubernetes Deployment with replica sets
  • Exposing the app using a NodePort service
  • Verifying pod and service status
  • Accessing the app from your browser

Perfect for DevOps engineers, developers, and SREs looking to gain practical Kubernetes experience.

No comments:

Post a Comment