Kubernetes Rolling Update and Rollback
Master zero-downtime Kubernetes deployments using RollingUpdate, maxSurge, maxUnavailable, preStop hooks, revision history, and rollbacks.
Kubernetes Deployments provide declarative lifecycle updates for Pods and ReplicaSets, enabling continuous software releases without user-facing downtime. By configuring the RollingUpdate strategy, tuneable surge parameters (maxSurge and maxUnavailable), readiness probes, and graceful termination hooks (preStop), engineering teams can safely release new container versions, monitor progression in real time, and roll back immediately when faults occur.
Deployment Strategies: RollingUpdate vs Recreate
Kubernetes Deployments support two primary update strategies specified under spec.strategy.type:
| Strategy | Behavior | Downtime Risk | Resource Overhead | Best Suited For |
|---|---|---|---|---|
| RollingUpdate | Incrementally replaces old Pods with new ones while maintaining service availability. | Zero downtime (when properly configured) | High (requires extra capacity during surge) | Production web services, stateless APIs, consumer apps |
| Recreate | Terminates all old Pods before creating new ones. | Guaranteed downtime during switchover | Low (no additional surge capacity required) | Legacy workloads unable to run two versions simultaneously (e.g., singleton DB lock) |
Understanding maxSurge and maxUnavailable
The pacing of a rolling update is governed by two parameters:
maxSurge: The maximum number (or percentage) of Pods that can be created above the configuredreplicastarget during an update. (e.g., with 4 replicas andmaxSurge: 25%, up to 5 total Pods can exist simultaneously).maxUnavailable: The maximum number (or percentage) of Pods that can be unavailable during the update process. (e.g., settingmaxUnavailable: 0ensures full capacity is maintained at all times).
Zero-Downtime Deployment Manifest
The following manifest configures a production zero-downtime rolling update with a preStop hook to drain in-flight HTTP connections:
apiVersion: apps/v1
kind: Deployment
metadata:
name: store-frontend
namespace: production
labels:
app: store-frontend
spec:
replicas: 4
revisionHistoryLimit: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 0
selector:
matchLabels:
app: store-frontend
template:
metadata:
labels:
app: store-frontend
spec:
terminationGracePeriodSeconds: 45
containers:
- name: store-frontend
image: store-frontend:v2.1.0
lifecycle:
preStop:
exec:
command: ["sh", "-c", "sleep 10"]
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 5
periodSeconds: 3
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
Rollout Commands and Rollback Workflow
# Update container image to trigger a new rolling update
kubectl set image deployment/store-frontend store-frontend=store-frontend:v2.2.0 -n production
# Monitor the progression of the rollout
kubectl rollout status deployment/store-frontend -n production
# View the deployment revision history
kubectl rollout history deployment/store-frontend -n production
# Inspect details of a specific historical revision
kubectl rollout history deployment/store-frontend --revision=2 -n production
# Roll back immediately to the previous revision
kubectl rollout undo deployment/store-frontend -n production
# Roll back to a specific revision
kubectl rollout undo deployment/store-frontend --to-revision=1 -n production
# Pause and resume a canary rollout
kubectl rollout pause deployment/store-frontend -n production
kubectl rollout resume deployment/store-frontend -n production
Common Mistakes
- Setting maxUnavailable to 100%: Setting
maxUnavailable: 100%causes all existing Pods to be terminated immediately, effectively transforming the rolling update into a downtime-inducing recreate deployment. - Missing Readiness Probes: Without readiness probes, Kubernetes considers new Pods ready as soon as the container process starts, killing old Pods before new containers have finished warming up or initializing connections.
- Omitting preStop hooks and graceful termination: When a Pod terminates, kube-proxy and iptables take several seconds to propagate endpoint removal. Without a
preStopsleep hook, active in-flight HTTP requests receive 502/504 errors. - Setting revisionHistoryLimit to 0: Disabling revision history prevents
kubectl rollout undofrom finding previous ReplicaSets, making instant rollbacks impossible.
Best Practices
- Use
maxSurge: 25%andmaxUnavailable: 0for mission-critical web applications where capacity cannot drop below 100%. - Always pair rolling updates with accurate
readinessProbedefinitions. - Include a small
preStophook (sleep 5-10s) to ensure load balancers and ingress controllers complete endpoint deregistration before the application stops accepting requests. - Configure
terminationGracePeriodSecondsto exceed your longest-running in-flight request plus thepreStopdelay.
Interview Questions
1. How do you achieve true zero-downtime deployments in Kubernetes?
Zero downtime requires three synchronized configurations:
strategy.rollingUpdate.maxUnavailable: 0so replica capacity never drops below the required count.- A dependable
readinessProbeso traffic is only routed to new Pods once fully initialized. - A
lifecycle.preStophook (e.g.sleep 10) paired with sufficientterminationGracePeriodSecondsso the terminating Pod drains in-flight connections while kube-proxy updates endpoint tables.
2. What triggers a new rollout revision in a Deployment?
A new rollout revision is only triggered when the Pod template specification (spec.template) in the Deployment manifest is mutated (such as changing container images, environment variables, labels, or resource requests). Scaling replicas (spec.replicas) or modifying deployment metadata does not create a new revision.
3. How does kubectl rollout undo work behind the scenes?
When kubectl rollout undo is executed, the Deployment controller identifies the target revision's underlying ReplicaSet from historical ReplicaSets. It updates the Deployment's spec.template to match that ReplicaSet's template and performs a standard rolling update, scaling up the old ReplicaSet while scaling down the current one.
4. What is the difference between maxSurge and maxUnavailable?
maxSurge controls how many additional Pods above the desired replica count can be provisioned during an update (scaling up new version first). maxUnavailable controls how many Pods below the desired replica count can be taken offline during an update (scaling down old version first).
5. Why is a preStop hook necessary when terminating a Pod during a rolling update?
Pod termination and endpoint deregistration happen asynchronously in Kubernetes. When a Pod enters Terminating state, kubelet sends a SIGTERM signal at the same moment the endpoint controller updates Endpoints. Because iptables/IPVS updates take several seconds to propagate across all nodes, new requests may still be routed to the dying container. A preStop: sleep 10 delays process shutdown until network routing rules have fully converged.
6. What does kubectl rollout pause do?
kubectl rollout pause suspends active deployment updates. This allows DevOps engineers to implement canary rollouts by applying a change, allowing one or two canary Pods to spin up, pausing the rollout to verify telemetry, and then resuming (kubectl rollout resume) or rolling back.
Summary
RollingUpdateenables progressive replacement of Pods without application downtime.maxSurgeandmaxUnavailabledetermine rollout pacing and cluster capacity headroom.- Readiness probes and
preStophooks are mandatory to eliminate dropped connections. - Deployment revisions are tracked via ReplicaSets and can be restored using
kubectl rollout undo.
Continue Learning
Previous: Auto Scaling HPA | Next: Kubernetes Security
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.