Show contents
Chapter 5 of 7 · 20 min
Updates and scaling
By the end of this chapter, you'll be able to
- Explain how a rolling update unfolds
- Set maxSurge and maxUnavailable according to the accepted risk
- Go back to the previous version with a rollback
- Compute the number of replicas the horizontal autoscaler chooses
The rolling update
When you change a Deployment's image (for example from 1.4.2 to 1.4.3), Kubernetes does not replace every pod at once. By default, it performs a rolling update: it creates a few pods of the new version, waits until they are ready (thanks to the readiness probe), removes as many old pods, and starts again. Two settings frame this pace:
maxSurge: how many pods above the desired number may be created during the update;maxUnavailable: how many pods below it are tolerated.
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
With maxSurge: 1 and maxUnavailable: 0, capacity never drops: we go up to 5 pods, then back down to 4, one version swap at a time. Simulation:
replicas, max_surge = 4, 1
old, new, step = replicas, 0, 0
print(f"step {step}: {old} old, {new} new")
while new < replicas:
step += 1
new += 1
assert old + new <= replicas + max_surge
old -= 1
print(f"step {step}: +1 new ready, -1 old → {old} old, {new} new")
step 0: 4 old, 0 new
step 1: +1 new ready, -1 old → 3 old, 1 new
step 2: +1 new ready, -1 old → 2 old, 2 new
step 3: +1 new ready, -1 old → 1 old, 3 new
step 4: +1 new ready, -1 old → 0 old, 4 new
Throughout the update, at least 4 ready pods serve customers. If a new pod never becomes ready, the update stops by itself, and the old pods keep serving.
We repeat until every pod runs the new version.
Text description of the animation
- Four pods of the old version serve customers.
- maxSurge allows one extra pod: a pod of the new version is created.
- We wait until it is ready. If it never is, the update stops here, with no damage.
- The new pod receives traffic: one old pod is removed. Capacity never dropped.
- We repeat until every pod runs the new version.
Going back
Kubernetes keeps a Deployment's version history. If the fix causes trouble, you go back to the previous version with one command:
kubectl set image deployment/payment-api api=registry.example.com/payment-api:1.4.3
kubectl rollout status deployment/payment-api
kubectl rollout undo deployment/payment-api
rollout status follows the update to the end; rollout undo starts a rolling update back to the previous version.
The horizontal autoscaler
The HorizontalPodAutoscaler (HPA) adjusts the number of replicas according to a metric, most often CPU usage, as a percentage of the requests. Its basic formula:
desired replicas = round up (current replicas × current metric ÷ target metric)
To avoid changing all the time for small variations, it does not move if the metric ÷ target ratio stays between 0.9 and 1.1, and it always stays between a set minimum and maximum.
import math
def hpa(current, metric, target, minimum=2, maximum=10):
ratio = metric / target
if abs(ratio - 1) <= 0.1:
return current
return max(minimum, min(maximum, math.ceil(current * ratio)))
for current, metric in [(4, 90), (6, 30), (3, 61), (8, 200)]:
print(f"{current} pods at {metric}% → {hpa(current, metric, 60)} pods")
4 pods at 90% → 6 pods
6 pods at 30% → 3 pods
3 pods at 61% → 3 pods
8 pods at 200% → 10 pods
With a 60% target: 4 pods at 90% go to 6; 6 pods at 30% go down to 3; 3 pods at 61% do not move (a ratio of 1.02, within tolerance); 8 pods at 200% would want 27 pods, but the maximum is 10.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: payment-api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: payment-api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
Mini-lab: a day of trafficPython 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: capacity during the update
Write bounds(replicas, max_surge, max_unavailable) that returns the maximum number of pods at the same time and the minimum number of available pods during a rolling update. Compare the settings (1, 0), (0, 1) and (2, 1) for 4 replicas.
Hint 1
replicas + max_surge, the minimum available replicas - max_unavailable.Hint 2
max_surge and max_unavailable are both 0, the update can never progress: raise an error.Show the solution and common mistakes
def bounds(replicas, max_surge, max_unavailable):
if max_surge == 0 and max_unavailable == 0:
raise ValueError("maxSurge and maxUnavailable cannot both be 0")
return replicas + max_surge, replicas - max_unavailable
for setting in [(1, 0), (0, 1), (2, 1)]:
high, low = bounds(4, *setting)
print(f"surge={setting[0]}, unavailable={setting[1]}: up to {high} pods, at least {low} available")
surge=1, unavailable=0: up to 5 pods, at least 4 available
surge=0, unavailable=1: up to 4 pods, at least 3 available
surge=2, unavailable=1: up to 6 pods, at least 3 available
(1, 0) never lowers capacity but needs extra room in the cluster; (0, 1) needs none, but serves customers with one pod less during the update; (2, 1) goes faster, at the cost of both.
Common mistakes:
- Accepting
(0, 0): Kubernetes rejects it, since no pod could be added or removed. - Forgetting that these settings can also be percentages (
25%by default): on 4 replicas, 25% is 1 pod.
Summary
- 1A rolling update replaces pods in small groups, waiting for the new ones to be ready.
- 2maxSurge allows extra pods, maxUnavailable fewer pods: (1, 0) never lowers capacity.
- 3kubectl rollout status follows an update, kubectl rollout undo goes back to the previous version.
- 4The horizontal autoscaler computes replicas from usage, as a percentage of requests, between a minimum and a maximum.
- 5Without requests, the autoscaler cannot measure anything and never changes the number of pods.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.