Kubernetes — Delete All Pods Evicted State

Maciej
1 min readApr 22, 2021

Introduction

In K8s sometimes when we run pods, due to lack of resources like memory or CPU or some application error pods get evicted. In this case Kubernetes will try restart these evicted pods but , if there are still no resources left or the application has any errors, the pod will still be in evicted state.

Should we care about this?

Definitely yes, when we have many evicted pod in the cluster it can lead to a serious network load as each pod, although evicted, is connected to the network and is blocking the IP address which can lead to exhaustion. of course, if we have a fixed pool of addresses for our cluster.

How Delete Evicted Pods ?

The easiest way to do this is to use the kubectl delete pod command to delete pod in Kuberenetes. This is a bit limiting because with this command we have to give the name of a particular pod to delete it.

Things get complicated when we have 200 pods that are in evicted state. Running kubectl delete pod 200 times was terrible.

To quickly remove a large number of pods in evicted status, execute the following command.

ozyrys@ozyrys-Lenovo-ideapad-700-15ISK:~$ kubectl get pod -n develop | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n develop

⚠️ If we need to remove the pod in a state other than Evicted , just change the word Evicted to CrashLoopBackOff and run command.

Source: https://giphy.com

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.