Difficulty: Advanced
How do you safely take a Kubernetes node out of service for maintenance (for example an OS patch or upgrade) without disrupting applications?
Cordon the node so nothing new is scheduled on it, drain it so existing Pods are evicted gracefully and recreated on other nodes, do the maintenance, then uncordon it. kubectl drain cordons automatically and uses the Eviction API, which respects PodDisruptionBudgets, so applications with enough replicas and a sensible PDB stay available throughout.
kubectl top nodes, or rely on the cluster autoscaler).kubectl cordon <node> marks it SchedulingDisabled (it sets spec.unschedulable and adds the node.kubernetes.io/unschedulable:NoSchedule taint). Running Pods are untouched.kubectl drain <node> --ignore-daemonsets --delete-emptydir-data. Each Pod is evicted, honouring its terminationGracePeriodSeconds, and its controller creates a replacement elsewhere.kubectl uncordon <node> so it accepts Pods again. Existing Pods do not move back automatically; new scheduling and rollouts fill it over time.| Flag | Why |
|---|---|
--ignore-daemonsets | DaemonSet Pods would just be recreated on the same node, so drain refuses without this |
--delete-emptydir-data | Allows evicting Pods using emptyDir; that scratch data is lost (replaced the old --delete-local-data) |
--force | Needed for bare Pods with no controller; they will not come back |
--grace-period=<s> | Override each Pod's termination grace period |
--timeout=<duration> | Give up after this long instead of waiting forever |
--pod-selector | Drain only matching Pods |
A PodDisruptionBudget (PDB) like minAvailable: 2 or maxUnavailable: 1 tells the Eviction API how many Pods of an app may be voluntarily disrupted at once. If evicting a Pod would break the budget, the API returns HTTP 429 and kubectl drain keeps retrying until replacements elsewhere become Ready.
This is what keeps a 3-replica checkout service up while nodes are patched one by one. It also causes the most common drain problem: a PDB with minAvailable equal to the replica count (or a single replica with maxUnavailable: 0) blocks the drain forever. Fix the PDB or scale up first.
Managed platforms run this exact sequence for you: EKS managed node groups, GKE surge upgrades and AKS node image upgrades all cordon and drain nodes while respecting PDBs. Karpenter and the cluster autoscaler also drain nodes before removing them. Your job is to make workloads drain-friendly: run at least 2 replicas, define PDBs, handle SIGTERM gracefully, avoid bare Pods, and spread replicas across nodes and zones.
NODE=ip-10-0-2-41.ap-south-1.compute.internal
kubectl cordon $NODE
kubectl drain $NODE \
--ignore-daemonsets \
--delete-emptydir-data \
--timeout=10m
# ...apply OS patches, reboot...
kubectl get node $NODE # wait for Ready
kubectl uncordon $NODE
drain cordons on its own, but cordoning first makes the intent explicit and lets you check the node before evicting anything.
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: checkout-pdb
spec:
maxUnavailable: 1 # with 3 replicas, at least 2 stay up
selector:
matchLabels:
app: checkout
During a drain, only one checkout Pod can be evicted at a time; the next waits until the replacement is Ready.
kubectl cordon, kubectl drain, Eviction API, PodDisruptionBudget, kubectl uncordon