Control plane
Draining a node
Cordon, evict, PodDisruptionBudget
node-2 needs a kernel update. Before you reboot it, its Pods have to go somewhere else, without taking web below what it needs to serve. kubectl drain does that in two moves: it cordons the node so nothing new lands there, then evicts each Pod through the Eviction API. Every eviction is checked against web's PodDisruptionBudget, which says two of its three Pods must stay healthy. Two of them are on node-2, so one has to wait.
- Node
- node-2 · 2 of web's 3 Pods
- Budget
- web-pdb · minAvailable 2
- Command
- kubectl drain node-2 --ignore-daemonsets
- Drained in
- ≈ 10 s in this run
Read the whole lesson as text
1. The budget
You give web a PodDisruptionBudget: at least two Pods matching app=web must be healthy, which means Ready. The disruption controller in the controller manager counts them and keeps the budget's status current: three healthy, two required, so one disruption is allowed. The budget only limits voluntary evictions that go through the Eviction API. A node that dies, a Pod you delete directly or an OOM kill doesn't ask it.
- You create web-pdb: keep at least two web Pods healthy.
- The disruption controller counts web's Ready Pods and writes the budget's status.
2. Cordon
You run kubectl drain node-2 --ignore-daemonsets. Its first move is a cordon: it sets spec.unschedulable on the Node. The node lifecycle controller turns that into a taint, node.kubernetes.io/unschedulable:NoSchedule, and the scheduler's NodeUnschedulable filter rules node-2 out for every new Pod. Nothing that is already running is touched. A cordon on its own changes nothing but where new Pods go.
- kubectl drain starts by marking node-2 unschedulable.
- The node lifecycle controller adds the matching taint.
3. What's on the node
kubectl lists every Pod whose spec.nodeName is node-2 and sorts them. The kube-proxy and kube-flannel Pods belong to DaemonSets: evicting them is pointless, because their controller would put them straight back, so --ignore-daemonsets leaves them. Without that flag drain stops. It would also stop for a Pod with no controller, which nothing would recreate, unless you add --force, and for a Pod with an emptyDir volume, whose data would be lost, unless you add --delete-emptydir-data. That leaves web's two Pods.
- kubectl asks for every Pod on node-2, in all namespaces.
- DaemonSet Pods are skipped with a warning. The two web Pods will be evicted.
4. The first eviction
kubectl evicts both web Pods at once, each with a POST to the Pod's eviction subresource. For the first, the API server finds web-pdb and reads one disruption allowed. It lowers that to 0 and writes the Pod's name into the budget's disruptedPods before deleting anything, so a second eviction arriving in the same instant can't spend the same disruption. Then it deletes the Pod with its normal grace period, and node-2's kubelet starts shutting it down.
- kubectl asks the API server to evict the first web Pod.
- The API server takes the one allowed disruption and records the Pod in the budget.
- The API server deletes the Pod. node-2's kubelet sees the deletion and begins the shutdown.
5. 429
The eviction for the second Pod arrives a moment later. The budget now allows 0 disruptions, so the API server refuses with 429 Too Many Requests and deletes nothing. 429 here doesn't mean the API server is overloaded: it means try again later. kubectl prints the error and retries the same eviction every five seconds, for as long as it takes, unless you gave drain a --timeout.
- kubectl asks to evict the second web Pod.
- Refused: evicting it would leave web with fewer healthy Pods than the budget allows.
6. A replacement
Meanwhile the ReplicaSet counts two Pods where it wants three and creates a replacement. The scheduler's NodeUnschedulable filter rules out node-2, so the replacement lands on node-1. While it starts, the first Pod finishes shutting down. Once the replacement is Ready, the disruption controller counts three healthy Pods again and the budget allows one disruption.
- The ReplicaSet controller creates a Pod to get back to three.
- node-2 is cordoned, so the scheduler places the replacement on node-1.
- web exits after SIGTERM, and the kubelet finishes deleting the Pod.
- The replacement passes its readiness probe on node-1.
- The disruption controller counts three healthy Pods, so one disruption is allowed again.
7. The second eviction
kubectl's next retry arrives with one disruption allowed. The API server spends it and deletes the second Pod. Its shutdown is the same as the first's: the EndpointSlice marks it terminating, so the Service stops sending it new connections, the kubelet sends SIGTERM, and the container has up to 30 seconds. The ReplicaSet creates another replacement, which also goes to node-1.
- Five seconds later, kubectl tries the second eviction again.
- This time the budget allows it. The Pod is deleted and its shutdown starts.
- The ReplicaSet makes another replacement, and the scheduler puts it on node-1 too.
- The second replacement is Ready and the second old Pod is gone. web has three Pods, all on node-1.
8. Empty
Every Pod drain was going to evict is gone, so it prints node/node-2 drained and exits. Only the DaemonSet Pods are left, and you can reboot. When node-2 is back, kubectl uncordon removes the mark and the taint. Nothing moves back on its own: node-2 stays empty until new Pods are scheduled. Two traps are worth knowing. A budget whose minAvailable equals the replica count allows no disruption at all, so drain waits forever. And with one replica, any eviction is an outage.
- Both evictions have finished, and kubectl drain exits.
- After the reboot, you make node-2 schedulable again.
- The unschedulable taint comes off, and node-2 takes new Pods again.
Summary
Draining is a cordon and a series of evictions. The cordon only stops new Pods. Each eviction is a request the API server can refuse: it checks the PodDisruptionBudget, spends a disruption before deleting anything, and answers 429 when none is left. kubectl just keeps asking. The pace is set by how fast replacements become Ready elsewhere, and a budget that can never be met keeps the node from ever being empty.
Next lesson: Upgrading the cluster