Kubernetes Deployments demo with update and rollback

We’re going to follow this demo by creating a Deployment for nginx image. Below is the example yaml file that I’m using for creating the Deployment as deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    tier: frontend
spec:
  replicas: 6
  template:
    metadata:
      name: nginx-2
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx
          image: nginx
  selector:
    matchLabels:
      app: myapp

First we’ll create the deployment using the below command and record it:

$ kubectl create -f deployment.yaml --record

Now, let’s quickly check the status of the deployment:

$ kubectl rollout status deployment/myapp-deployment

The above command will give output as below:

Waiting for deployment "myapp-deployment" rollout to finish: 0 of 6 updated replicas are available...
Waiting for deployment "myapp-deployment" rollout to finish: 1 of 6 updated replicas are available...
Waiting for deployment "myapp-deployment" rollout to finish: 2 of 6 updated replicas are available...
Waiting for deployment "myapp-deployment" rollout to finish: 3 of 6 updated replicas are available...
Waiting for deployment "myapp-deployment" rollout to finish: 4 of 6 updated replicas are available...
Waiting for deployment "myapp-deployment" rollout to finish: 5 of 6 updated replicas are available...
deployment "myapp-deployment" successfully rolled out

To view the version history, run below command:

$ kubectl rollout history deployment/myapp-deployment

//You can see below output with change-cause..
deployment.apps/myapp-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment.yaml --record=true

Now let’s edit the deployment and change the nginx version to a lower version:

$ kubectl edit deployment myapp-deployment --record

Search for the image name and change the section as below and save with wq! vi command:

containers:
  image: nginx:1.18


//output after saving with wq!:
deployment.apps/myapp-deployment edited

Run the status command again to see the effect of Rolling updates:

$ kubectl rollout status deployment/myapp-deployment

You can also run the describe deployment command to check the effect as below:

$ kubectl describe deployment myapp-deployment


//Check the events section in the output as below:
Events:
  Type    Reason             Age                    From                   Message
  ----    ------             ----                   ----                   -------
  Normal  ScalingReplicaSet  13m                    deployment-controller  Scaled up replica set myapp-deployment-fd8649446 to 6
  Normal  ScalingReplicaSet  6m9s                   deployment-controller  Scaled up replica set myapp-deployment-ddbb47dd8 to 2
  Normal  ScalingReplicaSet  6m9s                   deployment-controller  Scaled down replica set myapp-deployment-fd8649446 to 5
  Normal  ScalingReplicaSet  6m9s                   deployment-controller  Scaled up replica set myapp-deployment-ddbb47dd8 to 3
  Normal  ScalingReplicaSet  5m40s                  deployment-controller  Scaled down replica set myapp-deployment-fd8649446 to 4
  Normal  ScalingReplicaSet  5m40s                  deployment-controller  Scaled up replica set myapp-deployment-ddbb47dd8 to 4
  Normal  ScalingReplicaSet  5m34s                  deployment-controller  Scaled down replica set myapp-deployment-fd8649446 to 3
  Normal  ScalingReplicaSet  5m34s                  deployment-controller  Scaled up replica set myapp-deployment-ddbb47dd8 to 5
  Normal  ScalingReplicaSet  5m27s                  deployment-controller  Scaled down replica set myapp-deployment-fd8649446 to 2
  Normal  ScalingReplicaSet  5m17s (x3 over 5m26s)  deployment-controller  (combined from similar events): Scaled down replica set myapp-deployment-fd8649446 to 0

The Annotations in the above description from the output after you run the describe command, will show the revision and the image is now nginx:1.18.

Let’s again change the nginx version using a set mage command and record it and check the status:

$ kubectl set image deployment myapp-deployment nginx=nginx:1.18-perl --record

Check the recorded versions again:

$ kubectl rollout history deployment/myapp-deployment


//output as below:
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment.yaml --record=true
2         kubectl edit deployment myapp-deployment --record=true
3         kubectl set image deployment myapp-deployment nginx=nginx:1.18-perl --record=true

You can see above how we changed between different nginx versions and the reason for it got recorded. Verify your pods, as all 6 should be running:

$ kubectl get pods

Let’s rollback the last version and see how we move back to the previous version, then check the version again which should show as Revision 4:

$ kubectl rollout undo deployment/myapp-deployment

$ kubectl rollout status deployment/myapp-deployment

$ kubectl describe deployment myapp-deployment

//check the nginx image should be nginx:1.18 in the description..

Check the versions again:

$ kubectl rollout history deployment/myapp-deployment

deployment.apps/myapp-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment.yaml --record=true
3         kubectl set image deployment myapp-deployment nginx=nginx:1.18-perl --record=true
4         kubectl edit deployment myapp-deployment --record=true

Please note that the Revision 2 earlier now shows as Revision 4 after undo.

Once you’re done, delete the Deployments as below:

$ kubectl delete deployment myapp-deployment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.