This blog post will show you how to roll back a deployment on a Kubernetes cluster using kubectl.
When working with Kubernetes, the preferred method of object deployment is to use deployment because it allows managing releases in versions.
In my case, I have a deployment that I updated with a new image, as I showed in the previous article.
To roll back the deployment, I will start by checking the deployment history using the following command.
kubectl rollout history deployment nginx-deployment
Below you can see the output of the command. It also important you use annotations to add details to the deployments.
deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 2 3 6 Update to 12 pods 7 Update to nginx version 1.19.7
From the status output, I will note the revision number of the deployment I would like to roll back to and run the following command.
kubectl rollout undo deployment nginx-deployment --to-revision=7
From here, Kubernetes will roll back the deployment.
Another option is to undo a deployment of the previous version using the undo option which doesn’t require to specify a revision number.
kubectl rollout undo deployments nginx-deployment