App
Rolling update
New version, zero downtime
Change the image of a running Deployment and watch two ReplicaSets trade Pods one at a time. At every moment at least three Pods are ready to serve, so capacity never dips. Avoiding dropped requests also takes an app that shuts down politely and clients that retry.
- Command
- kubectl set image deployment/web web=…:1.5.0
- Replicas
- 3
- Strategy
- RollingUpdate · maxSurge 25% · maxUnavailable 25%
- Which means
- at most 4 Pods, never fewer than 3 ready
Read the whole lesson as text
1. Change the image
kubectl sends a patch that changes one field: the container image, from 1.4.2 to 1.5.0. Because the Pod template changed, the Deployment's generation goes up and the template gets a new hash. The Deployment controller sees that no ReplicaSet matches the new hash.
- kubectl changes one field in the Deployment: the image version.
- The Deployment controller notices the Pod template changed, and needs new Pods.
2. Surge one
The Deployment controller creates a ReplicaSet for the new template. How fast it can go comes from the strategy: 25% of 3 Pods is 0.75. maxSurge rounds up to 1, so one extra Pod is allowed. maxUnavailable rounds down to 0, so no old Pod may go until a new one is ready. The new Pod is scheduled onto node-1.
- It creates a second ReplicaSet for the new version, starting with 1 Pod.
- That ReplicaSet creates one new Pod.
- The scheduler places the new Pod on node-1.
3. Wait for ready
node-1 pulls 1.5.0, which is quick because three of its five layers are already there from 1.4.2. The container starts, but the rollout waits for the readiness probe. Once it passes, the new Pod joins the Service's endpoints. For a short while both versions answer requests, so your app has to cope with that.
- node-1 pulls the new image. Most of it is already there, so it's quick.
- The new Pod passes its health check.
- The new Pod is reported Ready.
- It joins the Service's endpoints. Old and new versions now both serve traffic.
4. Retire one
With one new Pod ready, there are 4 ready Pods and only 3 are needed. The Deployment controller scales the old ReplicaSet from 3 to 2. The ReplicaSet controller picks which Pod to delete: it prefers Pods that aren't ready, then Pods on a node crowded with web Pods, then the most recently ready. Here that's one on node-2. The Pod is marked for deletion and taken out of the endpoints right away.
- With an extra Pod ready, the old ReplicaSet can shrink from 3 to 2.
- It marks one old Pod, on node-2, for deletion.
- That Pod is taken out of the endpoints, so new traffic stops going to it.
5. Graceful shutdown
Zoom into node-2. The kubelet sees the deletion and starts shutting the Pod down. At the same moment kube-proxy on every node is removing the Pod from its rules, and those two things race. The preStop hook, a 5-second sleep, gives the rules time to catch up, though nothing checks that they have. Then the kubelet sends SIGTERM, the app finishes its open requests and exits. Had it ignored SIGTERM, SIGKILL would follow when the 30 seconds ran out.
- node-2's kubelet sees the deletion and starts shutting the Pod down.
- First, a 5-second pause, to give every node time to stop sending it traffic. Nothing checks that they did.
- Then the app gets SIGTERM: a polite request to finish up and exit.
- The app finishes its last requests and exits cleanly.
- The Pod's network is torn down and its IP freed.
- The Pod is finally removed from the cluster.
6. Repeat and finish
The same loop runs twice more: surge one new Pod, wait for it to be ready, retire one old Pod. Each round takes about 11 seconds, mostly the readiness delay. When the new ReplicaSet has 3 ready Pods, the old one sits at 0 Pods. It's kept, not deleted, so kubectl rollout undo can scale it back up.
- Round 2: another new Pod becomes ready, another old one goes.
- Round 3: the last old Pod is replaced.
- All 3 Pods run the new version. The old ReplicaSet stays, at 0 Pods, for rollback.
- kubectl rollout status reports success.
Summary
Three rounds of surge, wait, retire, and the number of ready Pods never dropped below 3. The slow part was waiting for readiness, which is the point: the rollout only moves as fast as new Pods can prove they work. The other half of zero downtime is the app's: handle SIGTERM, and leave time for the endpoints to update.
Next lesson: Pod to Pod, same node