Handling Pod Scheduling on Cordoned Nodes
Understanding Pod Behavior on Cordoned Nodes
When a node is cordoned in Kubernetes, the Kubernetes scheduler will no longer place new pods on that node. However, existing pods that are already running on the cordoned node will continue to run.
This behavior is important to understand, as it means that you can safely cordon a node without immediately disrupting any running workloads. The pods on the cordoned node will continue to run until they are terminated or rescheduled to a different node.
Draining Pods from Cordoned Nodes
While existing pods will continue to run on a cordoned node, you may want to gracefully drain the pods from the node before performing maintenance or decommissioning the node. To do this, you can use the kubectl drain
command:
kubectl drain --delete-local-data < node-name > --ignore-daemonsets
This command will evict all the pods from the specified node, except for pods managed by DaemonSets and pods with local data. The --ignore-daemonsets
and --delete-local-data
options ensure that critical pods are not disrupted during the draining process.
Handling Pod Rescheduling
When a node is cordoned, the Kubernetes scheduler will not place any new pods on that node. However, if a pod on the cordoned node needs to be rescheduled (e.g., due to a node failure or pod eviction), the scheduler will attempt to reschedule the pod on a different, available node.
To ensure that pods are rescheduled correctly, you should configure appropriate pod scheduling policies, such as node affinity or pod anti-affinity, to control where the pods are placed. This can help prevent disruptions to your application's availability during node maintenance or decommissioning.
Example: Draining Pods from a Cordoned Node
Here's an example of how to drain pods from a cordoned node in a Kubernetes cluster running on Ubuntu 22.04:
## Cordon the node
kubectl cordon node-1
## Drain the node
kubectl drain node-1 --ignore-daemonsets --delete-local-data
## Verify that the node is cordoned and drained
kubectl get nodes
kubectl get pods -o wide
This will cordon the node-1
node, drain all the pods from it (except for DaemonSet pods and pods with local data), and then you can verify the node's status and the pod placements.