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

StateDescription
WaitingThis 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.
RunningIf 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.
TerminatedThe 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.

PhaseDescription
PendingThe 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.
RunningThe 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.
SucceededAll containers in the Pod have terminated successfully and will not be restarted.
FailedAll 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.
UnknownThe 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 AContainer BNoneAlwaysOnFailure
WaitingWaitingPendingPendingPending
WaitingRunningPendingPendingPending
WaitingNon-zero terminatedPendingPendingPending
WaitingZero terminatedPendingPendingPending
RunningRunningRunningRunningRunning
RunningNon-zero terminatedRunningRunningRunning
RunningZero terminatedRunningRunningRunning
Non-zero terminatedNon-zero terminatedFailedRunningRunning
Non-zero terminatedZero terminatedFailedRunningRunning
Zero terminatedZero terminatedSucceededRunningSucceeded

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