Rolling Deployments
A rolling deployment replaces the instances of a service a few at a time: start some new ones, wait until they are healthy, stop some old ones, repeat. During the rollout both versions serve traffic. It is the default in Kubernetes Deployments, Docker Swarm services, AWS ECS and most autoscaling groups, because it needs no extra infrastructure.
Two numbers control it (Kubernetes names, used by most tools in some form):
| Setting | Meaning | Kubernetes default |
|---|---|---|
maxSurge |
how many instances above the desired count may exist during the update | 25 % |
maxUnavailable |
how many instances below the desired count may be not-ready | 25 % |
apiVersion: apps/v1
kind: Deployment
metadata:
name: shop
spec:
replicas: 4
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0 # never go below 4 ready pods
minReadySeconds: 10 # a new pod must stay ready this long before it counts
# ... selector and pod template with a readinessProbeThe Kubernetes course shows kubectl rollout status, history and undo. Here we look at the behaviour.
A simulation
The model runs a rollout of 4 replicas step by step. Each tick, it starts new pods as far as maxSurge allows, removes old pods as far as maxUnavailable allows, and a new pod becomes ready two ticks after it starts. It prints the pods per version and the ready capacity:
import math
def rollout(replicas, max_surge, max_unavailable, start_ticks=2, show=True):
old_ready, new_starting, new_ready = replicas, [], 0 # new_starting: ticks left per pod
tick, min_capacity = 0, replicas
while new_ready < replicas or old_ready > 0:
# a pod that finished starting becomes ready
new_starting = [t - 1 for t in new_starting]
new_ready += sum(1 for t in new_starting if t == 0)
new_starting = [t for t in new_starting if t > 0]
# scale down old pods while keeping ready >= replicas - max_unavailable
removable = old_ready + new_ready - (replicas - max_unavailable)
old_ready -= max(0, min(old_ready, removable))
# scale up new pods while keeping total <= replicas + max_surge
room = replicas + max_surge - (old_ready + new_ready + len(new_starting))
wanted = replicas - new_ready - len(new_starting)
new_starting += [start_ticks] * max(0, min(room, wanted))
ready = old_ready + new_ready
min_capacity = min(min_capacity, ready)
if show:
print(f" t={tick}: old={old_ready} new_ready={new_ready} starting={len(new_starting)} "
f"ready={ready}/{replicas}")
tick += 1
return tick, min_capacity
for surge, unavail in [(1, 0), (0, 1), (2, 2)]:
print(f"maxSurge={surge} maxUnavailable={unavail}")
ticks, cap = rollout(4, surge, unavail)
print(f" -> done after {ticks} ticks, lowest ready capacity {cap}/4")maxSurge=1 maxUnavailable=0
t=0: old=4 new_ready=0 starting=1 ready=4/4
t=1: old=4 new_ready=0 starting=1 ready=4/4
t=2: old=3 new_ready=1 starting=1 ready=4/4
t=3: old=3 new_ready=1 starting=1 ready=4/4
t=4: old=2 new_ready=2 starting=1 ready=4/4
t=5: old=2 new_ready=2 starting=1 ready=4/4
t=6: old=1 new_ready=3 starting=1 ready=4/4
t=7: old=1 new_ready=3 starting=1 ready=4/4
t=8: old=0 new_ready=4 starting=0 ready=4/4
-> done after 9 ticks, lowest ready capacity 4/4
maxSurge=0 maxUnavailable=1
t=0: old=3 new_ready=0 starting=1 ready=3/4
t=1: old=3 new_ready=0 starting=1 ready=3/4
t=2: old=2 new_ready=1 starting=1 ready=3/4
t=3: old=2 new_ready=1 starting=1 ready=3/4
t=4: old=1 new_ready=2 starting=1 ready=3/4
t=5: old=1 new_ready=2 starting=1 ready=3/4
t=6: old=0 new_ready=3 starting=1 ready=3/4
t=7: old=0 new_ready=3 starting=1 ready=3/4
t=8: old=0 new_ready=4 starting=0 ready=4/4
-> done after 9 ticks, lowest ready capacity 3/4
maxSurge=2 maxUnavailable=2
t=0: old=2 new_ready=0 starting=4 ready=2/4
t=1: old=2 new_ready=0 starting=4 ready=2/4
t=2: old=0 new_ready=4 starting=0 ready=4/4
-> done after 3 ticks, lowest ready capacity 2/4
Read the three runs as trade-offs:
maxSurge: 1, maxUnavailable: 0: full capacity throughout, at the cost of one extra pod and a slower rollout. The safe choice for services running near capacity.maxSurge: 0, maxUnavailable: 1: no extra resources (useful when the cluster or licence count is tight), but capacity drops to 3/4 during the rollout.2, 2: fast, but half the capacity can be missing at once.
Kubernetes rounds a percentage maxSurge up and maxUnavailable down, so 25 % of 4 replicas is 1 and 1, and both can't be zero at once.
What can go wrong
| Problem | Why | Defence |
|---|---|---|
| bad version takes all traffic | the rollout continues as long as pods become ready | a meaningful readiness probe; minReadySeconds; progressDeadlineSeconds to mark the rollout failed |
| two versions at once | old and new serve requests side by side | API and database changes must be backward compatible for one release (Rollback) |
| sessions dropped | old pods are killed with open connections | graceful shutdown on SIGTERM, a preStop delay, connection draining at the load balancer |
| slow rollback | rolling back is another rolling update | keep the old ReplicaSet (revisionHistoryLimit), or use blue/green for instant switch-back |
A rolling update doesn't analyse anything: a pod that passes its readiness probe but returns errors for real users still replaces a healthy one. Canary releases add that analysis (Canary Releases).
import math
replicas = 10
for surge_pct, unavail_pct in [(25, 25), (10, 10), (50, 0)]:
surge = math.ceil(replicas * surge_pct / 100) # rounded up
unavail = math.floor(replicas * unavail_pct / 100) # rounded down
print(f"{replicas} replicas, maxSurge {surge_pct}% -> {surge}, maxUnavailable {unavail_pct}% -> {unavail}: "
f"between {replicas - unavail} and {replicas + surge} pods")10 replicas, maxSurge 25% -> 3, maxUnavailable 25% -> 2: between 8 and 13 pods
10 replicas, maxSurge 10% -> 1, maxUnavailable 10% -> 1: between 9 and 11 pods
10 replicas, maxSurge 50% -> 5, maxUnavailable 0% -> 0: between 10 and 15 pods
Test yourself
With maxSurge: 1 and maxUnavailable: 0, what happens to ready capacity during a rollout?
- It drops by one
- It never drops below the desired count; one extra pod runs during the update
- It doubles
- It drops to zero briefly
A new version passes its readiness probe but returns 500s to users. What does a plain rolling update do?
- Stops automatically
- Keeps going and replaces every healthy pod, because it only looks at readiness
- Rolls back
- Pauses for approval
10 replicas with maxUnavailable: 25%: how many may be unavailable?
- 3 (rounded up)
- 2 (rounded down)
- 2.5
- 25