Deployment Rolling Update & Rollback- Exercise-02

  • There are two basic ways to deploy to Kubernetes: imperatively, with the many kubectl commands, or declaratively, by writing YAML manifests.
  • It is good practice to use YAML for production.
  • kubectl commands for debugging in production or dev/test environment.

Creating the Deployment

i) Create the Deployment YAML file nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
app: nginx
replicas: 10
app: nginx
app: nginx
- name: nginx
image: nginx:1.14.2
- containerPort: 80

ii) Create the Deployment by running the following command:

$ kubectl apply -f nginx-deployment.yaml --record or 
$ kubectl create -f nginx-deployment.yaml --record

Also, notice that we have used the record option this time.

The advantage of adding on this option is that the entire command will save in the deployment history. I’ll show you how this will help in a minute.

iii) Run kubectl get deployments to check if the Deployment is created.

$ kubectl get deployments

Updating a Deployment

iv) Let’s update the nginx Pods to use the nginx:1.16.1 image instead of the nginx:1.14.2 image.

$ kubectl set image deploy [DEPLOYMENT-NAME] [CONTAINER-NAME]=[CONTAINER-IMAGE]:[TAG] --record$ kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.16.1 --record or 
$ kubectl edit deployment/nginx-deployment

v) To see the rollout status, run

$ kubectl rollout status deployment/nginx-deployment

vi) We can also verify rollout by describe command by using grep command:

$ kubectl describe deployment nginx-deployment |grep Image

vii) Get more details on your updated Deployment:

$ kubectl get deploy,po,rs

Note: Deployment updated the Pods by creating a new ReplicaSet and scaling it up to 10 replicas, as well as scaling down the old ReplicaSet to 0 replicas.

Rolling Back a Deployment

Sometimes, you may want to rollback a Deployment, In case the Deployment is not stable or crash looping.

viii) Updating the Deployment — image name as nginx:1.161 instead of nginx:1.16.1

$ kubectl set image deployment nginx-deployment nginx=nginx:1.161 --record

ix) Check the details of updated pods, replica set, and deployment.

$ kubectl get pods,rs,deploy

Note: If we look above, Kubernetes try to update the new docker image version but Pods created by the new ReplicaSet is stuck in an image pull loop and this is because nginx:1.161 doesn't exist in docker hub.

x) Before fixing the above issue let’s check the rollout history

$ kubectl rollout history deployment/nginx-deployment

xi) To see the details of each revision, run:

$ kubectl rollout history deployment/nginx-deployment --revision=2

xii) To undo the current rollout and rollback to the previous revision:

$ kubectl rollout undo deployment/nginx-deployment

xiii) We can rollback to a specific revision by specifying it with --to-revision:

$ kubectl rollout undo deployment/nginx-deployment --to-revision=1

xiv) Also, check the rollout status by the following command:

$ kubectl rollout status deployment/nginx-deployment
$ kubectl get deploy nginx-deployment -o wide

Manual & automatic scaling a deployment

xv) We can scale a Deployment by using the following command:

$ kubectl scale deployment/nginx-deployment --replicas=15

xvi) Horizontal Pod autoscaling based on the CPU utilization(80%) of your existing Pods.

$ kubectl autoscale deployment/nginx-deployment --min=15 --max=20 --cpu-percent=80

Pausing and Resuming a rollout of a Deployment

  • Kubernetes enables you to pause a Deployment. You can then make adjustments to the Deployment and resume it.
  • Deployments do not need to be paused to make a change. Use the pause to pause a Deployment so that you can make several changes ( that are kept in a queue till resume is ordered).

xvii) Pause by running the following command:

$ kubectl rollout pause deployment/nginx-deployment

xviii) Then update the image of the Deployment with nginx latest version

$ kubectl set image deployment/nginx-deployment nginx=nginx:latest --record

xix) Notice that no new rollout started:

$ kubectl rollout status deployment/nginx-deployment

xx) resume the Deployment rollout and observe a new ReplicaSet coming up with all the new updates:

$ kubectl rollout resume deployment/nginx-deployment

xxi) Check the rollout status now

$ kubectl rollout status deployment nginx-deployment




DevOps Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

On the subject of coding interviews…

Why personal projects are the precursor of freelancing

How to Convert a Grouped Excel File That Contains Merged Cells into JSON Format

Hi everyone, today I have shared about a 3D block chain game project called “Mech Master”.

The Fibonacci RSI. An Interesting Combination in Trading.

Working With PDFs in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


DevOps Engineer

More from Medium

Build RKE single node Cluster

Pods YAML Walkthrough

Learn Kubernetes: Creation of a pod running a Nginx server

Using Kubernetes sidecar containers