Signup/Sign In
LAST UPDATED: OCTOBER 26, 2023

How to Delete all the Evicted Pods in Kubernetes?

    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.

    delete evicted pods kubernetes

    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 the 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 Kubernetes. 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 the name nginx-07rdsz in the 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 the 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 many ways to do everything when working with a 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, check out how you can apply Namespace Resource Quotas and Limit Range to manage pod resource consumption. Not having this may be the reason for your pods getting Evicted.

    Frequently Asked Questions

    Here are some frequently asked questions related to evicted pods in Kubernetes.

    1. What are Evicted Pods in Kubernetes?

    Evicted Pods are Kubernetes Pods that have been terminated by the system due to resource constraints or other issues. They are not running and do not consume any resources, but they still exist in the cluster's metadata.

    2. How do you find all Evicted Pods in Kubernetes?

    To find all Evicted Pods in Kubernetes, you can use the kubectl get pods command with the "--field-selector" flag and the "status.phase" field set to "Failed". This will return a list of all Pods that have failed, including any that have been evicted.

    3. How do you delete all Evicted Pods in Kubernetes?

    To delete all Evicted Pods in Kubernetes, you can use the kubectl delete pod command with the "--field-selector" flag and the "status.phase" field set to "Failed". This will delete all Pods that have failed, including any that have been evicted.

    4. What are some best practices for managing Evicted Pods in Kubernetes?

    To avoid issues with Evicted Pods in Kubernetes, it is important to properly configure resource limits and requests for your applications, to ensure they have the resources they need to run. It is also a good practice to regularly monitor and clean up any failed or evicted Pods, to avoid wasting resources and causing performance issues.

    Related Articles

    here are some related articles that I think you might be interested in reading based on the current article.

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS