Sub topic 9
Scaling and what happens to the volumes
This chapter assumes you can apply YAML with kubectl, and that you know how a StatefulSet creates Pods named web-0, web-1, web-2 and one PersistentVolumeClaim (PVC) per Pod from volumeClaimTemplates. It also assumes you know the ordered lifecycle from the previous chapter. Here you will see what scaling does to Pods and to their volumes, and how to control it.
Scaling follows ordinals
A StatefulSet scales by ordinal. Scaling up adds the next numbers, and scaling down removes the highest numbers first.
kubectl scale statefulset web --replicas=5
kubectl scale statefulset web --replicas=2The first command creates web-3, then web-4. With the default OrderedReady policy, each Pod must be Running and Ready before the next one starts. The second command, run when five replicas exist, removes web-4, then web-3, then web-2. Each Pod must be fully gone before the next one is deleted. Only web-0 and web-1 remain.
Each new Pod also gets a new PVC from the template. Scaling to five creates www-web-3 and www-web-4.
Volumes are kept by default
By default, Kubernetes does not delete PVCs when a Pod is removed. After scaling from five replicas to two, check the claims:
kubectl get pvc -l app=webYou still see www-web-0 through www-web-4. The Pods web-2 to web-4 are gone, but their claims and the data on them remain. This is a safety choice, because deleting data is hard to undo.
If you scale back up to five, the new web-2 Pod attaches to the existing www-web-2 claim. It starts with the data the earlier web-2 wrote. Identity and storage stay paired by ordinal.
The same rule applies when you delete the StatefulSet. The Pods go away and the PVCs stay.
Controlling retention with a policy
The persistentVolumeClaimRetentionPolicy field lets you choose what happens to claims in two situations.
spec:
persistentVolumeClaimRetentionPolicy:
whenDeleted: Retain
whenScaled: Delete| Field | Situation | Values |
|---|---|---|
| `whenDeleted` | The StatefulSet is deleted | `Retain` (default) or `Delete` |
| `whenScaled` | Replicas are reduced | `Retain` (default) or `Delete` |
With the settings above, deleting the StatefulSet keeps all claims, but scaling from five to two deletes www-web-2, www-web-3, and www-web-4. Scaling back up later gives you empty volumes for those ordinals.
Two details matter in practice:
- The policy deletes the PVC object. Whether the underlying PersistentVolume is also deleted depends on its reclaim policy, which comes from the StorageClass or PV. A
Retainreclaim policy keeps the disk even after the claim is gone. - Pod restarts, rescheduling, and rolling updates never delete claims. The policy applies only to scale-down and StatefulSet deletion.
Cleaning up manually
With the default policy, unused claims accumulate and keep costing storage. Delete them yourself once you are sure the data is no longer needed:
kubectl delete pvc www-web-2 www-web-3 www-web-4To remove everything created by the earlier example, delete the StatefulSet and then its claims by label:
kubectl delete statefulset web
kubectl delete pvc -l app=webThe label selector works here because the example's volumeClaimTemplates PVCs receive the labels from the Pod template's selector labels. If you are unsure, run kubectl get pvc --show-labels first.
Common errors
- Scale-down looks stuck. A Pod being deleted must finish terminating, and Pods are removed one at a time under
OrderedReady. A lower-ordinal Pod that is not Ready can also block the scale operation. Checkkubectl get podsand fix the unhealthy Pod first. - Scaled-up Pod has old data. This is expected with
Retain. The Pod reused the earlier claim. If your application cannot handle stale data, usewhenScaled: Deleteor remove the claims before scaling up. - Pod stays Pending after scale-up. The new claim cannot be bound, often because no PV is available or the StorageClass cannot provision one. Run
kubectl describe pvc www-web-3. - Claims remain after deleting the StatefulSet. This is the default. Set
whenDeleted: Deleteor delete the claims yourself.
Exercise
A StatefulSet named db has five replicas and uses whenScaled: Retain. You scale it to three and later scale it back to five. What happens to db-3 and db-4 the second time, and how would the result differ with whenScaled: Delete?
Show answer
With Retain, the claims data-db-3 and data-db-4 survive the scale-down, so the recreated Pods attach to them and see their old data. With Delete, the claims are removed during scale-down (and the PV too if its reclaim policy is Delete), so the recreated Pods get new, empty volumes.
Exercise
Which Pod does a StatefulSet remove first when you reduce replicas from four to two, and in what order?
Show answer
It removes web-3 first, then web-2, highest ordinal first, waiting for each to terminate before deleting the next under the default OrderedReady policy.