List of Topics:
Location Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Build a Custom Node.js Docker Image, Push it to Docker Hub, Deploy it on Kubernetes using a Deployment, Expose it Via a NodePort Service, and Verify the Application’s Response?

Kubernetes

Condition for Build a Custom Node.js Docker Image, Push it to Docker Hub, Deploy it on Kubernetes using a Deployment, Expose it Via a NodePort Service, and Verify the Application’s Response

  • Description:
    This task helps you to create and deploy a custom Node.js application using Docker and Kubernetes. We begin by building a simple Express-based web server and defining the required project files, including server.js and package.json. After preparing the application code, we write a Dockerfile to containerize the Node.js app and build the image locally. Once the image is ready, we push it to Docker Hub so that Kubernetes can pull it during deployment. Next, we create a Kubernetes Deployment specifying multiple replicas of our application to ensure scalability and reliability. After deploying the Pods, we expose the application externally through a NodePort service, which maps the internal container port to a port accessible from outside the cluster. Finally, we retrieve the Kubernetes node's IP address and test the application using a browser or curl to confirm it returns the expected Node.js response. This end-to-end workflow demonstrates how to build, containerize, deploy, and expose a production-style Node.js application on Kubernetes.

Steps

  •  STEP 1 — Create the Project
    mkdir my-node-app
    cd my-node-app
                
     Create server.js:
    nano server.js
                
    const express = require("express");
    const app = express();
    
    app.get("/", (req, res) => {
      res.send("Hello from your custom Node.js Docker image running on Kubernetes!");
    });
    
    app.listen(8080, () => console.log("Server running on port 8080"));
                
     Create package.json:
    nano package.json
                
    {
      "name": "my-node-app",
      "version": "1.0.0",
      "main": "server.js",
      "dependencies": {
        "express": "^4.18.2"
      }
    }
                
  •  STEP 2 — Create Dockerfile
    nano Dockerfile
                
    FROM node:18-alpine
    
    WORKDIR /app
    
    COPY package.json .
    RUN npm install
    
    COPY . .
    
    EXPOSE 8080
    CMD ["node", "server.js"]
                
  •  STEP 3 — Build & Push Docker Image
    docker build -t mycybersecurity/my-node-app:v1 .
    docker push mycybersecurity/my-node-app:v1
                
  •  STEP 4 — Deploy to Kubernetes
     Create deployment file deployment.yaml:
    nano deployment.yaml
                
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: custom-app-deployment
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: custom-app
      template:
        metadata:
          labels:
            app: custom-app
        spec:
          containers:
          - name: custom-app
            image: mycybersecurity/my-node-app:v1
            ports:
            - containerPort: 8080
                
     Apply:
    kubectl apply -f deployment.yaml
    kubectl get pods
                
  •  STEP 5 — Expose Using NodePort
     Create service file service.yaml:
    nano service.yaml
                
    apiVersion: v1
    kind: Service
    metadata:
      name: custom-app-service
    spec:
      type: NodePort
      selector:
        app: custom-app
      ports:
      - port: 8080
        targetPort: 8080
        nodePort: 30080   # you can choose any (30000–32767)
                
     Apply:
    kubectl apply -f service.yaml
    kubectl get svc
                
  •  STEP 6 — Test the App
     Find your node IP:
    kubectl get nodes -o wide
                
     Example:
    192.168.49.2
                
     Test:
    curl http://192.168.49.2:30080
                
     Expected output:
    Hello from your custom Node.js Docker image running on Kubernetes!
                
Screenshots
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523