Running Nodejs application using K8s
- Docker account and Docker desktop installed, must be in running state
- Choco package manager
- Kubectl https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/
- minikube , enable hyperv on windows os https://minikube.sigs.k8s.io/docs/start/
- Check status using " minikube status "
- start minikube cluster : "minikube start --driver=hyperv"
- now check minikube status again
minikube status minikube type: Control Plane host: Running kubelet: Running apiserver: Running kubeconfig: Configured
- Clone this repository in VS code
- got to current project directory and go to terminal
- initiate project with json file and to install required dependencies , its ok to delete npm_module folder later on before doing actual k8s work. Do "npm init -y" and then "npm install express"
- if you want to clear docker build cache "docker builder prune -a"
- Make sure to logiin to your personal docker account - "docker login"
- then build project with - "docker build . -t namu123/k8s-web-hello" . This will push your repo to docker hub which has account name namu123
- now you can check running docker images with - "docker images"
- Now, this repository is dockerized & now its time add it to k8s cluster & perform other activities like scaling, accessing on cluster ip , adding load balancer port service etc. For this below remaining prt
- go to powershell in windows and do "minikube start"
- Check status: minikube status minikube type: Control Plane host: Running kubelet: Running apiserver: Running kubeconfig: Configured
- kubectl get pods
- kubectl get deploy
- kubectl get svc
- To create new deployment of current project - " kubectl create deployment k8s-web-hello --image=namu123/k8s-web-hello "
- This will start one pod in cluster - "kubectl get pods" NAME READY STATUS RESTARTS AGE k8s-web-hello-785b88d575-5vxbb 1/1 Running 0 35s
"kubectl get deploy" NAME READY UP-TO-DATE AVAILABLE AGE k8s-web-hello 1/1 1 1 8m13s
"kubectl scale deploy k8s-web-hello --replicas=4" deployment.apps/k8s-web-hello scaled
kubectl get pods NAME READY STATUS RESTARTS AGE k8s-web-hello-785b88d575-5vxbb 1/1 Running 0 9m22s k8s-web-hello-785b88d575-fcdps 1/1 Running 0 29s k8s-web-hello-785b88d575-h5lgh 1/1 Running 0 29s k8s-web-hello-785b88d575-mwjd6 1/1 Running 0 29s
kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES k8s-web-hello-785b88d575-5vxbb 1/1 Running 0 9m32s 10.244.0.3 minikube k8s-web-hello-785b88d575-fcdps 1/1 Running 0 39s 10.244.0.5 minikube k8s-web-hello-785b88d575-h5lgh 1/1 Running 0 39s 10.244.0.4 minikube k8s-web-hello-785b88d575-mwjd6 1/1 Running 0 39s 10.244.0.6 minikube
- Now , add service to access web app on port. Use - "expose deployment k8s-web-hello --port=3000" -> service/k8s-web-hello exposed
- Now check services, "kubectl get svc" NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE k8s-web-hello ClusterIP 10.107.120.24 3000/TCP 14s kubernetes ClusterIP 10.96.0.1 443/TCP 10m
- Now go inside minikube cluster and access webapp using ip and port
minikube ssh _ _ _ _ ( ) ( ) ___ ___ () ___ ()| |/') _ _ | |_ __ /' _
_
| |/' _\| || , < ( ) ( )| '_
\ /'\ | ( ) ( ) || || ( ) || || |\
\ | () || |) )( / () () ()()() ()()() ()\___/'(_,__/'
__)
$ curl 10.107.120.24:3000 VERSION 2: Hello from the k8s-web-hello-785b88d575-5vxbb$ $ curl 10.107.120.24:3000; echo VERSION 2: Hello from the k8s-web-hello-785b88d575-mwjd6
$ curl 10.107.120.24:3000 VERSION 2: Hello from the k8s-web-hello-785b88d575-h5lgh$ 20. Now, you can delete service and expose again with new type NodePort OR Load Balancer using " kubectl expose deployment k8s-web-hello --type=NodePort --port=300" OR " kubectl expose deployment k8s-web-hello --type=LoadBalancer --port=3000"