Deploy NodeJS App on Kubernetes Using Kustomize

In this article we are going to cover Deploy NodeJS App on Kubernetes Using Kustomize.

Kubernetes is a powerful platform for managing containerized applications, and Node.js is a popular choice for building scalable web apps. Combining these two can help you efficiently deploy and manage your applications.

Kustomize makes this process even easier by allowing you to customize Kubernetes configurations without changing the original files. In this article, we’ll show you how to deploy a Node.js app on Kubernetes using Kustomize, step by step.

Prerequisites

1. Install Kubernetes tools:

  • Kubectl
  • Kustomize

2. A Kubernetes cluster (e.g., Minikube, kind, or a cloud-managed cluster).

3. A basic Node.js app with a Dockerfile to containerize the app.

Step-1: Set Up the Node.js Application

Create a Docker Image

If you don’t already have a Docker image for your Node.js app, create one:

Create the Project Folder:

Open a terminal and create a directory for your project:

mkdir nodejs-kustomize-demo
cd nodejs-kustomize-demo

Install npm:

apt install npm

    Initialize a new Node.js project:

    npm init -y
    Deploy NodeJS App on Kubernetes Using Kustomize 1
    • This command generates a package.json file with default settings.

    Write the Node.js Application

    1. Create a file named app.js:

      vim app.js

      2. Node.js App Example:

      // app.js
      const http = require('http');
      const PORT = process.env.PORT || 3000;

      const server = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Hello, Kubernetes with Kustomize!\n');
      });

      server.listen(PORT, () => {
      console.log(`Server running on port ${PORT}`);
      });

      Explanation:

      • This app creates an HTTP server that responds with a “Hello, Kubernetes with Kustomize!” message.
      • The port is configurable through the environment variable PORT (important for containers).

      Prepare the Dockerfile

      1. Create a file named Dockerfile:

        vim Dockerfile

        2. Dockerfile:

        # Use Node.js base image
        FROM node:16

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

        # Copy package.json and package-lock.json
        COPY package*.json ./

        # Install dependencies
        RUN npm install

        # Copy the rest of the application files
        COPY . .

        # Expose the port that the app runs on
        EXPOSE 3000

        # Start the application
        CMD ["node", "app.js"]

        Explanation:

        • Base Image (node:16): Uses the official Node.js Docker image.
        • WORKDIR: Sets /usr/src/app as the working directory in the container.
        • COPY Commands: Copies the application files into the container.
        • RUN npm install: Installs the application dependencies.
        • EXPOSE 3000: Exposes port 3000, which the app listens on.
        • CMD: Specifies the command to run the app (node app.js).

        Build the Docker Image

        1. Build the Docker image:

        docker build -t <your-dockerhub-username>/nodejs-kustomize:latest .
        Deploy NodeJS App on Kubernetes Using Kustomize 2
        • Replace <your-dockerhub-username> with your Docker Hub username.
        • The . at the end refers to the current directory containing the Dockerfile.

        2. Verify that the image was built:

        docker images
        Deploy NodeJS App on Kubernetes Using Kustomize 3

        You should see your image listed.

        Push the Docker Image to Docker Hub

        1. Log in to Docker Hub:

        docker login
        Deploy NodeJS App on Kubernetes Using Kustomize 4

        Use the one-time device confirmation code in the website:

        Deploy NodeJS App on Kubernetes Using Kustomize 5

        2. Push the image to Docker Hub:

        docker push <your-dockerhub-username>/nodejs-kustomize:latest
        Deploy NodeJS App on Kubernetes Using Kustomize 6

        This makes the image available for Kubernetes to pull from Docker Hub.

        Verify the image is available on Docker Hub by visiting the Docker Hub website and checking your repository.

        Deploy NodeJS App on Kubernetes Using Kustomize 7

        Step2:Create the Kubernetes Base Configuration

        The base configuration will define the common Kubernetes resources.

        Directory Structure:

          kustomize-nodejs/
          ├── base/
          │   ├── deployment.yaml
          │   ├── service.yaml
          │   └── kustomization.yaml
          ├── overlays/
              ├── development/
              │   ├── kustomization.yaml
              │   ├── deployment-patch.yaml
              ├── production/
                  ├── kustomization.yaml
                  ├── deployment-patch.yaml

          Base Configuration

          Base Deployment (base/deployment.yaml):

          apiVersion: apps/v1
          kind: Deployment
          metadata:
          name: nodejs-app
          labels:
          app: nodejs-app
          spec:
          replicas: 1
          selector:
          matchLabels:
          app: nodejs-app
          template:
          metadata:
          labels:
          app: nodejs-app
          spec:
          containers:
          - name: nodejs-app
          image: <your-dockerhub-username>/nodejs-kustomize:latest
          ports:
          - containerPort: 3000

          Base Service (base/service.yaml):

          apiVersion: v1
          kind: Service
          metadata:
          name: nodejs-app
          spec:
          selector:
          app: nodejs-app
          ports:
          - protocol: TCP
          port: 80
          targetPort: 3000
          type: LoadBalancer

          Base Kustomization File (base/kustomization.yaml):

          resources:
          - deployment.yaml
          - service.yaml

          Create Overlays

          Overlays allow you to customize configurations for specific environments.

          Development Overlay

          Patch for Development (overlays/development/deployment-patch.yaml):

          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: nodejs-app
          spec:
            replicas: 1
            template:
              spec:
                containers:
                - name: nodejs-app
                  env:
                  - name: NODE_ENV
                    value: development

          Development Kustomization File (overlays/development/kustomization.yaml):

          resources:
          - ../../base
          
          patchesStrategicMerge:
          - deployment-patch.yaml
          Production Overlay

          Patch for Production (overlays/production/deployment-patch.yaml):

          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: nodejs-app
          spec:
            replicas: 3
            template:
              spec:
                containers:
                - name: nodejs-app
                  env:
                  - name: NODE_ENV
                    value: production

          Production Kustomization File (overlays/production/kustomization.yaml):

          resources:
          - ../../base

          patchesStrategicMerge:
          - deployment-patch.yaml

          Step3:Deploy NodeJS APP on kubernetes using Kustomize

          1. Deploy to Development Environment:

          kubectl apply -k overlays/development
          Deploy NodeJS App on Kubernetes Using Kustomize 8

          2. Deploy to Production Environment:

          kubectl apply -k overlays/production
          Deploy NodeJS App on Kubernetes Using Kustomize 9

          Step4:Access the Application

          To access your application, use the following kubectl port-forward command:

          kubectl port-forward --address 0.0.0.0 svc/nodejs-app 8888:80

          This command forwards traffic from your local machine’s port 8888 to the Kubernetes service’s port 3000. You can now access your application in the browser at:

          http://<your-ip>:8888

          Replace <your-ip> with the IP address of your server.

          Deploy NodeJS App on Kubernetes Using Kustomize 10

          Step5:Clean Up

          To delete the resources:

          kubectl delete deployment nodejs-app
          kubectl delete svc nodejs-app

          Conclusion:

          In this guide, we deployed a Node.js application on Kubernetes using Kustomize. We covered setting up Docker images, writing Kubernetes manifests, and managing them with Kustomize.

          This process simplifies app deployment and configuration management. You’re now ready to deploy and manage applications on Kubernetes.

          Harish Reddy

          Leave a Comment

          This site uses Akismet to reduce spam. Learn how your comment data is processed.

          Share via
          Copy link
          Powered by Social Snap