Sub topic 10
Preemption caveats: grace periods, PodDisruptionBudgets, and affinity
This chapter assumes you know how a PriorityClass lets the scheduler preempt lower-priority Pods. It covers three behaviors that make preemption slower or less predictable than "evict the victims and place the Pod."
Victims get a grace period
Preemption does not kill victims instantly. The scheduler deletes them, and each victim then follows its normal termination sequence. That sequence sends SIGTERM and waits up to terminationGracePeriodSeconds (30 seconds by default) before sending SIGKILL.
The high-priority Pod stays Pending for that whole time. The scheduler records the chosen node in status.nominatedNodeName, which you can read with:
kubectl get pod important-app -o jsonpath='{.status.nominatedNodeName}{"\n"}'Long grace periods on low-priority workloads therefore delay the Pod that preempted them. Keep them short for Pods that are likely victims.
PodDisruptionBudgets are best effort
A PodDisruptionBudget (PDB) limits how many Pods of an application can be voluntarily disrupted at once.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: batch-pdb
spec:
minAvailable: 2
selector:
matchLabels:
app: batchWhen the scheduler picks victims, it prefers nodes where preemption does not violate a PDB. If no such node exists, it preempts anyway. The PDB does not protect a Pod from a higher-priority Pod that cannot otherwise be scheduled.
Do not use a PDB as a guarantee against preemption. To keep important Pods safe, give them a high priority.
Inter-Pod affinity can block preemption
Suppose the pending Pod has required inter-Pod affinity to a lower-priority Pod on a node. Evicting that Pod would break the affinity rule, so the scheduler does not preempt on that node and looks elsewhere.
The reverse also matters. Required anti-affinity with Pods on other nodes does not help, because Kubernetes does not support cross-node preemption. The scheduler considers one node at a time. It only evicts Pods on that node, so it cannot free a topology domain that spans several nodes.
The practical rule is to make affinity point at Pods of equal or higher priority.
When preemption does not help
Preemption only removes Pods. It cannot fix a Pod that is unschedulable for another reason, such as:
- A
nodeSelectoror node affinity that matches no node. - A taint the Pod does not tolerate.
- A request larger than any node's allocatable capacity.
Check the Pod's events with kubectl describe pod. A FailedScheduling message that names these causes means preemption will not run.
Exercise
A Pod with a high PriorityClass has been Pending for 40 seconds. Its nominatedNodeName is set, and low-priority Pods on that node are shown as Terminating. What is the most likely explanation, and what would you change to shorten the wait?
Show answer
The victims are still in their graceful termination period, and the high-priority Pod cannot bind until they exit. Lower terminationGracePeriodSeconds on the low-priority workloads, or make sure they handle SIGTERM and exit promptly.