remove node from Kubernetes cluster safely

Confirm the name of the node you want to remove, and make sure that the pods on the node can be terminated. The pod list can be retrieved using the below command:

kubectl get nodes

Mark the node as unschedulable by using the cordon command. This ensures that no new pods will get scheduled to the node while you are preparing it for removal.

kubectl cordon <nodename>

Use the drain command to evict the user pods from the node. They will be scheduled on other nodes by their controller. This command is blocking and will return when the pods have been removed.

kubectl drain <nodename>

You can verify that no user pods are running on the node in question using get pods again.

kubectl get pods

There may still be pods running on the node that are system pods. DaemonSet pods may also still be present. If the drain command fails, it may be because some pods are not managed by a controller. Running drain again with the –force flag will cause all of the pods to be deleted.

At this point you can either maintain the node or replace it completely. If replacing the node, use the following command to delete it from the cluster.

kubectl delete node <nodename>

If you leave the node in the cluster after the maintenance operation, you need to run

kubectl uncordon <node name>