I recently noticed a big accumulation of pods with status ‘Shutdown’. We have been using Kubernetes since October, 2020.
Production and staging is running on the same nodes except that staging uses preemtible nodes to cut the cost. The containers are also stable in staging. (Failures occur rarely as they are caught in testing before).
Service provider Google Cloud Kubernetes.
I familiarized myself with the docs and tried searching however neither I recognize neither google helps with this particular status. There are no errors in the logs.
I have no problem pods being stopped. Ideally I’d like K8s to automatically delete these shutdown pods. If I run kubectl delete po redis-7b86cdccf9-zl6k9
, it goes away in a blink.
kubectl get pods | grep Shutdown | awk '{print $1}' | xargs kubectl delete pod
is manual temporary workaround.
PS. k
is an alias to kubectl
in my environment.
Final example: it happens across all namespaces // different containers.
I stumbled upon few related issues explaining the status
https://github.com/kubernetes/website/pull/28235
https://github.com/kubernetes/kubernetes/issues/102820
"When pods were evicted during the graceful node shutdown, they are marked as failed. Running kubectl get pods
shows the status of the the evicted pods as Shutdown
."
7
Answers
First, try to forcefully delete the kubernetes pod using the below command:
You can directly delete the pod using the below command:
Then, check the status of the pod using the below command:
Here, you will see that pods have been deleted.
You can also verify using the documentation in the yaml file also.
Most programs gracefully shut down when receiving a SIGTERM, but if you are using third-party code or are managing a system you don’t have control over, the preStop hook is a great way to trigger a graceful shutdown without modifying the application.
Kubernetes will send a SIGTERM signal to the containers in the pod.
At this point, Kubernetes waits for a specified time called the termination grace period.
For more information refer.
Right now Kubernetes doesn’t remove evicted and shutdown status pods by default. We also faced a similar kind of issue in our environment.
As an automatic fix, you can create a Kubernetes cronjob which can remove the pod with evicted and shutdown status. The Kubernetes cronjob pod can authenticate using the serviceaccount and RBAC where you can restrict the verbs and namespaces for your utility.
You can use https://github.com/hjacobs/kube-janitor .This provide various configurable option to clean up
The evicted pods are not removed on purpose, as k8s team says here 1, the evicted pods are nor removed in order to be inspected after eviction.
I believe here the best approach would be to create a cronjob 2 as already mentioned.
You don’t need any grep – just use selectors that kubectl provides. And, btw, you cannot call kubectl from the busybox image, because it doesn’t have kubectl at all. I also created a service account with the right of pods deletion.
My take on this problem looks something like this (inspiration from other solutions here):
I just set up a cronjob to clean the dead GKE pods.
Complete setup includes RBAC role, role binding, and a service account.
Service account and cluster role setup.
Cronjob to clean up dead pods.