Pod Lifecycle
The kubelet accepts a set of PodSpecs provided through various mechanisms (primarily via the apiserver) and ensures that the containers described in those PodSpecs are running in a healthy state.
This post covers container states in Kubernetes, Pod phases, and the relationship between the two.
Container States
Containers have 3 states: Waiting, Running and Terminated
| State | Description |
|---|---|
| Waiting | This is the default state of a container. If a container is neither in the Running nor Terminated state, it must be in the Waiting state. To provide more information, the reason and related details for being in this state are displayed alongside it. |
| Running | If a container is in the Running state, it means the container is operating normally without any issues. Once a container enters the Running state, the postStart hook is executed if one exists. The time at which the container entered this state is also shown. |
| Terminated | The container has finished executing its tasks and has stopped running. Before the container exits, the preStop hook is executed if one exists. The reason for entering this state and the exit code are also shown. |
Pod Phases
A Pod phase is a simple summary of where the Pod is in its lifecycle.
The meaning and number of Pod phases are strictly defined. There are currently 5 phases in total, as shown below.
| Phase | Description |
|---|---|
| Pending | The Pod has been accepted by the Kubernetes system, but one or more container images have not yet been created. The wait time includes the time to schedule the Pod and the time to download images over the network, which may take a while. |
| Running | The Pod has been bound to a node, all containers in the Pod have been created, and at least one container is running or is in the process of starting or restarting. |
| Succeeded | All containers in the Pod have terminated successfully and will not be restarted. |
| Failed | All containers in the Pod have terminated, and at least one container terminated due to failure. That is, the container exited with a non-zero status or was terminated by the system. |
| Unknown | The state of the Pod cannot be obtained for some reason, typically because communication with the host where the Pod resides has failed. |
Relationship Between Container States and Pod Phases
The following example illustrates the relationship between container states, Pod phases, and Pod restart policies. The first two columns show the states of two containers within a Pod, and the last three columns show the Pod’s status when restartPolicy is set to None, Always, or OnFailure respectively.
| Container A | Container B | None | Always | OnFailure |
|---|---|---|---|---|
| Waiting | Waiting | Pending | Pending | Pending |
| Waiting | Running | Pending | Pending | Pending |
| Waiting | Non-zero terminated | Pending | Pending | Pending |
| Waiting | Zero terminated | Pending | Pending | Pending |
| Running | Running | Running | Running | Running |
| Running | Non-zero terminated | Running | Running | Running |
| Running | Zero terminated | Running | Running | Running |
| Non-zero terminated | Non-zero terminated | Failed | Running | Running |
| Non-zero terminated | Zero terminated | Failed | Running | Running |
| Zero terminated | Zero terminated | Succeeded | Running | Succeeded |
Here is the manifest I used for testing — feel free to try it yourself: https://github.com/chengqing-su/kubernetes-learning/tree/master/pod-lifecycle