Kubernetes is a very popular and the most widely used container orchestration. It has a sophisticated and flexible scheduling system to deploy your application on production. The administrators would use a kubectl CLI to manage the cluster and deployed pods. The kubectl CLI provides enough features to manage the cluster, but the developer can customize CLI with a kubectl plugin. This article will present what pod eviction is and introduce a kubectl plugin to do that, kubectl-evict.
Kubernetes provides pod eviction, which stops pods and removes them from the node. The pod eviction is usually used when the node is shutting down, or the cluster performs a rolling restart of nodes to minimize the downtime of the service. The kubectl CLI has a sub-command
kubectl drain to evict pods from the specified node and, the command makes the node as unschedulable:
$ kubectl drain your-node
The kubectl CLI does not provide a feature to evict specific pods. I sometimes need this feature to safely remove pods from the node or test a PodDisruptionBudget. The kubectl-evict is a kubectl plugin that allows evicting certain pods from the node.
Although there are already plugins to evict pods such as dwradcliffe/kubectl-evict and kubectl-evict-pod, the above plugin has a high compatibility interface with
kubectl logs or
kubectl exec and provides the following features:
- Allows evicting pods by a label selector
- Evicts pods created by Deployment or DaemonSet
You can get the plugin by the following:
$ go install github.com/ueokande/kubectl-evict@latest
To evict a specific pod:
$ kubectl evict nginx-abcd-1234
It also allows evicting pods by label selector. The following shows to evict pods which has a label
$ kubectl evict -l app=nginx
Evict pods created by Deployment:
$ kubectl evict deployment/nginx
You can of course evict pods from the node:
$ kubectl evict node/worker-1
kubectl evict does NOT make the node as unschedulable (cordon), so use
kubectl cordon or
kubectl drain if you need to do this.