Sub topic 8
Rolling updates, partitions, and OnDelete
This chapter assumes you can apply manifests with kubectl, and that you have a running three-replica StatefulSet named web like the one from the earlier chapters. You should also know that Pods are named web-0, web-1 and web-2, and that each keeps its own PersistentVolumeClaim.
How a StatefulSet applies a template change
When you change the Pod template of a StatefulSet, for example the container image, the controller must replace the existing Pods. The updateStrategy field controls how. There are two types:
| Type | Behavior |
|---|---|
| `RollingUpdate` | The controller replaces Pods automatically. This is the default. |
| `OnDelete` | The controller replaces a Pod only after you delete it by hand. |
With RollingUpdate, the controller works in reverse ordinal order. It deletes web-2, recreates it from the new template, waits until it is Running and Ready, and only then moves to web-1, and finally web-0. Each replacement keeps the Pod's name and reattaches its existing PersistentVolumeClaim, so the data survives.
Staging a rollout with partition
The rollingUpdate.partition field limits the update to Pods whose ordinal is greater than or equal to the partition value. Pods with a lower ordinal stay on the old revision, even if they are deleted and recreated. The default is 0, which updates everything.
This gives you a canary. Set the partition to 2, change the image, and only web-2 is updated.
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":2}}}}'
kubectl set image statefulset/web nginx=nginx:1.27
kubectl rollout status statefulset/webCheck which image each Pod runs:
kubectl get pods -l app=web -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[0].imageExpected result: web-2 shows nginx:1.27, while web-0 and web-1 still show nginx:1.25. Note that kubectl rollout status can report that it is waiting for the partitioned Pods rather than finishing. That is expected while the partition holds them back.
If web-2 looks healthy, lower the partition step by step.
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":1}}}}'
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":0}}}}'At partition 1, web-1 is updated. At 0, web-0 follows and the rollout is complete.
Taking manual control with OnDelete
With updateStrategy.type: OnDelete, changing the template updates nothing by itself. The controller creates the new revision only when a Pod is deleted and recreated.
spec:
updateStrategy:
type: OnDeleteYou then choose the order and timing yourself, which suits systems where you must demote a primary or wait for replication to catch up before restarting a member.
kubectl delete pod web-2
kubectl get pods -l app=web -wThe recreated web-2 uses the new template. The other Pods keep running the old one until you delete them.
Common errors
- A rollout that never finishes. If a new Pod never becomes Ready, for instance because of a bad image tag, the controller waits and does not touch the lower ordinals. Unlike a Deployment, it does not roll back by itself. Fix the template, then delete the stuck Pod if it does not recover. Older Kubernetes versions required deleting the broken Pod manually.
- Forgetting the partition. If an update seems to stop partway, check
kubectl get statefulset web -o jsonpath='{.spec.updateStrategy.rollingUpdate.partition}'. - Expecting
OnDeleteto update automatically. Changing the template underOnDeleteshows no change until you delete Pods. - Rollback by template only. Reverting the template does not repair data. If the new version migrated its on-disk format, the old version may not read it.
Exercise
A StatefulSet named web has 5 replicas and a partition of 3. You change the container image. Which Pods are updated, and in what order? What do you do to finish the rollout?
Show answer
Only web-4 and web-3 are updated, in that order (highest ordinal first, each waiting for Ready). web-0, web-1 and web-2 stay on the old image. To finish, lower the partition to 0 (optionally step by step), and the controller updates web-2, web-1 and web-0 in turn.
Exercise
Why might you pick OnDelete over RollingUpdate for a database cluster?
Show answer
OnDelete lets you control which member restarts and when. You can, for example, update the replicas first, wait for replication to catch up, and restart the primary last, which the automatic reverse-ordinal order cannot guarantee.