Sub topic 3
Rolling Deployments: Replacing Instances in Batches
This chapter assumes you can run a containerized web service behind a load balancer. It also assumes you know what a Kubernetes Deployment and Service are, and that you have read the previous chapter on readiness checks.
The working model
A rolling deployment replaces old instances with new ones in small batches. At any moment, some instances run the old version and some run the new one. The load balancer sends traffic only to instances that report ready. When every instance runs the new version, the rollout is complete.
Rolling is the default strategy in most orchestrators because it needs no extra environment. It uses the capacity you already have, plus a small temporary margin.
The two settings that control the pace
Kubernetes exposes two settings on a RollingUpdate strategy.
maxSurgeis how many pods above the desired replica count may exist during the update. It can be a number or a percentage.maxUnavailableis how many pods below the desired count may be unready during the update.
Together they set the batch size and the capacity you keep during the rollout.
| Setting | Effect | Cost |
|---|---|---|
| `maxSurge: 1`, `maxUnavailable: 0` | Full capacity throughout, one pod at a time | Needs room for one extra pod, slower |
| `maxSurge: 0`, `maxUnavailable: 1` | No extra capacity needed | Capacity drops by one pod during the update |
| `maxSurge: 25%`, `maxUnavailable: 25%` | Larger batches, faster | Less predictable capacity and a larger blast radius |
A worked example
This Deployment runs four replicas. It adds one new pod, waits for it to pass its readiness probe, and only then removes an old pod.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: example/web:2.0
ports:
- containerPort: 8080
readinessProbe:
httpGet:
path: /healthz
port: 8080
periodSeconds: 5Because maxUnavailable is 0, Kubernetes never drops below four ready pods. Because maxSurge is 1, it never runs more than five pods. The readiness probe is what makes this safe. Without it, Kubernetes treats a pod as ready as soon as its container starts, and the old pods can be removed before the new ones can serve requests.
To start the rollout, apply the manifest with a changed image tag. Then watch it:
kubectl rollout status deployment/web
kubectl rollout undo deployment/webThe first command blocks until the rollout finishes or stalls. The second returns the Deployment to its previous revision. Run it only if the new version turns out to be bad.
Common errors
- Missing or shallow readiness probe. If
/healthzreturns 200 before the app can reach its database, the rollout continues while new pods fail real requests. Make the probe check what the app needs to serve traffic. - A stalled rollout. If new pods never become ready, Kubernetes stops adding them. Old pods keep serving, so users are mostly unaffected, but the rollout hangs.
kubectl rollout statusshows this. The Deployment'sprogressDeadlineSeconds(600 seconds by default) marks the rollout as failed after that time. It does not roll back for you. - Dropped connections. When an old pod terminates, in-flight requests can fail. Handle SIGTERM in the app, finish current requests, and set a suitable
terminationGracePeriodSeconds. - Mixed versions. For the length of the rollout, both versions serve traffic. A user can hit the old version on one request and the new version on the next. API responses, cached data, and database schemas must work with both versions at once. A later chapter covers this constraint.
- Slow starts hidden by fast pacing. If the app takes 60 seconds to warm up, set
minReadySecondsor astartupProbeso a pod is not counted as available too early.
Where rolling fits
Rolling suits stateless services, tolerates only brief mixed-version periods, and costs almost nothing extra. Its weakness is rollback speed. Undoing a bad release means running another rolling update in reverse, which takes as long as the first one did.
Exercise
A Deployment has 10 replicas, maxSurge: 2, and maxUnavailable: 0. What is the most pods that can exist during the update, and the fewest ready pods? Name one risk if the readiness probe always returns 200.
Show answer
At most 12 pods exist (10 + 2) and at least 10 are ready. If the probe always returns 200, Kubernetes counts new pods as ready before they can serve requests, so it removes old pods early. Users can receive errors even though the rollout appears healthy.