Rolling Updates and Rollbacks: Zero-Downtime Deployments
Kubernetes rolling update guide: maxSurge, maxUnavailable, readiness, preStop, PodDisruptionBudgets and rollbacks for zero downtime.
maxSurge and maxUnavailable, and only moves on when new pods pass their readiness probe. That makes zero-downtime deployments possible but not automatic. You also need graceful shutdown so old pods finish in-flight requests, a short preStop delay so traffic stops arriving before the app exits, database changes that both versions can work with, and a progress deadline and rollback plan for when a release goes wrong.
A team deploys several times a day with the default Deployment settings, and every deployment produces a small burst of 502 errors at the load balancer. The new pods are healthy, the old pods are healthy, and still some requests fail. The culprit is the few seconds between a pod being told to shut down and every load balancer and node learning that it is gone. Kubernetes gives you the tools to close that gap, but you have to use them.
A rolling update is the default Deployment strategy: when the pod template changes, Kubernetes creates a new ReplicaSet and shifts replicas from the old one to the new one in steps, as introduced in the workload model article. A rollback is the same process in reverse, pointing the Deployment back at a previous ReplicaSet’s template.
Controlling the pace
spec:
replicas: 6
minReadySeconds: 10
progressDeadlineSeconds: 300
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25% # extra pods allowed above 6 during the rollout
maxUnavailable: 0 # never drop below 6 ready pods
| Setting | Meaning | Trade-off |
|---|---|---|
maxSurge |
How many pods above the desired count may exist | Higher means faster rollouts but needs spare capacity |
maxUnavailable |
How many pods below the desired count may be unavailable | Higher is faster and needs no spare capacity, but reduces serving capacity |
minReadySeconds |
How long a new pod must stay ready before it counts as available | Catches pods that pass readiness then crash shortly after |
progressDeadlineSeconds |
How long a rollout may make no progress before it is marked failed | Default 600 seconds; lower it so failures surface quickly |
Both surge and unavailable default to 25%. For services that must keep full capacity, set maxUnavailable: 0 and a positive maxSurge, so a new pod must become ready before an old one is removed. The cluster needs room for the extra pods; otherwise the rollout stalls with pending pods.
Readiness is the gate for every step. If new pods never pass their readiness probe, for example because the new version cannot reach its database, the rollout pauses with the old pods still serving. That is the single most important safety property of rolling updates, and it only works if the readiness probe tests something real.
Closing the shutdown gap
When Kubernetes removes an old pod, two things happen in parallel: the pod is removed from Service endpoints, which then propagates to kube-proxy on every node and to external load balancers, and the containers receive SIGTERM. If the application exits immediately on SIGTERM, requests that are still being routed to it during propagation fail. The fix has two parts:
spec:
terminationGracePeriodSeconds: 45
containers:
- name: api
lifecycle:
preStop:
sleep:
seconds: 10
- A preStop delay. The
preStophook runs beforeSIGTERMis sent. A short sleep keeps the pod serving while endpoint removal propagates. The nativesleepaction shown here is a beta feature enabled by default in Kubernetes 1.30, released last month; on older versions, useexec: { command: ["sleep", "10"] }, which needs asleepbinary in the image. - Graceful shutdown in the app. On
SIGTERM, stop accepting new connections, finish in-flight requests, close database connections and exit. The grace period must cover thepreStopdelay plus the longest request you want to finish.
Long-lived connections, such as WebSockets and gRPC streams, need the application to tell clients to reconnect elsewhere, because no amount of waiting drains a connection that never ends on its own.
Protecting availability during other disruptions
Rollouts are not the only time pods are removed. Node upgrades, autoscaler scale-downs and kubectl drain evict pods too. A PodDisruptionBudget limits how many pods of an application such voluntary disruptions can take down at once:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: billing-api
spec:
maxUnavailable: 1
selector:
matchLabels:
app: billing-api
Do not set a budget that can never be satisfied, such as maxUnavailable: 0 or minAvailable equal to the replica count: node drains will hang until someone intervenes.
Database changes: expand, then contract
During a rolling update, old and new versions run at the same time and share the same database. A migration that renames a column breaks whichever version does not expect it. Use the expand-and-contract pattern:
- Expand: add the new column or table in a backward-compatible way; deploy code that writes to both and reads the new one with a fallback.
- Migrate: backfill existing data.
- Contract: in a later release, once no running version uses the old column, remove it.
The same applies to API contracts between services and to message formats on queues. Rolling updates require every change to be compatible with the previous version for at least one release.
Watching and rolling back
kubectl rollout status deploy/billing-api --timeout=5m # exits non-zero if the rollout fails
kubectl rollout history deploy/billing-api
kubectl rollout undo deploy/billing-api # previous revision
kubectl rollout undo deploy/billing-api --to-revision=12
kubectl rollout pause deploy/billing-api # stop mid-rollout to inspect
kubectl rollout resume deploy/billing-api
Make kubectl rollout status part of your deployment pipeline so a stuck rollout fails the pipeline rather than silently timing out ten minutes later. Record why each revision exists by setting the kubernetes.io/change-cause annotation on the Deployment, such as the commit or release version; the old --record flag is deprecated.
Two caveats about rollbacks. rollout undo only reverts the pod template; it does not revert ConfigMaps, Secrets or database migrations, which is another reason for versioned configuration and expand-contract migrations. And if you deploy through GitOps, roll back by reverting the commit, or the next sync will undo your rollback.
Recreate, canary and blue-green
- Recreate stops all old pods before starting new ones. It causes downtime, but it is the right choice when two versions must never run at the same time, or when pods share a single-attach volume.
- Canary releases send a small percentage of traffic to the new version first and watch metrics before continuing. A simple version uses two Deployments behind one Service, with replica counts setting the ratio. Argo Rollouts and Flagger automate weighted canaries with metric analysis and automatic rollback, usually through an ingress controller or service mesh.
- Blue-green runs the full new version alongside the old and switches the Service selector at once, giving instant rollback at the cost of double capacity during the release.
Frequently Asked Questions
How do rolling updates work in Kubernetes?
When a Deployment’s pod template changes, Kubernetes creates a new ReplicaSet and gradually scales it up while scaling the old one down, within the limits of maxSurge and maxUnavailable, waiting for new pods to pass readiness before continuing.
Why do I get errors during Kubernetes deployments?
Usually because old pods stop before every load balancer and node has removed them from rotation, or because the app exits without finishing in-flight requests. Add a preStop delay, handle SIGTERM gracefully and make sure readiness probes reflect real readiness.
How do I roll back a Kubernetes deployment?
Run kubectl rollout undo deployment/NAME, optionally with –to-revision. It restores a previous pod template but not ConfigMaps, Secrets or database changes. With GitOps, revert the commit instead.
What is a PodDisruptionBudget?
An object that limits how many pods of an application can be unavailable during voluntary disruptions such as node drains and cluster autoscaler scale-downs, so maintenance never takes too many replicas down at once.
Zero downtime is a set of habits
Kubernetes provides the mechanics: gradual replacement, readiness gating, revision history. Zero downtime comes from the habits around them: honest readiness probes, a preStop delay, graceful shutdown, disruption budgets, backward-compatible changes and a pipeline that watches every rollout. With those in place, deploying on a busy afternoon stops being an event. The official documentation is worth keeping open: the rolling update section of the Deployment documentation and the Pod disruptions documentation. Related reading on this site: the Horizontal Pod Autoscaler and Ingress controllers.
Originally posted on 21 May 2024 and post last modified on 21 May 2024