kubectl cordon & drain: Command Reference for CI/CD
Stop scheduling on a node, then safely evict its pods.
kubectl cordon marks a node unschedulable; drain cordons and then evicts pods honouring PodDisruptionBudgets; uncordon reverses cordon. Together they bracket node maintenance in automation. This reference covers the flags that keep a drain from hanging.
Common flags and usage
- cordon <node>: stop new pods landing (running pods stay)
- drain <node>: cordon then evict pods via the eviction API
- --ignore-daemonsets: required; DaemonSet pods are not evicted
- --delete-emptydir-data: allow evicting pods with emptyDir data
- --timeout=5m: cap the drain so a PDB block fails the step
- uncordon <node>: make the node schedulable again
Example
shell
kubectl cordon ip-10-0-1-5
kubectl drain ip-10-0-1-5 --ignore-daemonsets \
--delete-emptydir-data --timeout=5m
# ... node maintenance ...
kubectl uncordon ip-10-0-1-5In CI
A drain blocks on a PodDisruptionBudget ("Cannot evict pod ... violates the disruption budget") until another replica is Ready, so pass --timeout to fail instead of hanging. Always uncordon in a trap/finally block; a forgotten uncordon leaves the node piling up Pending pods.
Key takeaways
- drain needs --ignore-daemonsets and usually --delete-emptydir-data.
- A PDB can block a drain; cap it with --timeout so CI fails fast.
- Always uncordon in a trap; a forgotten cordon strands new pods.
Related guides
kubectl Cheat Sheet: Commands for Everyday KubernetesA kubectl cheat sheet - get, describe, logs, apply, rollout, debug, and context commands for daily Kubernetes…
kubectl get: Command Reference for CI/CDReference for kubectl get: list resources, output formats, label and field selectors, jsonpath extraction, an…
kubectl describe: Command Reference for CI/CDReference for kubectl describe: render a resource plus its Events to debug ImagePullBackOff, CrashLoopBackOff…
kubectl delete: Command Reference for CI/CDReference for kubectl delete: remove resources by name, label, or manifest, graceful vs force deletion, --wai…