Wednesday, March 5, 2025

k8s Practices

 

Deployment of Simple Nginx

  • minikube status
  • minikube start

  • kubectl create deployment my-nginx --image=nginx:latest
  • kubectl get deployments
  • kubectl get pods
  • minikube dashboard

Try http://localhost/

  • kubectl expose deployment my-nginx --port=80 --type=LoadBalancer
  • minikube service my-nginx

Try Again http://localhost/

  • kubectl get pods
  • kubectl delete deployment my-app


Demo Reactjs Webapp Project

Step 1) Setup Fresh React Project On local

Step 2) Craete Dockerfile

FROM node
WORKDIR /app/
COPY . .
RUN npm install
EXPOSE 3000
CMD ["npm", "start"]

Step 3) Build and deploy to Dockerhub

docker build -t subhash1583/webapp:V1
docker login
docker push subhash1583/webapp:V1

Step 4) Create Project

kubectl create deployment my-webapp --image=reponame/webapp-demo:V1
kubectl get deployments
kubectl get pods

Step 5) Exposing Port

kubectl expose deployment my-webapp --port=3000 --type=LoadBalancer

kubectl get services
minikube service my-webapp


Scaling our App

  • kubectl scale deployment node-app --replicas=4
  • kubectl get pods
  • kubectl scale deployment node-app --replicas=2
  • kubectl get pods


Rollout in K8

Make Changes in src/App.js

  • docker build -t subhash1583/webapp:v3 .
  • docker push subhash1583/webapp:v3

Map Docker image with kubernets so that latest docker build v3 get depolyed

  • kubectl set image deployment my-webapp write-container-name=subhash1583/webapp:v3
  • kubectl get pods


Rollback in K8

Map intentionally Wrong Docker image tag with kubernets

  • kubectl set image deployment my-webapp write-container-name=subhash1583/webapp:v4
  • kubectl get pods

Showing error status : ImagePullBackOff 

To Stop(Rollout) this

  • kubectl rollout undo deployment my-webapp


Self-Healing in K8

Demo In local System:

https://drive.google.com/drive/folders/1PsUe7IV5BGoSN7ieiXMKzJ4gBOVgsJjM

npm i
node index.js

  • http://localhost:3000/
  • http://localhost:3000/exit

Demo With K8: Using Existing docker image to save time

  • kubectl create deployment node-app --image=philippaul/node-demo-app:v1
  • kubectl get deployment
  • kubectl get pods

  • kubectl expose deployment node-app --port=3000 --type=LoadBalancer
  • minikube service node-app

  • http://localhost:3000/
  • http://localhost:3000/exit


  • YAML config For Deployment: 1

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#deployment-v1-apps

vim deployment-node-app.yaml

  • kubectl apply -f deployment-node-app.yaml
  • kubectl get pods

vim deployment-node-app.yaml

Change Replica no (in up or down)

  • kubectl apply -f deployment-node-app.yaml
  • kubectl get pods


  • YAML config For Services: 2

https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.32/#service-v1-core

vim service-node-app.yaml

  • kubectl apply -f service-node-app.yaml
  • minikube service service-my-node-app


Multiple containers in single POD

Not Configured Yet.


Multiple containers in Separate POD

https://drive.google.com/drive/folders/1Fqjxbf1N1BY-vTveAT-itnUCjsDZ-j0b?usp=sharing


* Changes in node project (Dynamic Host & Port For DB URL)

* Building image & pushing image with 03 version


vim index.js

  • add :

const mongoHost = process.env.MONGO_HOST || 'localhost';
const mongoPort = process.env.MONGO_PORT || '27017';


  • Replace from : mongoose.connect('mongodb://mongo:27017/yourDatabaseName'
  • Replace this : mongoose.connect(`mongodb://${mongoHost}:${mongoPort}/yourDatabaseName`


  • docker build --platform linux/amd64 -t subhash1583/node-mongo-db:v3 .
  • docker push subhash1583/node-mongo-db:v3


First create combine deployment and service config file for MONGO APP so we will be having service name

vim mongo-db.yaml

  • kubectl apply -f mongo-db.yaml
  • kubectl get pods

Now create deployment and service config for NODE APP and explain how to use env variables in config

vim node-app.yaml


Now we will create configMAP file

vim mongo-config.yaml


Now update Existing file Again

vim node-app.yaml

  • From This:

spec:
containers:
- name: node-app
image: subhash1583/node-mongo-db:v9

  • To This: 

spec:
containers:
- name: node-app
image: subhash1583/node-mongo-db:v9
env:
- name: MONGO_HOST
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_HOST
- name: MONGO_PORT
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_PORT


 Run node-app deployment

  • kubectl apply -f mongo-config.yaml
  • kubectl apply -f node-app.yaml
  • minikube service service-node-app

Now Try:

  • http://localhost:3000/
  • http://localhost:3000/exit

Drawback: If the MongoDB pod is deleted, all stored data will be lost since Kubernetes pods are ephemeral.

Solution: To prevent data loss, we will configure Persistent Volumes (PV) and Persistent Volume Claims (PVC), ensuring data persists even if the pod is restarted or replaced.


  • Create Persistent Volume

vim host-pv.yml


  • Create Persistent Volume Claim
vim host-pvc.yml


Now update Existing file (to add the volume with mongo config)

  • From This:

spec:
containers:
- name: mongo-app
image: mongo:latest

  • To This: 

spec:
containers:
- name: mongo-app
image: mongo:latest
volumes:
- name: mongo-vol
persistentVolumeClaim:
claimName: host-pvc


Now update Existing file Again (to point the volume with mongo container)


  • From This:

spec:
containers:
- name: mongo-app
image: mongo:latest
volumes:
- name: mongo-vol
persistentVolumeClaim:
claimName: host-pvc

  • To This: 

spec:
containers:
- name: mongo-app
image: mongo:latest
volumeMounts:
- mountPath: /data/db
name: mongo-vol
volumes:
- name: mongo-vol
persistentVolumeClaim:
claimName: host-pvc

  • kubectl apply -f host-pv.yml
  • kubectl apply -f host-pvc.yml
  • kubectl apply -f mongo-db.yml


  • kubectl get pv
  • kubectl get pvc


Delete All Setup


  • kubectl delete pv pv-name
  • kubectl delete pvc pvc-name
  • kubectl delete deployment deployment-name
  • kubectl delete service service-name






No comments:

Post a Comment

testing