Estimated reading time: 3 minutes
Rolling back a Kubernetes deployment is an essential capability for managing application stability. Whether it’s due to a failed update or an unexpected bug, Kubernetes makes it easy to revert to a previous deployment version. This guide explains how to perform deployment rollbacks in Kubernetes.
What is a Kubernetes Deployment Rollback?
In Kubernetes, a rollback is the process of reverting a deployment to a previous revision. Each deployment update creates a new revision, allowing administrators to restore a known working state when something goes wrong.
Key Features:
- Kubernetes maintains revision history for deployments.
- Rollbacks are quick and do not require downtime when properly managed.
- Custom configurations allow fine-grained control over rollback behavior.
Pre-Requisites
- Kubernetes Cluster: Ensure you have a running Kubernetes cluster.
- Kubectl Access: The
kubectl
CLI tool must be configured to interact with the cluster. - Deployment Resource: A deployment resource must already exist in the cluster.
Checking Deployment History
Before rolling back, it’s important to review the deployment’s revision history.
Command:
kubectl rollout history deployment/<deployment-name>
Example:
kubectl rollout history deployment/nginx-deployment
Output:
REVISION CHANGE-CAUSE
1 Initial deployment
2 Updated image to nginx:1.19
3 Rolled back to nginx:1.18
This command shows all recorded revisions for the specified deployment.
Performing a Rollback
To rollback a deployment, use the kubectl rollout undo
command.
Command:
kubectl rollout undo deployment/<deployment-name>
Example:
kubectl rollout undo deployment/nginx-deployment
This restores the deployment to the most recent working revision.
Rolling Back to a Specific Revision
If you need to rollback to a specific revision, specify the revision number.
Command:
kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number>
Example:
kubectl rollout undo deployment/nginx-deployment --to-revision=2
This command restores the deployment to revision 2
.
Verifying the Rollback
After performing a rollback, verify the deployment status to ensure the process was successful.
Command:
kubectl rollout status deployment/<deployment-name>
Example:
kubectl rollout status deployment/nginx-deployment
Output:
deployment "nginx-deployment" successfully rolled out
Best Practices for Rollbacks
1. Set Change Causes: Annotate deployments with --record
during updates to maintain clear revision histories.
kubectl set image deployment/nginx-deployment nginx=nginx:1.19 --record
2. Monitor Deployment Health: Use kubectl rollout status
to track progress and identify issues.
3. Limit Revision History: Configure the revisionHistoryLimit
field in your deployment manifest to control how many revisions are stored:
spec:
revisionHistoryLimit: 5
4. Automate Alerts: Use monitoring tools like Prometheus or Grafana to trigger alerts during deployment failures.
Example Scenario: Rolling Back a Failed Update
Update a deployment:
kubectl set image deployment/nginx-deployment nginx=nginx:1.20 --record
Check the status:
kubectl rollout status deployment/nginx-deployment
If the update causes errors, rollback to the previous version:
kubectl rollout undo deployment/nginx-deployment
Confirm the rollback:
kubectl rollout history deployment/nginx-deployment
Common Issues and Solutions
No Revisions Available:
- Cause: Deployment has no prior revisions.
- Solution: Ensure updates are performed with the
--record
flag.
Rollback Fails:
- Cause: Underlying pods may not start successfully.
- Solution: Inspect pod logs with:
kubectl logs pod/<pod-name>
Slow Rollback:
- Cause: High resource usage or insufficient capacity.
- Solution: Monitor cluster resources and optimize autoscaling.