Sub topic 5
Tuning maxSurge and maxUnavailable
This chapter assumes you know Pods, labels, and selectors, can apply YAML manifests, and have used kubectl get and kubectl describe. It also assumes you know that a Deployment manages Pods through ReplicaSets. Here you will learn how two fields set the pace and safety of a rolling update.
The two limits
During a rolling update, the Deployment controller scales the new ReplicaSet up and the old ReplicaSet down. Two fields under spec.strategy.rollingUpdate limit how far it can move at each step.
maxSurgeis how many Pods abovereplicasmay exist during the update.maxUnavailableis how many Pods belowreplicasmay be unavailable during the update.
Both accept an absolute number or a percentage of replicas. The defaults are 25% for each. Percentages are rounded differently: maxSurge rounds up and maxUnavailable rounds down. Both cannot be 0 at the same time, because the rollout could not make any progress.
With replicas: 4, maxSurge: 1, and maxUnavailable: 0, the total Pod count may reach 5. The number of available Pods never drops below 4.
What "available" means
A Pod counts as available only after its readiness probe passes (and after minReadySeconds, if you set it). Without a readiness probe, a Pod is ready as soon as its containers start. The controller can then retire old Pods before the new ones can serve traffic. The limits only protect you if readiness reports the truth.
Choosing values
| Setting | Effect | Cost |
|---|---|---|
| `maxSurge: 1`, `maxUnavailable: 0` | Capacity never drops. Replacement is one Pod at a time. | Slower. Needs room for one extra Pod. |
| `maxSurge: 0`, `maxUnavailable: 1` | No extra capacity needed. | Capacity drops by one during the update. |
| `maxSurge: 50%`, `maxUnavailable: 0` | Fast and safe. | Needs room for 50% more Pods. |
| `maxSurge: 100%`, `maxUnavailable: 0` | Starts a full new set, then removes the old one. | Doubles resource use briefly. |
Raising maxSurge speeds up the rollout but uses more cluster resources and may leave Pods Pending if nodes are full. Raising maxUnavailable also speeds up the rollout, but it lowers capacity while the update runs.
Example
This is the manifest used throughout this tutorial. It adds at most one extra Pod and never removes a ready Pod before its replacement is ready.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 4
revisionHistoryLimit: 5
progressDeadlineSeconds: 300
minReadySeconds: 5
selector:
matchLabels:
app: web
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.26
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
periodSeconds: 5The diagram shows the first steps of an update from this manifest.
Common errors
- Both fields set to 0. The API server rejects the Deployment.
- No readiness probe. Pods count as available immediately, so a broken image can replace working Pods quickly.
maxUnavailable: 0with no spare capacity. The surge Pod staysPending, and the rollout stalls until the progress deadline passes.- Percentages on small replica counts. With
replicas: 2and the 25% defaults,maxSurgerounds up to 1 andmaxUnavailablerounds down to 0. That works, but withreplicas: 3andmaxUnavailable: 25%you get 0 as well, which is easy to overlook.
Exercise
A Deployment has replicas: 10, maxSurge: 25%, and maxUnavailable: 25%. What is the largest and smallest number of Pods that may exist and be available during the update?
Show answer
25% of 10 is 2.5. maxSurge rounds up to 3, so at most 13 Pods exist. maxUnavailable rounds down to 2, so at least 8 Pods are available.
Exercise
You have replicas: 3 and a cluster with no room for a fourth Pod. Which setting lets the rollout proceed without extra capacity, and what does it cost?
Show answer
Set maxSurge: 0 and maxUnavailable: 1. The controller removes one old Pod before creating its replacement, so capacity drops to 2 available Pods during the update.