Power of Kubernetes: Part 1 of Day 02

Welcome to Part 1 of Day 02 of the #5DaysOfKubernetes challenge! In Today’s blog post, we’ll walk through deploying a Node.js application on a Kubernetes cluster. By following these steps, you’ll have your Node.js app up and running in no time. For reference, you can find the Node.js code and Dockerfile in this GitHub repository.

To follow this, you need to install minikube on your local/AWS machine. If you don’t know then you can refer to my step-by-step blog which will help you to do it.

https://bittublog.hashnode.dev/unlocking-the-power-of-kubernetes-part-4-of-day-01

Step 1: Create a Docker Image

Assuming you’re already familiar with Docker, let’s create a Docker image for your Node.js project. Open your terminal and use the following command to build the image:

docker build — tag avian19/node-app .

Step 2: Push the Docker Image to Docker Hub

To share your Docker image with your Kubernetes cluster, you can push it to Docker Hub. First, log in to Docker Hub using your terminal:

docker login

Then, push the Docker image:

You can confirm that your image has been successfully pushed to Docker Hub.

Step 3: Prepare Kubernetes Deployment and Service Files

Create a dedicated directory for your Node.js application’s deployment. Inside this directory, add the contents of your deployment.yml and service.yml files.

deployment.yml file

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: avian19/node-app:latest
        ports:
        - containerPort: 3000

service.yml file

apiVersion: v1
kind: Service
metadata:
  name: node-app-service
spec:
  selector:
    app: node-app
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 3000
    nodePort: 30001

Step 4: Deploy Pods

To deploy the pods, use the deployment.yml file with the following command:

kubectl apply -f deployment.yml

Step 5: Deploy Services

Next, deploy the services using the service.yml file:

kubectl apply -f service.yml

Step 6: Validate the Deployment

You can check the status of your deployment by running the following command:

kubectl get deployment

Step 7: Access Your Application

To access your deployed application, use the following command to get the URL:

minikube service node-app-service

You can now use curl to access the content of your Node.js application through the provided URL.

In nodejs code, you can see that the content is the same at both places.

Conclusion

Congratulations! You’ve successfully deployed your Node.js application on a Kubernetes cluster. This tutorial guides you through the essential steps, from creating a Docker image to deploying your application. Now you can scale and manage your Node.js app with ease on Kubernetes.

Happy coding!