DEV Community

Shin'ya Ueoka
Shin'ya Ueoka

Posted on

How to evict specific pods on the Kubernetes cluster

Introduction

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.

Pod Eviction

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
Enter fullscreen mode Exit fullscreen mode

kubectl-evict: evicts specific pods

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.

GitHub logo ueokande / kubectl-evict

A kubectl plugin to evict pods

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
  • Supports --dry-run and --grace-period flags

You can get the plugin by the following:

$ go install github.com/ueokande/kubectl-evict@latest
Enter fullscreen mode Exit fullscreen mode

Usage

To evict a specific pod:

$ kubectl evict nginx-abcd-1234
Enter fullscreen mode Exit fullscreen mode

It also allows evicting pods by label selector. The following shows to evict pods which has a label app=nginx:

$ kubectl evict -l app=nginx
Enter fullscreen mode Exit fullscreen mode

Evict pods created by Deployment:

$ kubectl evict deployment/nginx
Enter fullscreen mode Exit fullscreen mode

You can of course evict pods from the node:

$ kubectl evict node/worker-1
Enter fullscreen mode Exit fullscreen mode

Note that kubectl evict does NOT make the node as unschedulable (cordon), so use kubectl cordon or kubectl drain if you need to do this.

Top comments (2)

Collapse
 
lkoniecz profile image
lkoniecz • Edited

error: unknown command "evict" for "kubectl"

I assume I need to install the plugin to kubectl?

Collapse
 
dwrolvink profile image
Dorus • Edited

I had the same problem. It turns out kubectl will look for binaries with the name kubectl-$suffix in the path. If you don't have GOBIN env set, the default will be ~/go/bin, which is not by default in the path.

You can do GOBIN=/usr/local/bin/ go install, but this requires sudo, so what I did is just extend the path: export PATH="$PATH:/home/username/go/bin" and then go install github.com/ueokande/kubectl-evict@latest.

This is all that was required for me.