Sub topic 7
Ordered startup, shutdown, and parallel management
This chapter assumes you can apply YAML with kubectl, and that you know the web StatefulSet and headless Service from the earlier chapters. It covers how a StatefulSet orders Pod creation and deletion, and how to turn that ordering off when you don't need it.
The default: OrderedReady
By default a StatefulSet uses podManagementPolicy: OrderedReady. The controller follows three rules:
- Pods are created one at a time in ordinal order:
web-0, thenweb-1, thenweb-2. - A Pod is created only after the previous Pod is Running and Ready. Ready means its readiness probe passes, or its containers have started if it has no probe.
- Pods are deleted in reverse order, from the highest ordinal down. The controller waits for each Pod to finish terminating before it deletes the next one.
The same rules apply when you scale. Scaling from 3 to 5 creates web-3 and then web-4. Scaling from 5 to 2 removes web-4, then web-3, then web-2.
This order matters for clustered software. A database may need its first node, web-0, to be healthy before other nodes can join it. On shutdown, removing the newest members first keeps the oldest members available as long as possible.
Watching the order
Apply the manifest and watch the Pods. You will see each Pod reach Running and 1/1 before the next one appears.
kubectl apply -f web.yaml
kubectl get pods -l app=web -wTo see the reverse order, scale down in one terminal while watching in another.
kubectl scale statefulset web --replicas=1web-2 terminates first, then web-1. web-0 stays.
Common error: a stuck rollout
Ordering has a cost. If web-1 never becomes Ready, the controller never creates web-2. The StatefulSet stays at two Pods, and nothing in the Pod list says "waiting" for you.
Check these in order:
kubectl get pods -l app=webshows which ordinal is not Ready.kubectl describe pod web-1shows probe failures, image pull errors, or an unbound PersistentVolumeClaim.kubectl get pvc -l app=webshows whether the claim for that Pod isPending.
Parallel management
Some workloads need stable names and storage but don't need ordering. Examples are sharded caches where every node is independent. Set podManagementPolicy: Parallel to launch and terminate all Pods at once.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: web
replicas: 3
podManagementPolicy: Parallel
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- name: web
containerPort: 80With Parallel, the controller creates web-0, web-1, and web-2 together and does not wait for any of them to be Ready. When scaling down, it terminates the Pods together too. Names, DNS records, and PersistentVolumeClaims behave exactly as before.
| Behavior | OrderedReady | Parallel |
|---|---|---|
| Creation | One at a time, in ordinal order | All at once |
| Waits for Ready before next Pod | Yes | No |
| Deletion | Reverse ordinal order, one at a time | All at once |
| Fits | Clustered software with a bootstrap order | Independent nodes needing stable identity |
Termination details
When you delete a StatefulSet object, Kubernetes makes no ordering promise about Pod termination. To get an orderly shutdown, scale to 0 first, wait for the Pods to disappear, and then delete the StatefulSet.
kubectl scale statefulset web --replicas=0
kubectl get pods -l app=web -w
kubectl delete statefulset webAlso give the container enough time to shut down cleanly. A Pod's terminationGracePeriodSeconds sets how long Kubernetes waits before it kills the container. Under OrderedReady, a long grace period delays every later deletion.
Exercise
A three-replica StatefulSet uses the default policy. web-0 is Ready and web-1 is stuck in CrashLoopBackOff. How many Pods exist, and what changes if you set podManagementPolicy: Parallel on a new StatefulSet with the same image?
Show answer
Two Pods exist, because the controller will not create web-2 until web-1 is Ready. With Parallel on a new StatefulSet, all three Pods are created at once, so web-2 starts anyway. web-1 still crashes, since the policy changes creation order, not the cause of the failure.