Sub topic 12
Tuning maxSurge and maxUnavailable, and when to use Recreate
This chapter assumes you can read a Deployment manifest, know that a Deployment manages ReplicaSets, and have seen a rolling update run. Here you will learn how the two rolling update settings control speed and capacity, and when to skip rolling updates entirely.
The two knobs
A Deployment's spec.strategy field picks how old Pods are replaced. The default type is RollingUpdate, which has two settings:
maxSurgeis how many Pods abovereplicasmay exist during the update.maxUnavailableis how many Pods belowreplicasmay be unavailable during the update.
Each takes an absolute number or a percentage. Percentages are of replicas. maxSurge rounds up and maxUnavailable rounds down. The defaults are 25% for both. Both cannot be 0, because the rollout could not make progress.
Worked example
Take replicas: 4. The table shows what each setting allows.
| maxSurge | maxUnavailable | Pods allowed at once | Minimum available | Effect |
|---|---|---|---|---|
| 1 | 0 | 5 | 4 | Safe and slow. Full capacity is kept. |
| 0 | 1 | 4 | 3 | No extra capacity needed. Runs at reduced capacity. |
| 2 | 1 | 6 | 3 | Faster. Needs room for extra Pods. |
| 100% | 0 | 8 | 4 | Starts all new Pods first, then removes the old ones. |
The manifest below is the safe, slow row. A new Pod must pass its readiness probe before an old Pod is removed.
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: nginx:1.25
readinessProbe:
httpGet:
path: /
port: 80Choosing values
- Capacity-sensitive service: use
maxUnavailable: 0. Without a readiness probe this setting protects little, because a Pod counts as available as soon as its containers start. - Tight cluster quota or no spare nodes: use
maxSurge: 0withmaxUnavailable: 1or more. Surge Pods that cannot be scheduled stall the rollout inPending. - Large Deployments: raise both values. With 100 replicas and the defaults, 25 Pods update per step.
When to use Recreate
type: Recreate stops all old Pods, then starts the new ones. It causes downtime, so use it only when old and new versions must never run together. Examples are a schema migration that breaks the old version, or an app that holds an exclusive lock on a single-writer volume. It takes no rollingUpdate block.
strategy:
type: RecreateExercise
A Deployment has replicas: 10, maxSurge: 30% and maxUnavailable: 25%. What are the maximum total Pods and the minimum available Pods during a rollout?
Show answer
maxSurge 30% of 10 is 3, so at most 13 Pods exist. maxUnavailable 25% of 10 is 2.5, rounded down to 2, so at least 8 Pods stay available.
Exercise
Your cluster has no spare capacity, and a rollout with maxSurge: 1 and maxUnavailable: 0 stalls with a Pending Pod. Which setting change lets the rollout proceed, and what do you give up?
Show answer
Set maxSurge: 0 and maxUnavailable: 1. The rollout then replaces Pods in place without needing extra room, but capacity drops by one Pod during the update.