Deployment object in Kubernetes

Deployment object in Kubernetes

The replication controller and replica set are not able to update and rollback apps in the Cluster.

A Deployment object acts as a supervisor for pods, giving you fine-grain control over how and when a new pod is rolled out, updated or rolled back to a previous state.
When using a deployment object, we first define the state of app, then K8s cluster schedules mentioned app instance and specific individual nodes.
Deployment provides a declarative update for the pod and Replica set.
K8s then monitors, if the nodes hosting an instances goes down or pod is deleted the deployment controller replaces it.
This provides a self-healing mechanism to address machine failure or maintenance.

The following are typical use cases of Deployment

  1. Create a deployment to roll out a Replica set:- The replica Set creates pods in the background to check the status of the Rollout to see if it succeeds or not.

  2. Declare the new state of pods:- By updating the pods template spec. of deployment. A new replica set is created and the deployment manages moving pod from the old replica set to new one at a controller rate. Each new replica set update the revision of the deployment.

  3. Rollback to an earlier Deployment Revision:- If the current state of the deployment is not stable. Each rollback update the revision of the deployment.

  4. Scale up the deployment to facilitate make load.

  5. Pause the deployment to apply multiple fixes to its pod template spec. and then resume it to state a new rollout.

  6. Clean up older Replica sets that you don't need anymore.

If there are problems in deployment, Kubernetes will automatically roll back to the previous version, however, you can explicitly roll back to a specific revision, as in our case to revision,(original pod version).
You can roll back to a specific version by specifying in with '--to-revision'
When you inspect the deployment in your cluster, the following field are displayed.

NAME:- List the name of the deployment into the namespace.
READY:- Display how many replicas of the application are available to your users if follow the pattern ready/desired.
UP-TO-DATE:-Display to number of replicas that have been updated to achieve the desired state.
AVAILABLE:- Display how many replicas of the application are available to your users.
AGE:- Display the amount of time that the application has been running.

Let's perform some practicals based on Deployment and Rollback:-

As usual first install minikube and start the minkube:-

check the minikube status:-

minikube status

if its not running the start the minikube by this command:-

minikube start --driver=docker

just for doing experiment of deployment object in kubernetes....use this yaml code by name "pod.yml"

kind: Deployment
apiVersion: apps/v1
metadata:
   name: mydeployments
spec:
   replicas: 2
   selector:     
    matchLabels:
     name: deployment
   template:
     metadata:
       name: testpod
       labels:
         name: deployment
     spec:
      containers:
        - name: c00
          image: ubuntu
          command: ["/bin/bash", "-c", "while true; do echo Sidharth shukla; sleep 5; done"]

apply the code by this command:-

kubectl apply -f pod.yml

to check the deployment is created or not

kubectl get deploy

to check how deployment create rs and pods

kubectl describe deploy mydeployments

for cheking rs

kubectl get rs

for scale up and down depending upon the no. of replica you can use this command:-

kubectl scale --replicas=1 deploy mydeployments

for this command node the name of pod

kubectl get pod

check the logs by using the pod name...note:- use ur own pod name

kubectl logs -f mydeployments-7f69b86784-thxfk

for rollout related work.........................try next 3 commands later.

kubectl rollout status deployments mydeployments

for cheking rollout history

kubectl rollout history deployments mydeployments

for undoing the rollout....only work when u really try to undo

kubectl rollout undo deploy/mydeployments

Some reasons why Deployments get failed

Your deployment may get stuck trying to deploy its newest replica set without ever completing it. This can occur due to some of the following factors.

  1. Insufficient Quata(when resources are less).

  2. Readiness probe failures.

  3. Images pull errors.

  4. Insufficient Permission.

  5. Limit Ranges.

  6. Application runtime.

just make some changes in pod.yml file......like change echo command type ...hello guys instead of sidharth shukla

kind: Deployment
apiVersion: apps/v1
metadata:
   name: mydeployments
spec:
   replicas: 2
   selector:     
    matchLabels:
     name: deployment
   template:
     metadata:
       name: testpod
       labels:
         name: deployment
     spec:
      containers:
        - name: c00
          image: ubuntu
          command: ["/bin/bash", "-c", "while true; do echo hello guys; sleep 5; done"]

reploy it by using this command

kubectl apply -f pod.yml

check pod

kubectl get pod

check logs....u get result "hello guys"

kubectl logs -f mydeployments-5b7947b7d9-5tb4v

for cheking releases use this commad

kubectl exec mydeployments-5b7947b7d9-5tb4v -- cat /etc/os-release

just scale up by using this command

kubectl scale --replicas=5 deploy mydeployments

now its time to rollback

kubectl rollout undo deploy/mydeployments

check pods

kubectl get pods

check logs

kubectl logs -f mydeployments-5b7947b7d9-tnpgv

u will get result in logs..."sidharth shukla"

Conclusion

You will get a basic understanding of deployment object in Kubernetes and come to know how you can roll back after deployment in Kubernetes. If you got stuck in rollback then to use correct pod id.

Happy learning!