Kubernetes
Pods
restartPolicy
Error Handling
Troubleshooting

restartPolicy Unsupported value Never supported values Always

System Design practice on Codemia

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

Practice system design

In Kubernetes, restartPolicy is a critical field in Pod configuration that defines how a pod should be restarted when a container in the pod crashes or completes. The restart policy has implications for the pod's lifecycle and the overall application's availability and resilience. However, there might be some confusion around the values that restartPolicy can take, as evidenced by the message: Unsupported value: "Never": supported values: "Always". This article will explore the technicalities of restartPolicy, particularly focusing on this message and the best practices around setting this field.

Understanding restartPolicy

The restartPolicy in Kubernetes specifies how the container's lifecycle is managed. It defines the conditions under which containers should be restarted if they exit.

Supported Values

Kubernetes defines three possible values for the restartPolicy:

  • Always: The default policy, which restarts the container whenever it exits, regardless of the exit status.
  • OnFailure: Restarts the container only if it exits with a non-zero status, indicating failure.
  • Never: Does not restart the container regardless of its exit status.

These options are typically set in the pod's specification section of a Kubernetes manifest file.

Configuration Example

Here is a YAML snippet illustrating how to configure the restartPolicy:

  • name: example-container
  • Pods Managed by Controllers: When using a higher-level controller that manages pods, like a Deployment, ReplicaSet, or StatefulSet, the restart policy must be set to Always. This is because these controllers are designed to provide self-healing capabilities by maintaining a specified number of running replicas. Thus, they inherently expect the pods to be restarted upon failure to maintain the desired state.
  • Pod-Only Usage: On the other hand, restartPolicy: Never or OnFailure can be applied to standalone pods, not managed by higher-level controllers, typically used for batch jobs or scripts that must run to completion.
  • Error Mitigation: If encountering the error message, ensure that restartPolicy is correctly set to Always within controllers like Deployments or ReplicaSets.
  • Pod Monitoring: Utilize observability tools integrated with Kubernetes, such as Prometheus and Grafana, to watch container states and events for better insights on restart occurrences.
  • Container Health Checks: Implement readiness and liveness probes inside your container definitions to preemptively handle failures before a restart policy is needed.

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.