Node Maintenance

Difficulty: Advanced

Question

How do you safely take a Kubernetes node out of service for maintenance (for example an OS patch or upgrade) without disrupting applications?

Answer

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.

The workflow

  1. Check capacity: make sure the remaining nodes can absorb the Pods (kubectl top nodes, or rely on the cluster autoscaler).
  2. Cordon: kubectl cordon <node> marks it SchedulingDisabled (it sets spec.unschedulable and adds the node.kubernetes.io/unschedulable:NoSchedule taint). Running Pods are untouched.
  3. Drain: kubectl drain <node> --ignore-daemonsets --delete-emptydir-data. Each Pod is evicted, honouring its terminationGracePeriodSeconds, and its controller creates a replacement elsewhere.
  4. Maintain: patch, reboot, upgrade the kubelet, replace hardware.
  5. Uncordon: kubectl uncordon <node> so it accepts Pods again. Existing Pods do not move back automatically; new scheduling and rollouts fill it over time.

Flags you will need

FlagWhy
--ignore-daemonsetsDaemonSet Pods would just be recreated on the same node, so drain refuses without this
--delete-emptydir-dataAllows evicting Pods using emptyDir; that scratch data is lost (replaced the old --delete-local-data)
--forceNeeded 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-selectorDrain only matching Pods

How PodDisruptionBudgets interact

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.

Maintenance at scale

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.

Safe node maintenance. Cordon stops new Pods, drain moves existing ones, uncordon returns the node to service.
  1. Cordon the node: Marked SchedulingDisabled, running Pods stay.
  2. Drain the node: Pods are evicted via the Eviction API.
  3. PDBs gate each eviction: Blocked evictions retry until replacements are Ready.
  4. Perform maintenance: Patch, reboot or upgrade the kubelet.
  5. Uncordon the node: The scheduler can place new Pods on it again.

Code examples

Draining and restoring a node

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.

PodDisruptionBudget that keeps checkout available

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.

Key points

  • Sequence: cordon, drain, maintain, uncordon.
  • cordon blocks new Pods only; drain evicts existing ones.
  • drain uses the Eviction API, which respects PodDisruptionBudgets.
  • --ignore-daemonsets and --delete-emptydir-data are almost always needed.
  • A PDB that allows zero disruptions blocks drains forever.

Common follow-up questions

  • What is the difference between cordon and drain?
  • Why might kubectl drain hang, and how do you fix it?
  • How do PodDisruptionBudgets differ from involuntary disruptions like node crashes?
  • How would you upgrade every node in a 50-node cluster with no downtime?

Interview tips

  • Give the full four-step sequence including uncordon; many candidates forget it.
  • Bring up PodDisruptionBudgets and the Eviction API; that is the real safety mechanism.
  • Mention what makes a workload drain-friendly (replicas, PDBs, graceful SIGTERM).

Concepts covered

kubectl cordon, kubectl drain, Eviction API, PodDisruptionBudget, kubectl uncordon

More Kubernetes & Orchestration questions