Introduction
In modern cloud-native applications, deployments happen frequently. Teams release new features, bug fixes, and updates regularly. However, not every deployment goes as planned. Sometimes, a new release introduces bugs, performance issues, or crashes.
This is where rollback in Kubernetes becomes extremely important.
Rollback allows you to safely return to a previous stable version of your application without downtime or major impact on users.
What is Rollback in Kubernetes?
Rollback in Kubernetes means reverting your application to a previous working version.
π Example:
π Rollback β Go back to Version 1
Kubernetes makes this easy using its built-in Deployment history feature.
Why Rollback is Important in Kubernetes
1. Quick Recovery from Failed Deployments
If a new deployment fails, rollback helps you recover instantly.
π This reduces downtime and protects user experience.
2. Safer Releases
Teams can deploy confidently knowing rollback is available.
3. Maintain Application Stability
Rollback ensures your system remains stable even after failed updates.
4. Essential for CI/CD Pipelines
Rollback is a key part of modern DevOps and Kubernetes CI/CD pipelines.
How Kubernetes Handles Deployment Versions
Kubernetes keeps track of deployment history using ReplicaSets.
π Each deployment creates a new ReplicaSet.
This allows Kubernetes to switch between versions easily.
Step-by-Step: Rollback Deployment in Kubernetes
Letβs understand how to rollback safely.
Step 1: Check Deployment Status
First, check your deployment status:
kubectl get deployments
π This shows all running deployments.
Step 2: View Deployment History
kubectl rollout history deployment <deployment-name>
π Example:
kubectl rollout history deployment my-app
Output
π Each revision represents a version.
Step 3: Inspect Specific Revision (Optional)
kubectl rollout history deployment my-app --revision=1
π Helps you understand what changed.
Step 4: Perform Rollback
To rollback to previous version:
kubectl rollout undo deployment my-app
π This rolls back to the last working version.
Step 5: Rollback to Specific Revision
kubectl rollout undo deployment my-app --to-revision=1
π Useful when you want a specific version.
Step 6: Verify Rollback
kubectl rollout status deployment my-app
π Ensures rollback is successful.
Real-World Example
Imagine you deployed a new version with a bug:
kubectl apply -f deployment.yaml
After deployment:
Users report errors
API starts failing
π Immediate fix:
kubectl rollout undo deployment my-app
π Within seconds, system returns to stable state.
Understanding Rolling Updates and Rollbacks
Kubernetes uses rolling updates by default.
π This means:
If something fails:
Best Practices for Safe Rollback in Kubernetes
1. Always Use Deployment (Not Pods)
Deployments support rollback.
π Pods alone do not support versioning.
2. Use Readiness and Liveness Probes
These ensure only healthy pods receive traffic.
π Helps detect failures early.
3. Set Proper Resource Limits
Avoid crashes due to memory or CPU issues.
4. Use Versioned Images
Do not use "latest" tag.
π Example:
myapp:v1
myapp:v2
5. Monitor Application with Logs and Metrics
Use tools like:
π Helps identify issues quickly.
6. Test Before Production Deployment
Always test in staging environment.
Common Mistakes to Avoid
β Using "latest" Docker tag
β Not checking rollout status
β Skipping monitoring
β Not maintaining deployment history
Advanced Tip: Pause and Resume Deployment
You can pause deployment before making changes:
kubectl rollout pause deployment my-app
After updates:
kubectl rollout resume deployment my-app
π Useful for controlled releases.
Real-World Use Cases
Rollback in Kubernetes is used in:
π Almost every production system uses rollback strategy.
Summary
Rollback in Kubernetes is a powerful feature that allows you to safely revert to a previous stable version of your application. By using commands like kubectl rollout undo and understanding deployment history, you can quickly recover from failures and maintain system stability. Following best practices like versioned images, monitoring, and proper testing ensures that your Kubernetes deployments remain reliable and production-ready.