To rollback a Kubernetes deployment, you can use the kubectl rollout undo command. Here’s a step-by-step guide:
Steps to Rollback a Deployment
-
Check Rollout History: First, check the rollout history of the deployment to see the available revisions.
kubectl rollout history deployment <deployment-name> -
Rollback to Previous Revision: Use the following command to rollback to the previous revision:
kubectl rollout undo deployment <deployment-name> -
Verify Rollback Status: After initiating the rollback, check the status to ensure it completed successfully:
kubectl rollout status deployment <deployment-name> -
Confirm the Current Deployment: You can verify the current state of the deployment and the image version being used:
kubectl get pods -l app=<app-label> -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}{end}' -
Check Rollout History Again: Finally, confirm the rollout history to see the updated revisions:
kubectl rollout history deployment <deployment-name>
Example
If your deployment is named web-app, the commands would look like this:
kubectl rollout history deployment web-app
kubectl rollout undo deployment web-app
kubectl rollout status deployment web-app
kubectl get pods -l app=web -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}{end}'
kubectl rollout history deployment web-app
Conclusion
This process allows you to revert to a previous stable version of your application quickly and efficiently. If you have any further questions or need assistance, feel free to ask!
