Deploying first nodejs application in Kubernetes cluster

Deploying first nodejs application in Kubernetes cluster

Step1. Create a docker image of Nodejs app, build it and Push to docker hub.

Node Image repo link of dockerhub:- https://hub.docker.com/r/sidharthhhh/node

Docker file code

FROM node:latest

COPY . /home/app

WORKDIR /home/app/

RUN npm install

EXPOSE 3000

CMD ["node", "app"]
docker build --tag sidharthhhh/node

In place of "sidharthhhh/node" put your "dockerusername/imagename"
And then check docker images by command

docker images

Push to docker hub by command

docker login
docker push sidharthhhh/node:latest

Step2. Prepare Kubernetes service and deployment file

deployment.yml

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

service.yml

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

step3- Deploy pods and service by following commands

First start minikube locally by command

minikube start --driver=docker

Check cluster status

minikube status

Deploy pods

kubectl apply -f deployment.yml
kubectl get pods

deploy service

kubectl apply -f service.yml
kubectl get svc

Step4- Validate the deployment and Access your application

kubectl get deployment
minikube service node-app-service

Conclusion

Finally, we deploy first node js application successfully. If you like the content then like and share with every person who needs.

I test above code with this nodejs source code locally , Github Link:- https://github.com/sidharthhhh/javascriptNotes/tree/main/node/day7.
This process will work in every basic nodejs application application but if you include a database then you have do some configuration in your deployment.yml and service.yml file.

Thanks for your support everyone.