521

I tried to delete a ReplicationController with 12 pods and I could see that some of the pods are stuck in Terminating status.

My Kubernetes cluster consists of one control plane node and three worker nodes installed on Ubuntu virtual machines.

What could be the reason for this issue?

NAME        READY     STATUS        RESTARTS   AGE
pod-186o2   1/1       Terminating   0          2h
pod-4b6qc   1/1       Terminating   0          2h
pod-8xl86   1/1       Terminating   0          1h
pod-d6htc   1/1       Terminating   0          1h
pod-vlzov   1/1       Terminating   0          1h
halfer
  • 19,824
  • 17
  • 99
  • 186
Dimuthu
  • 7,946
  • 6
  • 22
  • 37
  • Are the scheduler and controller-manager running? – Antoine Cotten Feb 18 '16 at 21:00
  • 1
    Could be related to https://github.com/kubernetes/kubernetes/issues/51835 – donhector Mar 19 '18 at 23:21
  • 1
    @PromisePreston: please refrain from deliberately adding home-made tags to questions - [we remove them here](https://meta.stackoverflow.com/q/253028). – halfer Feb 14 '22 at 23:08
  • @halfer, I can't seem to find a StackOverflow policy that is against adding home-made tags to questions like the one I added previously to the question. The link you pointed me to showed that a lot of people disagreed with your POV. I mean there was a lot of bias as to why it is wrong to add tags to questions, you only stated your POV which a lot of people disagreed with. For a fact adding tags to questions helps for better search engine optimization, it also helps to bring a question into better context. You can point me to a StackOverflow policy that disagrees with adding tags to questions. – Promise Preston Feb 15 '22 at 08:53
  • 1
    Hi there Promise. The link I have supplied is canonical in two ways: firstly, it is highly upvoted. That means that the community have accepted it, and that any opposing positions would need to feature on another answer that beats it. There is indeed a counter-proposal, and that has not been upvoted as much, and it has been significantly downvoted (+39 and -19). – halfer Feb 15 '22 at 20:37
  • 1
    Secondly, guidelines become policy when the community (and its moderators) treat them as such. I've been fairly involved with _Meta_ on this question, and I take the view that this has happened on the home-made tags question. Have you been involved with discussing this policy yourself, or have you posted on Meta to express a view on it? I'm certainly in favour of anyone having a view, but I'd be less happy for folks to do what they like based on the observation that every policy has a few dissenters. – halfer Feb 15 '22 at 20:41

25 Answers25

890

You can use following command to delete the POD forcefully.

kubectl delete pod <PODNAME> --grace-period=0 --force --namespace <NAMESPACE>
Nitin
  • 10,101
  • 4
  • 17
  • 34
  • 71
    I did this in my cluster and the pod seemed to be removed but when I checked the node it's container was still running. I ended up restarting Docker on the node itself. https://github.com/kubernetes/kubernetes/issues/25456 Just be careful you're not hiding a systemic problem with this command. – mqsoh Jan 25 '18 at 19:03
  • 1
    If the pod is in a namespace other than `default` namespace then it is required to include `-n `, otherwise above command wont work. – IamLearning Mar 28 '18 at 08:07
  • 19
    @mqsoh : The force delete just remove it from the api-server store(etcd), the actual resource deleted may end up running indefinitely. – bits Apr 12 '18 at 17:27
  • my pod is stuck in terminating state even after using above command. I tried login to host and checked if container present using docker ps -a command. Container is not present nor able to delete terminating pod. – Swapnil B. Aug 10 '18 at 16:50
  • 25
    "warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely" What resources ? – Akshay Jan 31 '19 at 06:30
  • I have the following problem: `warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely. Error from server (Conflict): Operation cannot be fulfilled on namespaces "ingress-nginx": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system.` – papanito Aug 04 '19 at 18:12
  • i also saw the warning that @Akshay mentioned. i am worring about the inifinte resouces. is there a clean way to termiate/exit the pod? – RyanShao Aug 16 '19 at 01:04
  • 1
    This happened during a deploy and healthy apps were stuck in a state of termination while unhealthy apps entered a restart loop (due to stringent ready/health check timeouts).. awesome! I thought Kube's strength was resilience?? The only time the site has been down during the last 2 years has been ON Kubernetes. I really hope all this is down to user error because I'm losing faith. On another note, I force terminated and resources were released. – Damien Roche Sep 05 '19 at 16:18
  • 1
    You only answers *how* but I believe we are more wondering *why*. – MasterMind May 05 '22 at 10:21
118

The original question is "What could be the reason for this issue?" and the answer is discussed at https://github.com/kubernetes/kubernetes/issues/51835 & https://github.com/kubernetes/kubernetes/issues/65569 & see https://www.bountysource.com/issues/33241128-unable-to-remove-a-stopped-container-device-or-resource-busy

Its caused by docker mount leaking into some other namespace.

You can logon to pod host to investigate.

minikube ssh
docker container ps | grep <id>
docker container stop <id> 
noelmcloughlin
  • 1,723
  • 1
  • 12
  • 10
  • 22
    I can't believe this is the least upvoted answer and didn't have a single comment. While all the other answers address ways to work around or fix the problem, the OP clearly asked for the reason why the condition happens in the first place. – MisterStrickland May 10 '20 at 03:21
  • 3
    The answer already says this but I would like to stress it: Make sure you run these commands in the node where the pod is hosted! – Gerardo Cauich Jan 10 '21 at 19:31
89

Force delete the pod:

kubectl delete pod --grace-period=0 --force --namespace <NAMESPACE> <PODNAME>

The --force flag is mandatory.

Joan
  • 4,079
  • 2
  • 28
  • 37
  • 103
    But the real question for me is "why do we have to resort to this in the first place?" What kinds of things cause pods to get in this stuck state under otherwise normal operating conditions? – neverfox Jun 09 '17 at 16:41
  • 7
    Well, I can give you one example, we had a java container that had graceful shutdown, but was garbage-collecting itself to death, thus not reacting to signals. – Aurelia May 02 '18 at 10:35
  • 1
    It's good to provide the namespace, otherwise in a multi-namespace environment your pod will not be found, by default it's looking in the `kube-system` namespace. – Daniel Andrei Mincă Dec 04 '18 at 13:56
  • To force delete all pods in a namesapce at once `ktl get pods -o custom-columns=:metadata.name | xargs kubectl delete pod --force --grace-period=0` – deepdive Mar 17 '20 at 05:49
  • @deepdive `kubectl delete pod --all -n ` . – P.... Mar 10 '22 at 14:50
  • @neverfox, I discovered a case when a pod was deleted after the node became unavailable (terminated). It looks like Kubernetes waits some period of time to remove this node completely from the cluster, but the pod will be in a terminated state until then. – Roman Bats Sep 02 '22 at 13:34
  • @neverfox This maybe caused by, ```the pod has a finalizer associated with it that is not completing, or the pod is not responding to termination signals``` – jayaprakash R Oct 11 '22 at 18:22
48

I found this command more straightforward:

for p in $(kubectl get pods | grep Terminating | awk '{print $1}'); do kubectl delete pod $p --grace-period=0 --force;done

It will delete all pods in Terminating status in default namespace.

belabrinel
  • 851
  • 8
  • 15
  • 7
    If you want to run it on another namespaces like `kube-system` use: `for p in $(kubectl get pods -n kube-system| grep Terminating | awk '{print $1}'); do kubectl delete pod $p --grace-period=0 --force -n kube-system;done` – acrogenesis Apr 15 '19 at 15:15
  • This answer combined all the other answers perfectly for me. The only caveat was that I had a bunch of orphaned PVCs and a PV. So, I adapted your nice little script with this one liner `for p in $(kubectl get pvc | grep Bound | awk '{print $1}'); do kubectl delete pvc $p --grace-period=0 --force;done ` and then deleted the one PV. – granthbr Feb 14 '21 at 09:33
  • 1
    `kubectl delete pod --force $(kubectl get pods | grep Terminating | cut -d' ' -f1)` is the short form of this answer. – suzanshakya Sep 23 '22 at 17:21
40

In my case the --force option didn't quite work. I could still see the pod ! It was stuck in Terminating/Unknown mode. So after running

kubectl -n redis delete pods <pod> --grace-period=0 --force

I ran

kubectl -n redis patch pod <pod> -p '{"metadata":{"finalizers":null}}'
dejanualex
  • 3,872
  • 6
  • 22
  • 37
sh0umik
  • 1,549
  • 2
  • 17
  • 27
  • 4
    Before doing this, it's worth reading https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/ to understand what finalizers are. Also, looking at the specific finalizer that is stuck might give hints _why_ it's stuck and whether it's safe to bypass... – Beni Cherniavsky-Paskin May 30 '19 at 11:19
32

Delete the finalizers block from resource (pod,deployment,ds etc...) yaml:

"finalizers": [
  "foregroundDeletion"
]
Roee Rakovsky
  • 345
  • 3
  • 8
  • 1
    Persistent volume got deleted after this. What does it really do? – raiyan Jul 23 '18 at 13:43
  • 1
    This was the only thing that fixed the stuck pod for me when `delete -grace-period=0 --force` didn't. I'd also appreciate some elaboration on what does it do exactly, though. – valorl Aug 24 '18 at 07:52
  • This page explains foregroundDeletion. Its a meta data value that indicates the object is in the process of deletion. https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/ – Sean Keane Nov 02 '18 at 22:16
22

I stumbled upon this recently to free up resource in my cluster. here is the command to delete them all.

kubectl get pods --all-namespaces | grep Terminating | while read line; do
  pod_name=$(echo $line | awk '{print $2}' ) \
  name_space=$(echo $line | awk '{print $1}' ); \
  kubectl delete pods $pod_name -n $name_space --grace-period=0 --force
done

hope this help someone who read this

Pavlo Neiman
  • 7,438
  • 3
  • 28
  • 28
rrr
  • 369
  • 4
  • 8
  • 1
    Here is a warning: warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely. – d0zingcat Aug 04 '22 at 09:19
17

Practical answer -- you can always delete a terminating pod by running:

kubectl delete pod NAME --grace-period=0

Historical answer -- There was an issue in version 1.1 where sometimes pods get stranded in the Terminating state if their nodes are uncleanly removed from the cluster.

Alex Robinson
  • 12,633
  • 2
  • 38
  • 55
  • 2
    I guess that is the issue. I powered off one minion vm without removing from nodes. Is this an acceptable behaviour? Or is there a fix to remove those pods from kubernetes? – Dimuthu Feb 19 '16 at 02:09
  • Yeah, the workaround until version 1.2 comes around is to delete the pods. – Alex Robinson Feb 19 '16 at 07:06
  • 38
    You can always force delete a terminating pod with `kubectl delete pod NAME --grace-period=0` – Clayton Feb 21 '16 at 06:20
  • 4
    The doc says when running `kubectl delete ...` a `SIG_TERM` request will be sent to the container. But what if after the the grace period, the container is still running? I got a bunch of pods stuck at `Terminating`, some written in go, some in nodejs. The replicationController was removed, and the container is is still running – Quyen Nguyen Tuan Mar 09 '16 at 10:00
  • @QuyenNguyenTuan: I also had the same issue but ultimately I found out that cause for such container not destroying problems is due to a zombie processes in my container. https://en.wikipedia.org/wiki/Zombie_process. – Dimuthu Mar 17 '16 at 07:09
  • 4
    `kubectl delete pod PODNAME --grace-period=0` worked for me as suggested by Clayton. – Yogesh Jilhawar May 06 '16 at 06:34
8

Please try below command:

kubectl patch pod <pod>-p '{"metadata":{"finalizers":null}}'
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ela_murugan
  • 201
  • 3
  • 3
  • 4
    From Review:  Command/Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users with similar issues. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Feb 09 '21 at 12:23
  • Following up on @sɐunıɔןɐqɐp's comment, seriously, what does this command do? – TheLebDev Feb 14 '22 at 11:38
  • 1
    What this command does is it attempts to manually patch the pod in the effort to force kubernetes into deleting it immediately without any additional considerations. This is provided there isn't a different reason it's not deleting like an internal service error on the cluster's management pods. In addition the command should be "kubectl patch pod --patch '{"meta data":{"finalizers":null}}". Leaving the --patch command out will throw an error – Illegal Operator May 22 '23 at 09:01
8

For my case, I don't like workaround. So there are steps :

  • k get pod -o wide -> this will show which Node is running the pod
  • k get nodes -> Check status of that node... I got it NotReady

I went and I fixed that node. For my case, it's just restart kubelet :

  • ssh that-node -> run swapoff -a && systemctl restart kubelet (Or systemctl restart k3s in case of k3s | or systemctl restart crio in other cases like OCP 4.x (k8s <1.23) )

Now deletion of pod should work without forcing the Poor pod.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
7

If --grace-period=0 is not working then you can do:

kubectl delete pods <pod> --grace-period=0 --force
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Paul Ma
  • 609
  • 6
  • 8
  • There are some situations where this appears to work but it does not actually delete. It may have to do with issues where kubelet loses state of the pod and can not get the state so leaves it .. (e.g https://github.com/kubernetes/kubernetes/issues/51835 ). I have not found a way to purge it as of yet. – cgseller Apr 03 '18 at 14:09
7

Force delete ALL pods in namespace:

kubectl delete pods --all -n <namespace> --grace-period 0 --force
6

I stumbled upon this recently when removing rook ceph namespace - it got stuck in Terminating state.

The only thing that helped was removing kubernetes finalizer by directly calling k8s api with curl as suggested here.

  • kubectl get namespace rook-ceph -o json > tmp.json
  • delete kubernetes finalizer in tmp.json (leave empty array "finalizers": [])
  • run kubectl proxy in another terminal for auth purposes and run following curl request to returned port
  • curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json 127.0.0.1:8001/k8s/clusters/c-mzplp/api/v1/namespaces/rook-ceph/finalize
  • namespace is gone

Detailed rook ceph teardown here.

zub0r
  • 1,299
  • 1
  • 14
  • 20
6

I used this command to delete the pods

kubectl delete pod --grace-period=0 --force --namespace <NAMESPACE> <PODNAME>

But when I tried run another pod, it didn't work, it was stuck in "Pending" state, it looks like the node itself was stuck.

For me, the solution was to recreate the node. I simply went to GKE console and deleted the node from the cluster and so GKE started another.

After that, everything started to work normally again.

Thiago G. Alves
  • 1,202
  • 1
  • 14
  • 24
6

I had to same issue in a production Kubernetes cluster.

A pod was stuck in Terminating phase for a while:

pod-issuing   mypod-issuing-0   1/1     Terminating   0  27h

I tried checking the logs and events using the command:

kubectl describe pod mypod-issuing-0 --namespace pod-issuing
kubectl logs mypod-issuing-0 --namespace pod-issuing

but none was available to view

How I fixed it:

I ran the command below to forcefully delete the pod:

kubectl delete pod <PODNAME> --grace-period=0 --force --namespace <NAMESPACE>

This deleted the pod immediately and started creating a new one. However, I ran into the error below when another pod was being created:

Unable to attach or mount volumes: unmounted volumes=[data], unattached volumes=[data mypod-issuing-token-5swgg aws-iam-token]: timed out waiting for the condition

I had to wait for 7 to 10 minutes for the volume to become detached from the previous pod I deleted so that it can become available for this new pod I was creating.

halfer
  • 19,824
  • 17
  • 99
  • 186
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
5

you can use awk :

kubectl get pods --all-namespaces | awk '{if ($4=="Terminating") print "oc delete pod " $2 " -n " $1 " --force --grace-period=0 ";}' | sh
Abilogos
  • 4,777
  • 2
  • 19
  • 39
jon
  • 51
  • 1
  • 1
  • I did a small variation: kubectl get pods --all-namespaces | awk '{if ($4=="Terminating") print "kubectl delete pods " $2 " -n " $1 " --force --grace-period=0 ";}' | sh – Reginaldo Santos Aug 04 '21 at 20:04
  • this is great. but I mixes oc (OCP and kubectl) so suggest to replace the oc with kubectl. – Tilo May 03 '23 at 19:02
4

Before doing a force deletion i would first do some checks. 1- node state: get the node name where your node is running, you can see this with the following command:

"kubectl -n YOUR_NAMESPACE describe pod YOUR_PODNAME"

Under the "Node" label you will see the node name. With that you can do:

kubectl describe node NODE_NAME

Check the "conditions" field if you see anything strange. If this is fine then you can move to the step, redo:

"kubectl -n YOUR_NAMESPACE describe pod YOUR_PODNAME"

Check the reason why it is hanging, you can find this under the "Events" section. I say this because you might need to take preliminary actions before force deleting the pod, force deleting the pod only deletes the pod itself not the underlying resource (a stuck docker container for example).

JBoy
  • 5,398
  • 13
  • 61
  • 101
3

I'd not recommend force deleting pods unless container already exited.

  1. Verify kubelet logs to see what is causing the issue "journalctl -u kubelet"
  2. Verify docker logs: journalctl -u docker.service
  3. Check if pod's volume mount points still exist and if anyone holds lock on it.
  4. Verify if host is out of memory or disk
Hem
  • 619
  • 13
  • 26
3

I am going to try the most extense answer, because none of the above are wrong, but they do not work in all case scenarios.

The usual way to put an end to a terminating pod is:

kubectl delete pod -n ${namespace} ${pod} --grace-period=0 

But you may need to remove finalizers that could be preventing the POD from stoppoing using:

kubectl -n ${namespace} patch pod ${pod} -p '{"metadata":{"finalizers":null}}'

If none of that works, you can remove the pod from etcd with etcdctl:

# Define variables
ETCDCTL_API=3
certs-path=${HOME}/.certs/e
etcd-cert-path=${certs-path}/etcd.crt
etcd-key-path=${certs-path}/etcd.key
etcd-cacert-path=${certs-path}/etcd.ca
etcd-endpoints=https://127.0.0.1:2379
namespace=myns
pod=mypod

# Call etcdctl to remove the pod
etcdctl del \
--endpoints=${etcd-endpoints}\
--cert ${etcd-cert-path} \
--key ${etcd-client-key}\
--cacert ${etcd-cacert-path} \
--prefix \
/registry/pods/${namespace}/${pod} 

This last case should be used as last resource, in my case I ended having to do it due to a deadlock that prevented calico from starting in the node due to Pods under terminating status. Those pods won't be removed until calico is up, but they have reserved enough CPU to avoid calico, or any other pod, from Initializing.

Apeasant
  • 177
  • 8
3

To delete all pods in "Terminating" state in all namespaces:

kubectl get pods --all-namespaces | awk '/Terminating/{print $1 " " $2}' | while read -r namespace pod; do kubectl delete pod "$pod" -n "$namespace" --grace-period=0 --force;done
Branko
  • 31
  • 1
  • 1
2

One reason WHY this happens can be turning off a node (without draining it). Fix in this case is to turn on the node again; then termination should succeed.

  • A very over simplistic answer. What if you can't turn it back on. If Kubernetes is truly declarative then it should notice this problem and fix itself. – Mark Nov 29 '22 at 15:15
1

Following command with awk and xargs can be used along with --grace-period=0 --force to delete all the Pods in Terminating state.

kubectl get pods|grep -i terminating | awk '{print $1}' | xargs kubectl delete --grace-period=0 --force pod
Deb
  • 587
  • 4
  • 12
  • I have the same error message scenario. I recently installed an NFS server in my cluster, and some nodes of the same node pool have this problem. I provisionally scale the nodes, and the problem is solved, but it is not the final solution. I'm still investigating, as the node has free resources, nothing still justifies the problem – ovrdoz Feb 22 '22 at 21:48
  • this forcing process, it's bad and negative for Kubernetes, I don't recommend this type of action...only in last case of troubleshooting – ovrdoz Feb 22 '22 at 21:50
  • 1
    if one is 'required' to stop pods and forceful stop doesnt create any issue, then why not. Kubernetes wouldnt have this option had it been bad and negative. There may be many scenarios where Pods has to be stopped at a certain instance, this option may be used. Btw "one size doesnt fit all" – Deb Mar 06 '22 at 03:50
1

go templates will work without awk, for me it works without --grace-period=0 --force but, add it if you like

this will output the command to delete the Terminated pods.

kubectl get pods --all-namespaces -otemplate='{{ range .items }}{{ if eq .status.reason  "Terminated" }}{{printf "kubectl delete pod -n %v %v\n" .metadata.namespace .metadata.name}}{{end}}{{end}}'

if you are happy with the output, you cat add | sh - to execute it. as follow:

kubectl get pods --all-namespaces -otemplate='{{ range .items }}{{ if eq .status.reason  "Terminated" }}{{printf "kubectl delete pod -n %v %v\n" .metadata.namespace .metadata.name}}{{end}}{{end}}' |sh -
Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43
1

My pods stuck in 'Terminating', even after I tried to restart docker & restart server. Resolved after edit the pod & delete items below 'finalizer'

$ kubectl -n mynamespace edit pod/my-pod-name
halfer
  • 19,824
  • 17
  • 99
  • 186
0

for me below command has resolved the issue

oc patch pvc pvc_name -p '{"metadata":{"finalizers":null}}

Saawaj
  • 41
  • 9