Sub topic 9
Protecting workloads during node removal
This chapter assumes you know Pods, Deployments, and Nodes, and that you have read how the Cluster Autoscaler removes underused nodes. The same controls also apply to Karpenter consolidation, which a later chapter covers.
Why removal needs guardrails
Both autoscalers remove a node by draining it. Draining cordons the node so nothing new lands on it, then evicts each pod through the Kubernetes Eviction API. The Eviction API honors PodDisruptionBudgets. Deleting a pod directly does not.
A drain is a voluntary disruption. It happens by choice, so you can limit it. Hardware failure and spot reclamation are involuntary, and budgets cannot stop them.
PodDisruptionBudgets
A PodDisruptionBudget (PDB) states how many pods of a group must stay available during voluntary disruptions.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: webWith this budget, the autoscaler can evict a web pod only while at least two others stay ready. You can use maxUnavailable instead of minAvailable. Set only one of the two.
Common PDB mistakes
- Budget equals replica count. With
minAvailable: 3on three replicas, no eviction is ever allowed. The node never drains. maxUnavailable: 0. This has the same effect.- Single replica with
minAvailable: 1. The pod can never be evicted. - Selector matches nothing. The budget does nothing, and you may not notice.
Check the current state with kubectl get pdb. If ALLOWED DISRUPTIONS is 0, the budget blocks drains.
Other eviction controls
| Control | Effect | Applies to |
|---|---|---|
| `cluster-autoscaler.kubernetes.io/safe-to-evict: "false"` | Pod blocks removal of its node | Cluster Autoscaler |
| `karpenter.sh/do-not-disrupt: "true"` on a pod | Karpenter will not voluntarily disrupt the node | Karpenter |
| `karpenter.sh/do-not-disrupt: "true"` on a node | Same, for that node | Karpenter |
| `terminationGracePeriodSeconds` | Time a pod gets to shut down after SIGTERM | Both |
Set an annotation on the pod template, not on the Deployment object, so the pods receive it:
spec:
template:
metadata:
annotations:
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"The Cluster Autoscaler also refuses to remove nodes that hold certain pods by default:
- Pods not managed by a controller, such as bare pods.
- Pods using local storage such as
emptyDirorhostPath. - Pods that a PDB or scheduling rules prevent from moving.
- Pods in
kube-systemwithout a PDB. The flag--skip-nodes-with-system-pods=falsechanges this.
Marking a pod safe-to-evict: "true" overrides the local-storage and bare-pod blocks.
Graceful shutdown
Eviction sends SIGTERM and waits up to terminationGracePeriodSeconds, which defaults to 30. If your app needs longer to finish requests, raise it. Pair it with a preStop hook or SIGTERM handling so the app stops accepting new work first.
Exercise
A Deployment has 3 replicas and a PDB with minAvailable: 3. The Cluster Autoscaler never removes the node that runs one of these pods, even though the node is nearly empty. Why, and how do you fix it?
Show answer
The budget requires all 3 pods to stay available, so ALLOWED DISRUPTIONS is 0 and every eviction is refused. Lower it to minAvailable: 2 (or use maxUnavailable: 1) so one pod can be evicted at a time. Confirm with kubectl get pdb.