How to Rollback Deployment in Kubernetes Quickly and Safely
To rollback a deployment in Kubernetes, use the
kubectl rollout undo deployment/DEPLOYMENT_NAME command. This command reverts the deployment to the previous stable version, helping you quickly fix issues caused by recent changes.Syntax
The basic syntax to rollback a deployment is:
kubectl rollout undo deployment/DEPLOYMENT_NAME: Rolls back to the previous revision.--to-revision=N: Optional flag to rollback to a specific revision number.
bash
kubectl rollout undo deployment/DEPLOYMENT_NAME [--to-revision=N]
Example
This example shows how to rollback a deployment named webapp to its previous version.
bash
kubectl rollout undo deployment/webapp
Output
deployment.apps/webapp rolled back
Common Pitfalls
Common mistakes when rolling back deployments include:
- Using the wrong deployment name, causing the rollback to fail.
- Not checking the deployment history before rollback, which can lead to unexpected versions.
- Assuming rollback fixes all issues without verifying the deployment status after rollback.
Always verify the deployment status with kubectl rollout status deployment/DEPLOYMENT_NAME after rollback.
bash
kubectl rollout undo deployment/wrong-name # Correct command: kubectl rollout undo deployment/webapp
Quick Reference
| Command | Description |
|---|---|
| kubectl rollout undo deployment/DEPLOYMENT_NAME | Rollback to previous deployment revision |
| kubectl rollout undo deployment/DEPLOYMENT_NAME --to-revision=2 | Rollback to specific revision number 2 |
| kubectl rollout status deployment/DEPLOYMENT_NAME | Check current rollout status |
| kubectl rollout history deployment/DEPLOYMENT_NAME | View deployment revision history |
Key Takeaways
Use 'kubectl rollout undo deployment/DEPLOYMENT_NAME' to rollback to the previous version.
Check deployment history with 'kubectl rollout history' before rolling back.
Always verify the deployment status after rollback using 'kubectl rollout status'.
Specify '--to-revision=N' to rollback to a specific revision if needed.
Ensure you use the correct deployment name to avoid rollback errors.