The kubectl drain
command in Kubernetes is used to gracefully evacuate a node from the cluster, preparing it for maintenance or other purposes. When a node is drained, Kubernetes attempts to gracefully evict all the pods running on the node onto other nodes in the cluster, ensuring minimal disruption to applications.
Here are several examples illustrating the usage of kubectl drain
:
Example 1: Drain a specific node named node-1
:
kubectl drain node-1
This command will trigger the eviction of all pods from node-1
and mark it unschedulable.
Example 2: Drain a node with additional flags for force draining and ignoring local pods:
kubectl drain node-2 --force --ignore-daemonsets
Adding --force
ensures immediate eviction of pods that are not terminating, while --ignore-daemonsets
ignores pods managed by DaemonSets.
Example 3: Drain a node with a specified timeout (in seconds):
kubectl drain node-3 --timeout=3600s
This specifies a timeout of 3600 seconds (1 hour) for the eviction of pods from node-3
.
Example 4: Drain a node and skip waiting for graceful termination of pods:
kubectl drain node-4 --force --delete-local-data
Using --delete-local-data
ensures deletion of local data on the node and forces immediate eviction of pods.
Example 5: Drain a node, allowing pods to be rescheduled on other nodes:
kubectl drain node-5 --ignore-daemonsets --delete-emptydir-data
This command ignores DaemonSet-managed pods and deletes emptyDir volumes from pods being evicted.
Example 6: Drain a node and prevent new pods from being scheduled on it:
kubectl drain node-6 --ignore-daemonsets --disable-eviction
Adding --disable-eviction
prevents the node from being evicted, useful when the node cannot be safely drained.
Example 7: Drain a node with graceful period adjustment for pod termination:
kubectl drain node-7 --grace-period=120
Adjusting the --grace-period
to 120 seconds allows pods more time to gracefully terminate.
Example 8: Drain a node and reschedule evicted pods onto other nodes:
kubectl drain node-8 --ignore-daemonsets --pod-selector=app=nginx
Using --pod-selector
ensures only pods matching the specified label selector (app=nginx) are evicted and rescheduled.
Example 9: Drain a node and handle errors more strictly:
kubectl drain node-9 --ignore-daemonsets --force --delete-local-data --grace-period=30 --skip-wait-for-delete-timeout
This command combines multiple flags to handle different scenarios, ensuring pods are evicted forcefully and local data is deleted.
Example 10: Drain a node interactively, allowing confirmation before eviction:
kubectl drain node-10 --ignore-daemonsets --interactive
Using --interactive
prompts for user confirmation before initiating the drain operation on node-10
.
To verify whether the kubectl drain
command executed successfully, you can check the status of the node using:
kubectl get nodes
Ensure that the node you drained is marked as Ready=False
and has no pods scheduled on it anymore.
Also check similar articles.
Scheduling Nodes in Kubernetes with kubectl uncordon
How to Cordon Kubernetes Nodes with kubectl cordon
Monitor Resource Usage with kubectl top
Access Cluster Information Using kubectl cluster-info
Managing Kubernetes Certificates with kubectl certificate
Discussion about this post