Skip to content

Graceful node drain — the runbook for taking a node out of service safely

Step-by-step procedure for cordoning, draining, and replacing a Kubernetes node — handling PDB stalls, DaemonSets, local-volume pods, and the "drain hung after 10 minutes" case.

Taking a Kubernetes node out of service sounds like a one-liner — kubectl drain node-3 — and on a healthy cluster with well-tuned workloads that is exactly what it is. The reality on most clusters we inherit is that the first attempt stalls on a pod with emptyDir, the second attempt fails because three DaemonSets weren’t excluded, and the third attempt hangs for ten minutes on a PodDisruptionBudget that nobody documented. This article is the runbook we follow for a planned node drain, with the decision points called out where the safe choice differs from the fast one.

How to verify

Before touching anything, snapshot the node’s current state. If something goes wrong, this is what you compare against:

kubectl get node node-3 -o wide
kubectl get pods -A --field-selector spec.nodeName=node-3 -o wide
kubectl get pdb -A
kubectl top node node-3
kubectl describe node node-3 | grep -E 'Taints|Conditions|Allocatable|Allocated'

Capture the pod-on-node list to a file — you will reference it after the drain to confirm everything rescheduled. A simple count check is enough:

kubectl get pods -A --field-selector spec.nodeName=node-3 \
  -o jsonpath='{range .items[*]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}' \
  | tee /tmp/pre-drain-node-3.txt

What’s happening

kubectl drain does three things in sequence. First it cordons the node (sets spec.unschedulable: true) so the scheduler will not place new pods there. Second it enumerates every pod on the node and filters: DaemonSet pods are skipped by default, mirror pods are skipped, pods with emptyDir volumes require an explicit flag, and pods without a controller (bare pods) require another explicit flag. Third it submits eviction requests for each remaining pod, one at a time, respecting PodDisruptionBudgets.

The eviction is what trips most drains. An eviction request goes to the API server, which checks the PDB for that pod’s workload. If allowing the eviction would push the PDB below its threshold, the API returns a 429 and kubectl drain retries. Retry forever, by default — there is no built-in stall detection. Hence the --timeout flag.

The other failure mode is the pod that has no good rescheduling target. If your cluster is at 95% capacity and a 4-replica Deployment has anti-affinity rules requiring one pod per node, draining a node means the evicted pod has nowhere to go. The pod evicts (or doesn’t, depending on PDB), the Deployment controller schedules a replacement, and the replacement sits in Pending because no node satisfies the constraints. From the drain’s perspective, the eviction succeeded; from the workload’s perspective, you just dropped a replica.

The third trap is emptyDir. Pods using emptyDir lose their data on eviction — the volume is node-local and cannot follow the pod to a new host. kubectl drain refuses to evict these unless you pass --delete-emptydir-data, which makes the data loss explicit. For most stateless workloads this is fine (the emptyDir is a scratch directory). For anything with state, it is not.

The fourth trap is local-storage pods using local-path-provisioner or HostPath volumes. These are pinned to the node. Eviction does not migrate the data; the rescheduled pod fails to mount and sits in Pending indefinitely.

