Signup/Sign In

How to Delete all the Evicted Pods in Kubernetes?

Posted in Tricks   LAST UPDATED: AUGUST 9, 2021

    In Kubernetes, when we run pods, at times due to lack of resources like CPU or memory, or due to some application error, pods get evicted, then Kubernetes restart these evicted pods, but still when you run the kubectl get pod command, you will see the evicted pods.

    To list down all the pods, for a namespace studytonight, you can run the following command:

    kubectl get pod -n studytonight

    Running this command will list down all the pods running in the studytonight namespace.


    NAME READY STATUS RESTARTS AGE
    nfs-server-h6nw8 1/1 Running 0 1h
    nfs-web-07rxz 0/1 Evicted 8 16m
    nfs-web-fdr9h 0/1 Evicted 8 16m

    Why bother deleting an Evicted Pod?

    When we have too many evicted pods in our cluster, this can lead to network load as each pod, even though it is evicted is connected to the network and in case of a cloud Kubernetes cluster, will have blocked an IP address, which can lead to exhaustion of IP addresses too if you have a fixed pool of IP addresses for your cluster.

    Also, when we have too many pods in Evicted status, it becomes difficult to monitor the pods by running the kubectl get pod command as you will see too many evicted pods, which can be a bit confusing at times.

    Delete Evicted Pods

    We can use the kubectl delete pod command to delete any pod in Kuberenetes. But with this command, we need to provide the pod name to delete any particular pod.

    If you have one or two pods to delete, you can easily do that, by first running the kubectl get pod command:

    kubectl get pod -n studytonight


    NAME READY STATUS RESTARTS AGE
    busybox-h6yw8 1/1 Running 0 1h
    nginx-07rdsz 0/1 Evicted 8 16m
    busybox-fzr9h 0/1 Evicted 8 16m

    Then using the pod name to delete the pod, like:

    kubectl delete pod nginx-07rdsz -n studytonight

    The above command will delete the pod with name nginx-07rdsz in studytonight namespace and will release all the resources held by that pod.

    But what if you have 100 Evicted pods?

    Well, don't worry, we have a special command, which will find all the evicted pods and delete them just by running a single command. Here is the command:

    kubectl get pod -n studytonight | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n studytonight

    In the above command, we are searching for the pods with status Evicted and then running kubectl delete pod command for all of them.

    You can also use the above command to delete pods in any particular status like Running, Evicted, CrashLoopBackOff, etc.

    Conclusion:

    There are so many ways to do everything when you are working with Linux machine. This was just an ode to that freedom. So if you are pissed at pods getting Evicted too frequently, use this command to get rid of them.

    Also, checkout how you can apply Namespace Resource Quotas and Limit Range to managae pods resource consumption. Not having this may be the reason for your pods getting Evicted.

    You may also like:

    Published by: iamabhishek
    Tags:KubernetesLinuxHowTo
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS