Prompt
How do I set up rolling updates and rollbacks in Kubernetes?
Latest observation
In Kubernetes, rolling updates and rollbacks are built into Deployments. Here’s the practical setup and how to use them.
1) Use a Deployment
Rolling updates only work automatically when your app is managed by a Deployment (not a bare Pod).
Example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:1.25
ports:
- containerPort: 80
2) How rolling updates work
When you change the container image or pod template, Kubernetes gradually replaces old Pods with new ones.
Important fields:
maxUnavailable: how many Pods can be down during the updatemaxSurge: how many extra Pods can be created above desired replicas during the update
Example meaning:
replicas: 3maxUnavailable: 1maxSurge: 1
Kubernetes can temporarily have up to 4 Pods, while ensuring at least 2 remain available.
3) Trigger an update
Update the image in the Deployment:
kubectl set image deployment/my-app my-app=nginx:1.26
Or edit the YAML and apply it:
kubectl apply -f deployment.yaml
4) Watch the rollout
Check rollout status:
kubectl rollout status deployment/my-app
See rollout history:
kubectl rollout history deployment/my-app
View details of a specific revision:
kubectl rollout history deployment/my-app --revision=2
5) Roll back to a previous version
If the new version is bad, roll back:
kubectl rollout undo deployment/my-app
That reverts to the previous revision.
To roll back to a specific revision:
kubectl rollout undo deployment/my-app --to-revision=2
6) Make rollbacks possible
Keep revision history with:
revisionHistoryLimit: 10
This tells Kubernetes to retain old ReplicaSets so you can undo to them later.
7) Best practices
- Add readiness probes so traffic only goes to healthy Pods
- Use liveness probes to restart stuck containers
- Set resource requests/limits
- Keep
maxUnavailablelow for availability - Test updates in staging before production
Example probes:
readinessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: 80
initialDelaySeconds: 15
periodSeconds: 20
8) Optional: pause and resume rollouts
Pause a rollout:
kubectl rollout pause deployment/my-app
Resume:
kubectl rollout resume deployment/my-app
Useful if you want to make multiple changes without triggering intermediate rollouts.
If you want, I can also give you:
- a full working Deployment YAML with probes and update strategy, or
- a step-by-step demo showing update and rollback commands.