π 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.
π 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
π«£ 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.
π 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
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
- Now, Run the command
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
- Now, Run the following commands to forward your port
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.
π₯³ 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)
Usable information.
Thanks Buddy π
Nice
Thanks brother π
Great read
Thanks Buddy ππβΊ