KubernetesXXXIII · Cordon, Drain and UncordonCordon, drain, uncordon
Drain automation — Cordoning, draining, and replacing nodes at scale
What you'll learn
- Identify the cluster's drain automation mechanisms
- Configure the cluster-autoscaler for drain
- Apply the operational patterns for managing drain at scale
- Diagnose a drain automation that is failing
Prerequisites
Verified against Kubernetes 1.34.x · kubeadm 1.34.x · kubectl 1.34.x · etcd 3.6.x · CoreDNS 1.11.x · containerd 1.7.x / 2.x · 2026-08-16
The drain is the cluster’s standard tool for node maintenance. At scale, the drain is performed by the cluster-autoscaler, the node lifecycle controller, and the cluster’s maintenance automation. This lesson walks the cluster’s drain automation, the operational patterns, and the failure modes.
The cluster-autoscaler
flowchart LR
A[Cluster autoscaler] --> B{Watch nodes and Pods}
B -->|Underutilized node| C[Cordon and drain]
B -->|Unschedulable Pods| D[Add nodes]
C --> E[Remove nodes]
D --> F[Add capacity]
E --> G[Scale down]
F --> H[Scale up]
The cluster-autoscaler is the cluster’s primary drain
The cluster-autoscaler is the cluster’s primary drain automation. The autoscaler:
- Identifies underutilized nodes. The autoscaler watches the cluster’s nodes and Pods.
- Drains the underutilized nodes. The autoscaler cordons the node and evicts the Pods.
- Removes the underutilized nodes. The autoscaler calls the cloud provider’s API to remove the node.
- Adds nodes when capacity is needed. The autoscaler calls the cloud provider’s API to add nodes.
The autoscaler’s drain is the cluster’s automatic maintenance. The autoscaler is the cluster’s primary tool for managing the node lifecycle.
The node lifecycle controller
The node lifecycle controller is the cluster’s controller for managing the node lifecycle. The controller:
- Monitors the node’s status. The controller
watches the node’s
Readycondition. - Cordons the failing node. The controller
cordons the node when the
Readycondition isFalse. - Drains the failing node. The controller evicts the Pods on the node.
- Removes the failing node. The controller removes the node from the cluster.
The node lifecycle controller is the cluster’s automatic maintenance. The controller is the cluster’s primary tool for managing the node’s failure.
The cluster’s maintenance automation
The cluster’s maintenance automation is the cluster’s operator-defined automation. The automation:
- Identifies the maintenance window. The automation reads the maintenance window.
- Cordons the nodes. The automation cordons the nodes in the maintenance window.
- Drains the nodes. The automation drains the nodes.
- Performs the maintenance. The automation performs the maintenance.
- Uncordons the nodes. The automation uncordons the nodes.
The cluster’s maintenance automation is the cluster’s scheduled maintenance. The automation is the cluster’s primary tool for managing the maintenance.
The drain’s flags in automation
The drain’s flags in automation:
| Flag | Effect | Use case |
|---|---|---|
--ignore-daemonsets | Continue even if DaemonSet Pods cannot be evicted | Production drain |
--delete-emptydir-data | Delete the Pod’s emptyDir volumes | Cache eviction |
--force | Force the eviction of Pods that are not managed by a controller | Bare Pod eviction |
--grace-period | Override the Pod’s termination grace period | Fast drain |
The flags are the operator’s tuning for the drain. The production rule is to use the flags deliberately.
The drain’s timeout
The drain’s timeout is the time the drain waits for the
Pods to be evicted. The default is the Pod’s
terminationGracePeriodSeconds (default 30s).
The drain’s timeout is configurable:
# Substitute your own value before running:
NODE=node-19
kubectl drain "$NODE" --grace-period=60
The drain’s timeout is the operator’s tuning for the drain. The production rule is to set the timeout for the workload’s shutdown time.
The drain’s parallelism
The drain’s parallelism is the number of Pods that the drain evicts in parallel. The default is 1.
The drain’s parallelism is configurable:
# Substitute your own value before running:
NODE=node-19
kubectl drain "$NODE" --parallel=4
The drain’s parallelism is the operator’s tuning for the drain. The production rule is to set the parallelism for the cluster’s capacity.
The drain’s batch size
The drain’s batch size is the number of Pods that the drain evicts in a single batch. The default is 1.
The drain’s batch size is the operator’s tuning for the drain. The production rule is to set the batch size for the cluster’s capacity.
The drain’s error handling
The drain’s error handling is the cluster’s response to the drain’s failure. The error handling:
- Retry the eviction. The drain retries the eviction with exponential backoff.
- Skip the Pod. The drain skips the Pod if the Pod’s PDB rejects the eviction.
- Fail the drain. The drain fails if the Pod’s PDB rejects the eviction.
The drain’s error handling is the cluster’s protection against the drain’s failure. The production rule is to handle the drain’s errors deliberately.
The drain’s metrics
The drain’s metrics:
cluster_autoscaler_drain_duration_seconds— the time the drain takes to complete.cluster_autoscaler_evictions_total— the number of Pods evicted.cluster_autoscaler_failed_drains_total— the number of failed drains.
The metrics are the cluster’s view of the drain. The operator should monitor the metrics and alert on the drain’s failure.
The drain’s operational patterns
The drain’s operational patterns:
- Use the cluster-autoscaler. The autoscaler is the cluster’s primary tool for managing the drain at scale.
- Use the node lifecycle controller. The controller is the cluster’s primary tool for managing the node’s failure.
- Use the cluster’s maintenance automation. The automation is the cluster’s primary tool for managing the maintenance.
- Tune the drain’s flags. The flags are the operator’s tuning for the drain.
- Monitor the drain’s metrics. The metrics are the cluster’s view of the drain.
- Test the drain in non-production. A staging cluster that mirrors production is the right place to test the drain.
The drain’s failure modes
The drain’s failure modes:
| Failure | Symptom | Root cause |
|---|---|---|
| Autoscaler fails | Node is not removed | API server unreachable, cloud provider error |
| Controller fails | Node is not drained | Controller crashed, configuration error |
| Drain hangs | Pod is stuck in Terminating | Pod has a long shutdown |
| Drain rejects | Drain fails | PDB, DaemonSet, emptyDir |
The diagnostic:
# Pod suffix from `kubectl get pods -n kube-system -l app=cluster-autoscaler`:
POD_SUFFIX=7d9f8b6c5d-x2k9v
kubectl logs -n kube-system "cluster-autoscaler-$POD_SUFFIX"
The autoscaler’s logs show the drain’s failure. The fix is to investigate the autoscaler’s logs and the cloud provider’s API.
Quiz
Knowledge check · 4 questions
Q1. What is the main risk when automation drains several nodes at once?
Q2. Automation that respects PodDisruptionBudgets on every node cannot cause a workload outage.
Q3. Work out why an autoscaler that adds nodes correctly never removes any.
The cluster has grown from 40 to 71 nodes over six weeks and has never scaled down, despite average CPU utilisation of 22%. `kubectl logs -n kube-system deploy/cluster-autoscaler | grep -i 'cannot be removed'` returns, repeatedly: `node-19 cannot be removed: pod kube-system/coredns-9f8c7-mq2rk is not replicated and is not safe to evict`, `node-24 cannot be removed: pod monitoring/pushgateway-0 has local storage and cannot be evicted`. The autoscaler runs with default flags and no PDBs exist in `kube-system` or `monitoring`.
Q4. Which Pod annotation tells the cluster autoscaler a Pod may be moved during scale-down, and which two autoscaler defaults most often prevent a node from being removed?
Passing score: 75%. Answers are checked in this browser.
Production discipline
- Use the cluster-autoscaler. The autoscaler is the cluster’s primary tool for managing the drain at scale.
- Use the node lifecycle controller. The controller is the cluster’s primary tool for managing the node’s failure.
- Use the cluster’s maintenance automation. The automation is the cluster’s primary tool for managing the maintenance.
- Tune the drain’s flags. The flags are the operator’s tuning for the drain.
- Monitor the drain’s metrics. The metrics are the cluster’s view of the drain.
- Audit the drain at every release. The drain’s configuration should be version-controlled; the audit catches the failures.
- Test the drain in non-production. A staging cluster that mirrors production is the right place to test the drain.