Sub topic 13
How drains and evictions use the budget
This chapter assumes you know Pods, Deployments, labels and selectors, and basic kubectl use. You should also have created a PodDisruptionBudget (PDB) like the one in the previous chapter.
Evictions are not deletions
A PDB does not stop anyone from deleting a pod. kubectl delete pod ignores it. The budget applies only to the Eviction API, which is a subresource (pods/eviction) that a client calls to ask the API server to remove a pod politely.
When the API server receives an eviction request, it checks every PDB whose selector matches the pod:
- If the budget allows another disruption, the server deletes the pod and lowers the allowed count.
- If it does not, the server rejects the request with HTTP 429 (Too Many Requests).
Tools such as kubectl drain, the cluster autoscaler, and node upgrade controllers use this API. They are the main source of voluntary disruptions.
What kubectl drain does
A drain first cordons the node, which marks it unschedulable. Then it evicts each pod on the node through the Eviction API.
kubectl get pdb web-pdb
kubectl get pdb web-pdb -o jsonpath='{.status.disruptionsAllowed}{"\n"}'
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-dataThree flags matter here:
--ignore-daemonsetsskips DaemonSet pods, which would be recreated on the node at once.--delete-emptydir-dataallows evicting pods that useemptyDirvolumes, whose data is lost.--timeoutmakes the drain give up after a set time. Without it, the drain waits indefinitely.
When a request is rejected, kubectl drain prints a message such as Cannot evict pod as it would violate the pod's disruption budget and retries about every five seconds. The drain finishes only when the controller has created a replacement pod elsewhere and it has become Ready. That raises the allowed count again.
A worked example
Take the web Deployment with 6 replicas and maxUnavailable: 1. Suppose three pods run on node A.
- Drain starts. The first eviction succeeds, because
disruptionsAllowedis 1. It drops to 0. - The second eviction is rejected with 429, and the drain retries.
- The ReplicaSet creates a replacement pod. It is scheduled on another node, subject to your spread constraints.
- When the replacement becomes Ready,
disruptionsAllowedreturns to 1. The next retry succeeds.
The drain therefore moves the pods one at a time, and the workload never has more than one pod unavailable because of it.
Things the budget does not cover
- Direct deletes, such as
kubectl delete pod, bypass it. - Involuntary disruptions, such as a node crash, are not blocked. They still count against the budget, so a crash can leave no room for a drain.
kubectl drain --disable-evictiondeletes pods directly and skips the budget. Use it only in emergencies.
Exercise
A Deployment has 3 replicas and a PDB with minAvailable: 3. What happens when you run kubectl drain on a node that hosts one of its pods, and why?
Show answer
The drain blocks. With 3 replicas and minAvailable: 3, disruptionsAllowed is 0, so the API server rejects every eviction with 429. The drain keeps retrying until you relax the budget, add replicas, or the drain times out.