How do you roll back a failed deployment in Kubernetes?
Roll back a failed Kubernetes Deployment with kubectl rollout undo. Learn revision history, revisionHistoryLimit and how probe-gated rollbacks avoid downtime.
Expected Interview Answer
You roll back a failed Deployment with kubectl rollout undo deployment/<name>, which reverts the Deployment's pod template to a previous ReplicaSet revision that Kubernetes retains in its rollout history.
Kubernetes keeps a bounded history of ReplicaSets for each Deployment (controlled by revisionHistoryLimit). kubectl rollout history shows the revisions, and rollout undo restores the previous one, or a specific one with --to-revision=N. The rollback is itself a rolling update: the old ReplicaSet is scaled up and the failed one scaled down while readiness probes gate the transition. Good rollout strategy, readiness probes and progressDeadlineSeconds let Kubernetes detect a stalled rollout so you can undo before it affects all replicas.
- Restores a known-good version quickly
- No manifest edits needed for an emergency revert
- Rollback is gradual and probe-gated, avoiding downtime
- History lets you target a specific revision
- Works with the same rolling-update machinery
AI Mentor Explanation
Rolling back a Deployment is like a captain reversing a bad bowling change: the new bowler is leaking runs, so the captain brings back the previous bowler who was containing the batters. Kubernetes keeps a record of who bowled each over (revision history) and can restore the earlier spell over-by-over rather than all at once, so the innings never collapses during the switch.
Step-by-Step Explanation
Step 1
Detect the failure
Check kubectl rollout status deployment/<name>; a stalled rollout past progressDeadlineSeconds signals a bad release.
Step 2
Inspect history
Run kubectl rollout history deployment/<name> to see available revisions and their change-cause.
Step 3
Undo the rollout
Run kubectl rollout undo deployment/<name> to revert to the previous revision, or add --to-revision=N to target one.
Step 4
Watch the rollback
Monitor kubectl rollout status again; the old ReplicaSet scales up as the failed one scales down, gated by readiness probes.
Step 5
Confirm health
Verify pods are Ready and traffic is served, then investigate the root cause of the failed release.
What Interviewer Expects
- Knowledge of kubectl rollout undo and rollout history
- Understanding that Deployments retain ReplicaSet revisions
- Awareness of revisionHistoryLimit and its effect
- Role of readiness probes and progressDeadlineSeconds
- That rollback is itself a rolling update, not a hard swap
Common Mistakes
- Manually deleting pods instead of reverting the Deployment
- Not knowing rollout undo can target a specific revision
- Forgetting that a low revisionHistoryLimit discards revisions
- Assuming rollback is instant rather than a rolling update
- Ignoring readiness probes so failures go undetected
Best Answer (HR Friendly)
“In Kubernetes you can undo a bad update with a single command that switches the app back to the last working version. The system keeps a history of previous versions, so recovery is quick and happens gradually to avoid downtime.”
Code Example
# check whether the rollout stalled
kubectl rollout status deployment/web
# view revision history
kubectl rollout history deployment/web
# revert to the previous revision
kubectl rollout undo deployment/web
# or revert to a specific revision
kubectl rollout undo deployment/web --to-revision=3
# watch the rollback complete
kubectl rollout status deployment/webapiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
revisionHistoryLimit: 10
progressDeadlineSeconds: 120
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
annotations:
kubernetes.io/change-cause: "deploy v1.4.2"Follow-up Questions
- What does revisionHistoryLimit control?
- How does progressDeadlineSeconds help detect a failed rollout?
- What is the difference between rollout undo and rollout restart?
- How do readiness probes affect a rolling update?
- How would you automate rollback in a CI/CD pipeline?
MCQ Practice
1. Which command reverts a Deployment to its previous revision?
kubectl rollout undo reverts the Deployment's pod template to the previous ReplicaSet revision retained in history.
2. What determines how many old revisions a Deployment keeps for rollback?
revisionHistoryLimit caps the number of old ReplicaSets retained; setting it too low discards revisions you might need to roll back to.
3. How does a rollback actually apply the previous version?
A rollback scales the old ReplicaSet up and the new one down as a rolling update, using readiness probes to gate the transition and avoid downtime.
Flash Cards
Command to roll back a Deployment? — kubectl rollout undo deployment/<name>, optionally with --to-revision=N.
Where does Kubernetes store rollback history? — In previous ReplicaSets retained per Deployment, bounded by revisionHistoryLimit.
How do you see available revisions? — kubectl rollout history deployment/<name>, which lists revisions and their change-cause.
Is a rollback instantaneous? — No — it is a rolling update; the old ReplicaSet scales up while the failed one scales down, gated by readiness probes.
What helps detect a stalled rollout? — progressDeadlineSeconds combined with readiness probes marks a rollout as failed when it makes no progress.