The procedure

  1. Cordon first, then verify scheduling has stopped. Cordoning is reversible and zero-risk; do it as a separate step so you can confirm before evicting:

    kubectl cordon node-3
    kubectl get node node-3 | grep SchedulingDisabled
  2. Inventory the pods on the node by class. You need to know what flags the drain will require:

    # DaemonSet pods (will be skipped with --ignore-daemonsets)
    kubectl get pods -A --field-selector spec.nodeName=node-3 \
      -o jsonpath='{range .items[?(@.metadata.ownerReferences[0].kind=="DaemonSet")]}{.metadata.namespace}/{.metadata.name}{"\n"}{end}'
    # Pods with emptyDir volumes
    kubectl get pods -A --field-selector spec.nodeName=node-3 \
      -o json | jq -r '.items[] | select(.spec.volumes[]?.emptyDir) | "\(.metadata.namespace)/\(.metadata.name)"'
    # Bare pods (no controller)
    kubectl get pods -A --field-selector spec.nodeName=node-3 \
      -o json | jq -r '.items[] | select(.metadata.ownerReferences == null or .metadata.ownerReferences == []) | "\(.metadata.namespace)/\(.metadata.name)"'

    Bare pods should never exist in production — if you find one, document it and discuss with the workload owner whether --force is acceptable (which deletes the pod outright; it does not migrate).

  3. Check PDBs against the workloads you are about to evict. For each Deployment on the node, confirm the PDB disruptionsAllowed is non-zero:

    kubectl get pdb -A -o custom-columns=\
    NS:.metadata.namespace,NAME:.metadata.name,MAXUNAVAIL:.spec.maxUnavailable,MINAVAIL:.spec.minAvailable,ALLOWED:.status.disruptionsAllowed

    Any PDB with ALLOWED: 0 is a drain blocker. Either wait for the budget to recover (a previous eviction is in flight) or coordinate with the workload owner to temporarily relax the budget.

  4. Run the drain with appropriate flags. The flag set we use for the standard case:

    kubectl drain node-3 \
      --ignore-daemonsets \
      --delete-emptydir-data \
      --timeout=15m \
      --grace-period=60

    --ignore-daemonsets is almost always required — every cluster has DaemonSet workloads (CNI, kube-proxy, log shippers) that should not be evicted. --delete-emptydir-data is required only if step 2 found emptyDir pods; do not add it by default. --timeout=15m gives the drain a wall clock; without it, a stalled PDB hangs forever. --grace-period=60 overrides per-pod terminationGracePeriodSeconds if you want a hard cap.

  5. Watch the drain progress in a second terminal. The drain prints one line per pod evicted; this is your trace:

    watch kubectl get pods -A --field-selector spec.nodeName=node-3

    A healthy drain steadily decreases the pod count. A stalled drain stays at the same count for minutes — switch to the drain terminal and read the error.

  6. If the drain stalls. The output will name the pod that cannot evict. Check the PDB:

    kubectl describe pod <stuck-pod> -n <ns> | grep -A2 'Events'
    kubectl get pdb -n <ns>

    If the PDB is at zero allowed disruptions, you have three options: wait, relax the PDB temporarily (kubectl patch pdb foo-pdb -n team --type=json -p='[{"op":"replace","path":"/spec/maxUnavailable","value":2}]), or force-delete the pod (kubectl delete pod --force --grace-period=0). Force-delete is the last resort — it bypasses the PDB and immediately removes the pod without graceful shutdown.

  7. Confirm all workloads rescheduled. Compare against the pre-drain inventory:

    kubectl get pods -A -o wide | grep -vE 'node-3|Completed' > /tmp/post-drain-pods.txt

    Every namespaced/named pod from pre-drain-node-3.txt should appear on a different node in post-drain-pods.txt. A missing pod means a Pending pod somewhere — investigate before assuming the drain succeeded.

  8. Perform the maintenance, then uncordon.

    # ...do your patch / kernel update / kubelet upgrade...
    kubectl uncordon node-3

    The Cluster Autoscaler and scheduler will start placing pods back on the node within minutes. Watch for unexpected pod movement that signals a topology constraint is no longer satisfied.

Common pitfalls

  • A drain that prints “evicting pod xxx” and stops printing is almost always blocked on a PDB. Always run kubectl get pdb -A first when diagnosing.
  • --force deletes pods immediately without going through eviction. PDBs do not apply to --force deletes — useful in emergencies, dangerous if used routinely.
  • A node with a local-path-provisioner PVC cannot be drained safely without manual data migration. The drain will succeed (the pod evicts) but the workload will sit Pending forever because the PVC is pinned to the now-cordoned node. Plan the migration before the drain.
  • StatefulSet pods with stable network identity (pod-0, pod-1) will be rescheduled with the same name to a new node. Some applications (Patroni, Cassandra) treat this as a member rejoin; others (legacy stateful apps) may treat it as a new member and split-brain.
  • The --disable-eviction flag bypasses the PDB system entirely and uses direct DELETE. It is faster but defeats the purpose of having PDBs. Use only in emergencies you can defend.
  • A drain in progress holds API watches open. Running multiple drains in parallel against a small cluster can saturate the API server connection limit. Drain one node at a time.

Stack Harbor operates clusters where node maintenance is a scheduled pipeline run — drain, patch, uncordon, verify — with the PDB-stall path handled and the local-storage migration done in advance. If your last patch night involved three engineers in a call for four hours coaxing drains through, the drain runbook above is the first deliverable of clustered environments management.