When a pod gets stuck in the “Terminating” state in Kubernetes, it is not being removed from the cluster as expected. There could be several reasons, including issues with the underlying node, problems with the container runtime, or misconfigurations in the pod’s specifications.
How to fix “Pod Stuck in Terminating State”
To fix the pod stuck in the “Terminating” state,
- You might need to delete the pod stuck in a terminating state forcefully using this command.
kubectl delete pod <PODNAME> --grace-period=0 --force --namespace <NAMESPACE>
You can replace it with the name of the pod that is stuck in the Terminating state. If the pod is in a specific namespace, you can add the -n flag to the command to specify the namespace.
- Check pod details using this command.
kubectl describe pod <pod_name> -n <namespace>
- Check the node status using this command.
kubectl get nodes
This command is useful to check if the node where the pod is running has any issues. If the node is in a “NotReady” state or experiencing issues, it might not be able to gracefully terminate the pod.
- Check the logs using this command.
kubectl logs <pod_name> -n <namespace>
This command checks the logs of the containers in the pod. Look for any error messages or issues that could be causing the pod to be stuck in the “Terminating” state.
- Look for container statuses in the pod description to see if any containers are not terminating as expected. Sometimes, a container might be stuck due to a long-running process or a misconfigured preStop hook.
After you are done, verify that the stuck pod is now removed. For example, run this command:
kubectl get pods --all-namespaces
The kubectl get pods –all-namespaces command lists all the pods running in your Kubernetes cluster across all namespaces. This command helps you get an overview of the pods and their states throughout the cluster.
This command will output a table with columns for the namespace, pod name, the number of ready containers vs. the total number of containers in the pod, status, restart count, and the age of the pod. The output will look something like this.
NAMESPACE NAME READY STATUS RESTARTS AGE kube-system coredns-7c4d89b4d9-9m5fz 1/1 Running 0 1d kube-system coredns-7c4d89b4d9-rt7hf 1/1 Running 0 1d kube-system etcd-minikube 1/1 Running 0 1d kube-system kube-apiserver-minikube 1/1 Running 0 1d kube-system kube-controller-manager-minikube 1/1 Running 0 1d kube-system kube-proxy-cb7cc 1/1 Running 0 1d kube-system kube-scheduler-minikube 1/1 Running 0 1d
I hope this resolves your error.