Kubernetes can detect node failure and reschedule pods to other nodes automatically. The only issue is the time it takes for the node to go down and the pod to be rescheduled. Let’s see how you can drain a node in Kubernetes. But first, make sure you have kubectl installed on your system. To execute the commands, you will also need to install the minikube cluster and then launch the terminal window. You can use the minikube by following the below-mentioned command.
In addition to that, make sure your Kubernetes server must be version 1.5 or newer. Enter the “kubectl version” if you want to check the version. You must meet certain requirements to successfully complete this task. You don’t need your applications to be available during the node drain, or you don’t need them to be highly available at all. You have learned about the PodDisruptionBudget concept and set up PodDisruptionBudgets for the apps that require them.
Configure a Disruption Budget if Desired
To ensure that your workloads are available during maintenance, create a PodDisruptionBudget. Configure a PodDisruptionBudgets first, then continue following this advice if availability is crucial for any applications that may run on the node(s) that you’re draining.
The Use of Kubectl Drain to Remove/Delete a Node
You can use kubectl drain to evict all of your pods before performing maintenance on a node Safe evictions end the pod’s containers in a tidy manner while staying within the PodDisruptionBudgets that you have established.
Kubectl drain eventually ignores some pods on the node that are not possible to be destroyed by default. It is, therefore, safe to shut down the node by turning off its physical machine or, if it is running on a cloud platform, uninstalling its virtual machine. To begin, identify the node you wish to drain by its name. The command below will display a list of all of the nodes mentioned in your cluster.
Next, inform Kubernetes that the node should be drained:
After the node returns (and without causing an error), you can turn it off (or equivalently, if on a cloud platform, delete the virtual machine that backs the node). If you leave a node in the cluster for maintenance, use the following command to tell Kubernetes that it can start rescheduling new pods on the node.
Draining Numerous Nodes at the Same Time
Only one node should receive the kubectl drain instruction at a time. You can, however, perform many kubectl drain instructions simultaneously, in various terminals, or the background, for different nodes. Even if numerous drain instructions are operating at the same time, PodDisruptionBudget must be observed.
If you have a StatefulSet with 3 replicas and a PodDisruptionBudget of 2, kubectl drain will only remove a pod if all those pods are ready. If you run multiple drain commands at the same time, Kubernetes will honor the PodDisruptionBudget and guarantee that only one drain is executed.
Eviction in Kubernetes
You can use the eviction API to programmatically initiate evictions if you are not willing to use kubectl drain (for example, to avoid executing an external command or to have better control throughout the pod eviction process).
You must first be familiar with Kubernetes language clients in order to use the API. A Pod’s eviction subresource can be thought of as a DELETE action on the Pod that is policy-controlled. To try an eviction, you POST an attempted operation (or to attempt to construct an Eviction more accurately).
Stuck Evictions
Unless you act, an application may become broken, and the eviction API will always return 429 or 500. Similar effects can also be seen if the last Pod to be evicted has a long termination grace period.
There are two options in this situation: abort or pause the automatic operation. Restart the automation after determining the cause of the blocked application. Rather than using the eviction API, DELETE the Pod from your cluster’s control plane when a reasonable amount of time has passed.
Conclusion
In this article, we have talked about the use of kubectl drain. Various commands are explained to help you understand the method of draining in Kubernetes.