Deployments in Kubernetes








Let's forget about kubernetes and containers for sometime and imagine a normal scenario where any application is running in an environment, for obvious reasons we will want to have multiple instances of the applications running. Now, a newer version of application came in and application needs to be upgraded. Here we will want that application upgrade should happen on one instance at a time so that the users are not impacted.

This kind of upgrade is called Rolling updates.




Another thing is if in case one of the instance that were being updated went to an error state. now we would surely like to rollback the update and bring it back to the last stable state.


Let's say there are number of steps involved in an update and we would like to pause the environment so that after all steps are executed , all changes are deployed at once together.


The YAML syntax for deployment is very much same as that of a replica set except for a kind which is deployment here.


Example


In the below example we will create an nginx deployment running three replicas.


[root@node1 kubernetes]# cat nginx_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  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
[root@node1 kubernetes]# kubectl create -f nginx_deployment.yaml
deployment.apps/nginx-deployment created
[root@node1 kubernetes]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   0/3     3            0           13s

Updating a deployment


Let's update the nginx Pods to use the nginx:1.16.1 image instead of the nginx:1.14.2 image.


[root@node1 kubernetes]# kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1 --record
deployment.apps/nginx-deployment image updated
[root@node1 kubernetes]# kubectl rollout status deployment.v1.apps/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out


Check roll out history of a deployment


[root@node1 kubernetes]# kubectl rollout history deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1 --record=true

Rolling back to a previous version


[root@node1 kubernetes]# kubectl rollout undo deployment.v1.apps/nginx-deployment
deployment.apps/nginx-deployment rolled back
[root@node1 kubernetes]# kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           12m
[root@node1 kubernetes]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 16 Jun 2020 16:56:53 +0530
Labels