Kubernetes
Pods
Troubleshooting
Terminating
Container Issues

Pods stuck in Terminating status

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

When working with Kubernetes, one of the common issues operators and developers might encounter is Pods being stuck in the "Terminating" state. This situation often arises during the management or scaling down of Kubernetes applications and can be due to several reasons rooted in both the Kubernetes architecture and the specific application behavior. Understanding and troubleshooting these issues is crucial for maintaining the stability and reliability of applications deployed on Kubernetes.

Technical Background

In Kubernetes, a Pod represents one or more containers. These containers are the smallest deployable units of computing that can be created and managed in Kubernetes. When a Pod is no longer needed, it is scheduled for deletion, entering the Terminating state. During this state, Kubernetes performs a series of operations to gracefully shut down all the Pod's containers and release any associated resources.

The lifecycle of a Pod is managed by the Kubernetes Control Plane, which orders deletions via the Kubernetes API server. Upon receiving a delete request, the cluster attempts to clean up resources associated with the Pod. Several factors can result in Pods being stuck in the Terminating state, and understanding these factors is essential for resolving the issue.

Common Causes of Stuck Pods

Below are some common causes for Pods getting stuck in the Terminating state, along with technical explanations of each:

  1. Finalizers:
    • Finalizers are used to ensure that specific cleanup actions occur before the deletion of a Kubernetes object. If a finalizer is not removed, the Pod will remain in the Terminating state.
    • Solution: Inspect the Pod's metadata to check for finalizers. You can use the kubectl patch command to remove problematic finalizers.
  2. Grace Period:
    • By default, Kubernetes provides a 30-second grace period before forcefully deleting containers. This allows applications to clean up tasks. If processes do not terminate within this period, the Pod may remain stuck.
    • Solution: Manually delete the Pod using kubectl delete pod <pod-name> --grace-period=0 --force to override the graceful shutdown.
  3. Network and Storage Dependencies:
    • Pods might be waiting for network or storage resources to detach or decommission. If these operations don't complete successfully, the Pod can be stuck.
    • Solution: Check and ensure all associated network and storage resources are orderly released.
  4. Pending Node Issues:
    • If the node hosting the Pod is not responding or has network issues, the Pod could be stuck.
    • Solution: Perform a kubectl get node <node-name> to verify node status and address any issues accordingly.
  5. Application-Specific Cleanup:
    • Some applications have custom scripts or procedures to follow during termination that may not complete successfully.
    • Solution: Review application logs and code to ensure all shutdown procedures are correctly implemented and observe the logs for anomalies during termination.

Troubleshooting Steps

When addressing Pods in the Terminating state, consider the following steps as part of your troubleshooting regimen:

1. Check Pod Status and Logs

Inspect the Pod status and review logs to gather initial troubleshooting data:

bash
kubectl describe pod <pod-name>
kubectl logs <pod-name>

2. Examine Finalizers

Check for and clean up any finalizers that may be preventing deletion:

bash
kubectl get pod <pod-name> -o json | jq '.metadata.finalizers'

3. Force Deletion

Forcefully terminate the Pod if it's safe to do so:

bash
kubectl delete pod <pod-name> --grace-period=0 --force

4. Investigate Node and Resource Status

Ensure the node is in a healthy state and check resource bindings:

bash
kubectl get node <node-name>
kubectl get pvc
kubectl get svc

Summary Table

IssueDescriptionSolution
FinalizersObjects preventing deletion until custom resources are cleaned up.Inspect finalizers and remove faulty ones using kubectl patch.
Grace Period ExceedingDefault 30-second period for graceful shutdown exceeded without container exit.Override with kubectl delete pod <pod-name> --grace-period=0 --force.
Network/Storage DependencyWaiting for network or storage resources deallocationVerify all resource detachments are successful and complete.
Pending Node IssuesNon-responsive nodes can leave pods in terminating state.Check node health and network status with kubectl get node.
Application-Specific CleanupCustom shutdown procedures in applications not completing correctly.Investigate application logs and code to ensure proper handling of termination signals.

Additional Considerations

  • API Server and Controller Manager: Ensure that the Kubernetes API server and controller manager are running healthy, as they govern the deletion and cleanup processes.
  • Cluster Configuration: Review the cluster’s event logs for unusual activities or misconfigurations that might affect Pod deletion.

Understanding and resolving issues with Pods stuck in the Terminating state requires a comprehensive approach, examining not only Kubernetes itself but also the application lifecycle and interaction with underlying resources. By following structured troubleshooting steps, leveraging Kubernetes tools, and maintaining insights into application behavior, one can ensure smooth operation and rapid resolution of these issues.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.