Deployments in Kubernetes

What is Deployments in Kubernetes?

A deployment in kubernetes is basically a resource objects that 
provides declarative updates to applications. You can set
images, life cycle of a pods, how many replicas will be running
etc. We work to match application current state to desired
state. Deployments are a higher-level resource that manages
ReplicaSets and Pods.

A pod is a unit having one or more containers which is
responsible for running entire or a part of applications.
It shares storage and networking resources to run applications.
Deployment is a management tool which controls the behavior of
all running pods. It can decide what you are looking for
running applications by managing pods.

We have replica set which is used to run multiple instances of
pods. It can scale up and scale down the replicas as per
requirement basically it can perform the load balancing which
can be a requirement of an application. While Deployment is a
higher level management tool which can manages replica set and
can provide declarative updates to pods with lot of additional
features as well.

Take an example of Deployment now, Below is the example of
codes.
# cat deploy-varelite.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-varelite
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
Now apply it to build a deployment.
# kubectl apply -f deploy-varelite.yaml
deployment.apps/nginx-varelite created

# kubectl get deploy
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
nginx-varelite   3/3     3            3           18m

# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
nginx-varelite-cbdccf466-nh4xm   1/1     Running   0          18m
nginx-varelite-cbdccf466-trcw7   1/1     Running   0          18m
nginx-varelite-cbdccf466-wmkkp   1/1     Running   0          18m

# kubectl get pods -o  wide
NAME                             READY   STATUS    RESTARTS   AGE   IP               NODE      NOMINATED NODE   READINESS GATES
nginx-varelite-cbdccf466-nh4xm   1/1     Running   0          18m   172.16.182.18    worker3   <none>           <none>
nginx-varelite-cbdccf466-trcw7   1/1     Running   0          18m   172.16.235.150   worker1   <none>           <none>
nginx-varelite-cbdccf466-wmkkp   1/1     Running   0          18m   172.16.189.85    worker2   <none>           <none>

Deployment Few Key Features

1. Reliability

Let's test it. I'll delete one of any pod from above deployment
and see what it does.
# kubectl delete pod nginx-varelite-cbdccf466-nh4xm
pod "nginx-varelite-cbdccf466-nh4xm" deleted

# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
nginx-varelite-cbdccf466-26svl   1/1     Running   0          2s
nginx-varelite-cbdccf466-trcw7   1/1     Running   0          20m
nginx-varelite-cbdccf466-wmkkp   1/1     Running   0          20m
You can see in above command. A pod's age is 2s. It means the 
deleted pod got recreated. So it shows us reliability.
2. Scalability

Let's scale up the deployment by adding one more pod.
# kubectl scale --replicas=4 deploy nginx-varelite
deployment.apps/nginx-varelite scaled

# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
nginx-varelite-cbdccf466-26svl   1/1     Running   0          3m46s
nginx-varelite-cbdccf466-rxqpn   1/1     Running   0          3s
nginx-varelite-cbdccf466-trcw7   1/1     Running   0          24m
nginx-varelite-cbdccf466-wmkkp   1/1     Running   0          24m
3. Rolling and Rollback Support

Let's check what is the image version of current application
and we want to upgrade it to latest version we have.
# kubectl describe deploy nginx-varelite | grep -i image
    Image:        nginx:1.14.2
It has running with 4 replicas. I want to upgrade without 
impacting current application. We will change it by editing its
yaml. You can do it by command line as well.
# kubectl edit deploy nginx-varelite
deployment.apps/nginx-varelite edited

# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
nginx-varelite-57d98f69f6-kg47b   1/1     Running   0          8s
nginx-varelite-57d98f69f6-nj4gm   1/1     Running   0          6s
nginx-varelite-57d98f69f6-r4clk   1/1     Running   0          8s
nginx-varelite-57d98f69f6-xt6pn   1/1     Running   0          6s
You can see in above command, Pods have been created few 
seconds away. Now image has been upgraded.
# kubectl describe deploy nginx-varelite | grep -i image
    Image:        nginx:1.16
Let's go ahead and check its history. So that we can rollback 
if we have any fallout during upgrade.
# kubectl rollout history deploy nginx-varelite
deployment.apps/nginx-varelite
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

# kubectl rollout undo deploy nginx-varelite --to-revision=1
deployment.apps/nginx-varelite rolled back

# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
nginx-varelite-cbdccf466-6fbfm   1/1     Running   0          4s
nginx-varelite-cbdccf466-b8fhw   1/1     Running   0          4s
nginx-varelite-cbdccf466-mgrfh   1/1     Running   0          3s
nginx-varelite-cbdccf466-nmfzk   1/1     Running   0          2s

# kubectl describe deploy nginx-varelite | grep -i image
    Image:        nginx:1.14.2

Leave a Reply

Your email address will not be published. Required fields are marked *