Lab: RC, Deployment, StatefulSet

In this part we will cover below labs

  • Replication Controller
  • Deployments (Rolling Update and Recreate)
  • StatefulSets

Lab 1:  Replication Controller

kind: The type of Kubernetes resource being defined. Here, it’s a ReplicationController.

spec: The specification for the ReplicationController, which includes:

  • replicas: The desired number of replicas (pods) that the ReplicationController should maintain. In this case, it’s set to 3.
  • selector: A set of labels that the ReplicationController uses to identify the pods it should manage. In this case, it’s selecting pods with the label app: nginx.
 kubectl create -f repc.yaml
kubectl get rc
kubectl describe replicationcontroller hello-dep 
# for deleting rc
kubectl delete -f repc.yaml

Lab2: Replicaset

MatchLabel

kubectl create -f replicasetmatchlabel.yaml
kubectl describe replicaset.apps/my-replicaset
kubectl delete replicaset.apps/my-replicaset

matchexpression

kubectl create -f matchexpressionset.yaml
kubectl describe replicaset.apps/my-replicaiset1

Try to create one pod with same label

kubectl create -f replicasetpod.yaml

Delete the pod

kubectl delete replicaset.apps/my-replicaiset1

Again Create the pod and create rs , and check how many replicas

kubectl create -f replicasetpod.yaml
kubectl create -f matchexpressionset.yaml
kubectl describe replicaset.apps/my-replicaiset1

 

Lab3 : Deployment

This Deployment definition ensures that there will always be three replicas (pods) of the Nginx image (version 1.14.2) running, exposing port 80.

Deployments offer features like rolling updates, scaling, and automated rollout and rollback strategies, making them a more robust way to manage your application’s replicas in Kubernetes.

kubectl create -f dep.yaml
kubectl get deployment
kubectl get pods
kubectl describe deployment nginx-deployment
#For deleting the deployment
kubectl delete -f dep.yaml

Lab3: Rolling update Deployment

strategy: The update strategy for the Deployment. Here, it’s set to RollingUpdate, which ensures a gradual and controlled update of pods.

  • rollingUpdate: Configuration parameters for the rolling update strategy.
    • maxSurge: The maximum number of pods that can be created above the desired replicas during an update. Here, it’s set to 1, meaning one additional pod can be created.
    • maxUnavailable: The maximum number (or percentage) of pods that can be unavailable during an update. Here, it’s set to 25%, which allows up to 25% of pods to be unavailable at a time during the updat
kubectl create -f ru.yaml
kubectl get deployment
kubectl get pods
kubectl get rs
kubectl describe deployment hello-rcp
# update the image
 kubectl set image deployment/hello-rcp hello-dep=nginx:1.16.1
# Check rollout status
kubectl rollout status deployment/hello-rcp
#Rollout history
kubectl rollout history deployment/hello-rcp
#roll back
 kubectl rollout undo deployment/hello-rcp --to-revision=1
#scaleup
 kubectl scale deployment/hello-rcp --replicas=10
#delete
 kubectl delete deployment.apps/hello-rcp

Lab 4: Recreate

strategy: The update strategy for the Deployment. Here, it’s set to Recreate, which means that during an update, the existing pods are scaled down to zero replicas before scaling up the new ones. This is a less sophisticated strategy compared to RollingUpdate, and it can result in temporary downtime during updates.

kubectl create -f rc.yaml
kubectl get deployment
kubectl get pods
kubectl get rs
kubectl describe deployment hello-dep
# update the image
kubectl set image deployment/hello-dep hello-dep=nginx:1.16.1
# Check rollout status
kubectl rollout status deployment/hello-dep
#Rollout history
kubectl rollout history deployment/hello-dep
#roll back
kubectl rollout undo deployment/hello-dep --to-revision=1
#scaleup
kubectl scale deployment/hello-dep --replicas=10
#delete
kubectl delete deployment.apps/hello-dep

Lab 5: Statefulsets

clusterIP: The IP address to assign to the Service. Here, it’s set to None, making it a headless Service.

serviceName: The name of the headless Service associated with the StatefulSet.

This configuration sets up a headless Service named nginx and a StatefulSet named web with Nginx pods. The headless Service is used as the serviceName for the StatefulSet, providing DNS-based discovery for the pods

kubectl create -f sf.yaml
kubectl describe statefulset web
kubectl get pods
# check dns
kubectl run -it --rm --restart=Never dnsutils2 --image=tutum/dnsutils --command -- bash
#perform nslookup to ip of the pod
nslookup 10.244.0.5
exit
#delete one of the pod 
kubectl delete pod web-0
#then again check
kubectl get pods -o wide