Rolling Updates & Rollbacks

Difficulty: Intermediate

Question

How do you perform a zero-downtime update of a Deployment in Kubernetes, and how do you roll it back if something goes wrong?

Answer

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.

How a rolling update actually proceeds

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.

What makes it really zero downtime

The strategy alone is not enough. You need:

  1. A readiness probe that reflects real readiness (DB connected, caches warm). Without it, a Pod is considered ready the moment its container starts, and traffic hits a half-booted app.
  2. Graceful shutdown. When a Pod is terminated it gets 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).
  3. Backward compatible changes. During the rollout old and new versions serve traffic together. A DB migration that drops a column will break the old Pods.
  4. Optional safety nets: 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.

Rolling back

  • 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.
  • Record why a change happened with the 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.

Rolling update vs other strategies

StrategyDowntimeExtra capacityRollback speed
RollingUpdateNone if probes are rightUp to maxSurgeAnother rolling update
RecreateYes, all old Pods die firstNoneSlow
Blue-green (manual or Argo Rollouts)None2xInstant Service switch
Canary (Argo Rollouts, Flagger, service mesh)NoneSmallInstant, limited blast radius

Use Recreate when two versions must never run together, for example a single-writer app holding an RWO volume.

Rolling update with maxSurge 1, maxUnavailable 0. Capacity never dips because a new Pod must be Ready before an old one is removed.
  1. Template change creates ReplicaSet v2: For example kubectl set image updates the container image.
  2. Start one v2 Pod: Total Pods temporarily equals replicas plus one.
  3. Wait for readiness probe: Only Ready Pods receive Service traffic.
  4. Terminate one v1 Pod: SIGTERM, preStop hook, then graceful exit.
  5. Repeat until v1 is at zero: Old ReplicaSet is kept for rollback.
  6. Undo if needed: rollout undo scales v1 back up the same way.

Code examples

Deployment tuned for safe rollouts

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.

Rollout and rollback commands

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.

Key points

  • Defaults: maxSurge 25%, maxUnavailable 25%, revisionHistoryLimit 10, progressDeadlineSeconds 600.
  • Only changes to spec.template create a new revision; scaling does not.
  • Readiness probes and graceful shutdown are what actually make it zero downtime.
  • Rollback restores the Pod template only, not ConfigMaps, Secrets or DB schema.
  • Old and new versions run side by side, so changes must be backward compatible.

Common follow-up questions

  • What is the difference between a readiness and a liveness probe?
  • How would you implement a canary release in Kubernetes?
  • Why could users still see 502 errors during a rolling update?
  • How do you handle database migrations with rolling deployments?

Interview tips

  • Go beyond the command: mention readiness probes, preStop hooks and backward compatible migrations.
  • Explain maxSurge and maxUnavailable with a concrete replica count.
  • Point out that rollback does not undo config or schema changes; it shows real incident experience.

Concepts covered

RollingUpdate, maxSurge, maxUnavailable, Readiness Probe, kubectl rollout undo, Revision History

More Kubernetes & Orchestration questions