Build and Deploy NodeJS App On Kubernetes from Scratch
Tags: kubernetes, node, javascript, webdev
Published: 2023-02-09 15:11:49

🚀 Whenever we build our applications, Initially we do not care about writing scalable or Non-SPOF (Single point of failure) Applications. But It is always a good practice to think about the Design, Scalability and Availability of your WebApp.

To Handle Scalability we have many techniques like Microservices, Deploying multiple instances using Docker and using Load Balancers.

![Docker Containers meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7cmvq4xmeii62as4fu5.png)

😅 Yes, You read it right. Containerizing your app alone is not sufficient, It needs more than that. Container Management is one of the main task. This is where Kubernetes comes, Kubernetes does automate the container management, deployment and scaling our apps in a broader way.

## 📍 Containers

Containers is a kind of way to package all of your app with dependencies so that It can run the app independent of the platform. 🥲 If it is not clear, Don't worry you will catch up soon when you will go ahead in this Blog. 🙂

Node:- Follow this [Repo](https://github.com/AlphaDecodeX/nodejs_kubernetes_blog) for code.

- Our Dockerfile looks like this:-
```DOCKER
#Getting node image
FROM node:13-alpine 
#setting up your working directory
WORKDIR /app
#copying package json files
COPY package.json package-lock.json ./
#running npm inside the docker container and installing dependencies
RUN npm install --production 
#copying everything into the docker environment
COPY . .
#exposing port
EXPOSE 3000
#running node app on port 3000
CMD node index.js
```

🫣 Just one thing here to point out is that Docker build its file layer by layer. Each line in the Dockerfile is represented as a layer over the other. So that next time when you'll build the Dockerfile, then it would take only the changes.

![Dockerfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4u420h5jp4sllqfd8f2.png)

## 📝 Commands

- Run **_docker build -t (your_dockerhub_name)/node-app ._** command to build the image.

- To Run the container:- Run command **_docker run --rm -d -p 3000:3000 (your_dockerhub_name)/node-app_** to run docker container in the background. Now, container must be running and check localhost:3000 to view your app.

- 🚀 (your_dockerhub_name) refers to the name that you used or is showing on your Docker Desktop Dashboard after logging in.

- 📍 Note:- If you want to run elsewhere or share with others, You need to register it first. To Push your image to Docker IO run the command **_docker push (your_docker_name)/node-app_**

## 🧑‍💻 Kubernetes

It is a Container orchestration tool which does manage, deploy and scale the containers. To create Kubernetes clusters you can use Digital Ocean, Amazon Web Services etc. 

✈️ We will use KubeCTL to interact with Kubernetes. You can read more about Kubernetes BTS working [here](https://dev.to/lovepreetsingh/kubernetes-whats-whys-and-hows-of-kubernetes-with-demonstration-28dn).

😎 Now, to demonstrate the Kubernetes deployment on localhost, we will use minikube which mimics the behaviour of Kubernetes mentioned in the [Kubernetes BTS Working](https://dev.to/lovepreetsingh/kubernetes-whats-whys-and-hows-of-kubernetes-with-demonstration-28dn).

## 😊 Follow the steps

- Run Commands
```BASH
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-arm64
sudo install minikube-darwin-arm64 /usr/local/bin/minikube
```
- Before Starting MiniKube, Open your Docker Desktop to run the Docker in the background. If minikube fails to start then Kindly install VM from [here](https://minikube.sigs.k8s.io/docs/drivers/).

- After all that, Run the command:-
```BASH 
minikube start 
```

- Now, Run the command
```BASH
kubectl create deployment node-app --image=(your_dockerhub_name)/node-app
```
Note:- Run **_kubectl describe deployment node-app_** to see the status of your image running

- Type the following command to see the pod status

![Kubernetes Pod Status Command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3up1kb97dj8x9job72k.png)

- Now, Run the following commands to forward your port

![Kubectl port forwarding](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lap2tt0lyj5j8b9lg5pn.png)

It will forward the request at 9000 port to the pod running at 3000.

- Now, If you curl or request on 9000 port You will get the response.

![Kubectl minicube](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/62l4c469rxy8ocnipvbf.png)

---

🥳 You did it. That was it for today. We wrote this blog taking beginners into the count. Now further we can explore How to run Kubernetes Pods on AWS, Digital ocean or any other cloud Provider.

🚀 Remember, Cloud is nothing but someone else's machine running at some location of the world. So, deployment of docker image in the pod will follow the same process technically on any cloud provider. 

Try by yourself, and let us know in the comments.