How to restart a failed pod in kubernetes deployment
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Pod Failures in Kubernetes
In Kubernetes, a pod is the smallest deployable unit and can encompass one or more containers. When a pod in a deployment fails, it may need manual intervention to ensure resilience and service continuity. This article elucidates how to handle restarting failed pods step by step.
Why Pods Might Fail
Pods might fail in Kubernetes due to several reasons such as:
- Resource Constraints: The pod lacks adequate CPU or memory.
- Node Failures: The node hosting the pod is down.
- Configuration Errors: Incorrect environment variables or configurations.
- Application Errors: Issues within the application itself, such as crashes or exceptions.
- Network Issues: Disconnections or misconfigured services.
Understanding the cause is essential to address the failure appropriately.
Restart Policies
Kubernetes supports several restart policies that define how the system should respond to pod failures:
- Always: A new pod instance is always created after one gets terminated, regardless of its termination status.
- OnFailure: A new pod instance is created only if a pod terminates with an error (non-zero exit code).
- Never: Pods not restarted after termination, used for debugging purposes.
The default policy for pods in a Deployment is Always.
Steps to Restart a Failed Pod
Step 1: Identify the Pod Failure
The initial step is to identify the failed pod. You can use kubectl get pods to list all pods and their status:
To get detailed information about a failed pod, utilize:
Step 2: Debug the Pod Problem
Check the pod logs to understand why it failed. Use the following command:
For more comprehensive insights, especially in complex applications with multiple containers:
Step 3: Manually Restart the Pod
If debugging indicates a need for manual intervention, you can restart the pod simply by deleting it. Kubernetes Deployments manage the pod lifecycle and will automatically recreate it.
Since you're using a Deployment, Kubernetes will create a new pod instance as the desired state is maintained.
Step 4: Modify the Deployment (if necessary)
In some cases, you might need to update the deployment configuration, such as environmental variables or image versions. Edit the deployment with:
After changes are applied, Kubernetes deploys a new set of pods reflecting these modifications.
Additional Strategies for Pod Resilience
1. Resource Requests and Limits:
Set resource requests and limits for CPU and memory resources to ensure pods have the necessary resources and stay within defined constraints:
2. Readiness and Liveness Probes:
Implement liveness and readiness probes for better orchestration and management of pod lifecycle:
3. Horizontal Pod Autoscaling (HPA):
For workloads that require dynamic scaling, consider using HPA to increase or decrease the number of pod replicas based on demand:
Summary Table
Here is a quick reference table summarizing the key points:
| Aspect | Description |
| Pod Failure Causes | Resource constraints Configuration errors Application errors Node failures Network issues |
| Restart Policies | Always OnFailure Never |
| Commands for Debug | kubectl describe pod [pod-name]
kubectl logs [pod-name] |
| Manual Restart | kubectl delete pod [pod-name] |
| Enhancements | Resource limits Probes Autoscaling |
Arming yourself with these strategies ensures robust handling of failed pods within Kubernetes, enhancing the stability and reliability of your applications deployed in a Kubernetes environment.
Related reading
- How to Restart Kubernetes deployment using API Server
- How to restart kubernetes nodes?
- How to retrieve the pod/container in which run a given process
- How to retry image pull in a kubernetes Pods?
- How to restart apache2 without terminating docker container?
- How to restart kafka server properly?
- How to restart a SimpleMessageListenerContainer
- How to restart RabbitMQ service

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.