Day06- Deploying Your First Node.js Application on Kubernetes Cluster

Day06- Deploying Your First Node.js Application on Kubernetes Cluster

Welcome to Day 06 of the #30DaysOfKubernetes 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://medium.com/@aman.pathak_51134/day-04-setting-up-minikube-on-your-local-machine-or-aws-instance-620a4cb57abc

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!

Want to Know About Challenge?

If you’re eager to learn more and join our challenge through the GitHub Repository, stay tuned for the upcoming posts. Follow for more exciting insights into the world of Kubernetes!

Don’t forget to tag me on LinkedIn.

GitHub Repository: https://github.com/AmanPathak-DevOps/30DaysOfKubernetes

#30DaysOfKubernetes #Kubernetes #DeploymentFile #ServiceFile #K8s

See you on Day 07 as we unravel more Kubernetes mysteries!

Stay connected on LinkedIn: LinkedIn Profile

Stay up-to-date with GitHub: GitHub Profile

Feel free to reach out to me, if you have any other queries.

Happy Learning