Difficulty: Intermediate
How do you perform a zero-downtime update of a Deployment in Kubernetes, and how do you roll it back if something goes wrong?
A Deployment with the default RollingUpdate strategy replaces Pods gradually: it creates a new ReplicaSet for the new Pod template, scales it up while scaling the old one down, and only proceeds when new Pods pass their readiness probe. To roll back, kubectl rollout undo deployment/<name> scales the previous ReplicaSet back up, because Deployments keep old ReplicaSets as revision history.
Changing anything in spec.template (image, env, resources) creates a new revision. The controller then follows two knobs:
maxSurge (default 25%): how many extra Pods above replicas may exist during the rollout.maxUnavailable (default 25%): how many Pods below replicas may be unavailable.With replicas: 4, maxSurge: 1, maxUnavailable: 0, Kubernetes starts one new Pod, waits for it to become Ready, terminates one old Pod, and repeats. Capacity never drops below 4. Changing only replicas does not create a new revision.
The strategy alone is not enough. You need:
SIGTERM, and endpoint removal happens in parallel. Add a short preStop sleep so load balancers stop sending requests before the app exits, and make sure the app finishes in-flight requests within terminationGracePeriodSeconds (default 30s).minReadySeconds (a Pod must stay Ready this long to count as available) and progressDeadlineSeconds (default 600) which marks the rollout as failed if it stalls.kubectl rollout history deployment/api lists revisions. revisionHistoryLimit (default 10) controls how many old ReplicaSets are kept.kubectl rollout undo deployment/api goes to the previous revision; add --to-revision=3 for a specific one.kubernetes.io/change-cause annotation. The old --record flag is deprecated.A rollback only restores the Pod template. It does not revert ConfigMap contents, Secrets or database migrations. Teams at companies like Razorpay often version their ConfigMaps (app-config-v42) so the template change, and therefore the rollback, includes the config too.
| Strategy | Downtime | Extra capacity | Rollback speed |
|---|---|---|---|
RollingUpdate | None if probes are right | Up to maxSurge | Another rolling update |
Recreate | Yes, all old Pods die first | None | Slow |
| Blue-green (manual or Argo Rollouts) | None | 2x | Instant Service switch |
| Canary (Argo Rollouts, Flagger, service mesh) | None | Small | Instant, limited blast radius |
Use Recreate when two versions must never run together, for example a single-writer app holding an RWO volume.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
annotations:
kubernetes.io/change-cause: "bump to 2.3.1, fix UPI callback timeout"
spec:
replicas: 4
revisionHistoryLimit: 10
minReadySeconds: 10
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels: { app: api }
template:
metadata:
labels: { app: api }
spec:
terminationGracePeriodSeconds: 40
containers:
- name: api
image: myorg/api:2.3.1
readinessProbe:
httpGet: { path: /healthz/ready, port: 8080 }
periodSeconds: 5
lifecycle:
preStop:
exec: { command: ["sleep", "10"] } # let endpoints drain
maxUnavailable 0 keeps full capacity, the readiness probe gates traffic, and the preStop sleep avoids dropped requests while endpoints update.
kubectl set image deployment/api api=myorg/api:2.3.1
kubectl rollout status deployment/api # blocks until done or failed
kubectl rollout history deployment/api
kubectl rollout undo deployment/api # previous revision
kubectl rollout undo deployment/api --to-revision=3
kubectl rollout pause deployment/api # batch several edits
kubectl rollout resume deployment/api
rollout status is handy in CI pipelines because it exits non-zero when the progress deadline is exceeded.
RollingUpdate, maxSurge, maxUnavailable, Readiness Probe, kubectl rollout undo, Revision History