DEV Community

Cover image for Build and Deploy NodeJS App On Kubernetes from Scratch
server side digest
server side digest

Posted on

Build and Deploy NodeJS App On Kubernetes from Scratch

🚀 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

😅 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 for code.

  • Our Dockerfile looks like this:-
#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
Enter fullscreen mode Exit fullscreen mode

🫣 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

📝 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.

😎 Now, to demonstrate the Kubernetes deployment on localhost, we will use minikube which mimics the behaviour of Kubernetes mentioned in the Kubernetes BTS Working.

😊 Follow the steps

  • Run Commands
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-arm64
sudo install minikube-darwin-arm64 /usr/local/bin/minikube
Enter fullscreen mode Exit fullscreen mode
  • 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.

  • After all that, Run the command:-

minikube start 
Enter fullscreen mode Exit fullscreen mode
  • Now, Run the command
kubectl create deployment node-app --image=(your_dockerhub_name)/node-app
Enter fullscreen mode Exit fullscreen mode

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

  • Now, Run the following commands to forward your port

Kubectl port forwarding

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


🥳 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.

Top comments (6)

Collapse
 
thebrown profile image
Saleumsack

Usable information.

Collapse
 
ssd profile image
server side digest

Thanks Buddy 🚀

Collapse
 
vaibhav68849256 profile image
Vaibhav Khandare

Nice

Collapse
 
ssd profile image
server side digest

Thanks brother 😇

Collapse
 
mohmmadaslam profile image
Mohmmad Aslam

Great read

Collapse
 
ssd profile image
server side digest

Thanks Buddy 😊😊☺