DEV Community

Cover image for Kubernetes Pod/Container states
dejanualex
dejanualex

Posted on

Kubernetes Pod/Container states

The consideration for writing this post is this stackoverflow question How to get the status of a particular pod or container: kubectl get pods using jsonpath which even if is 5 years old it's still relevant today (32K hits).

kubectl gives you a powerful means to communicate with Kubernetes cluster's control plane. It's pretty handy when it comes to checking/debugging the status of your app...but it's very important to understand the basics

Pods follow a defined lifecycle, starting in the Pending phase, moving through Running if at least one of its primary containers starts OK, and then through either the Succeeded or Failed.

You can check a Pod's status (which is a PodStatus object) using:

# status for pods in all namespaces
kubectl get po -A -o jsonpath='{.items[*].status}'
Enter fullscreen mode Exit fullscreen mode

Within a POD's status you can find a phase field, which can have one of the following values:

  • Pending = the Pod has been accepted by the cluster, but one or more container has not been set up and ready to run
  • Running = all container have been created and at least one container is running in the pod
  • Succeeded = all containers in the Pod have terminated successfully
  • Failed = all containers in the Pod have terminated and at leas one container exited with non-zero status
  • Unknow = for some reason the Pod's state could not be obtained.

Furthermore you can check the PODS which Phase is different than Running:

kubectl get po -A --field-selector=status.phase!=Running
Enter fullscreen mode Exit fullscreen mode

When a Pod is being deleted, it can be shown as Terminating by some kubectl commands. This Terminating status is not one of the Pod phases.

The kubelet is able to restart containers, so Kubernetes tracks the container states within the Pod, which can be one of the following:

  • Waiting = container still running the operations needed to complete start up
  • Running = container is executing without issues.
  • Terminated = container began execution and then either ran to completion or failed for some reason

You can check the container states using the following:

kubectl get po -A -o jsonpath="{.items[*].status.containerStatuses[*].state}"
Enter fullscreen mode Exit fullscreen mode

Image description

Important notes, the output of some of the kubectl commands can be rather verbose so you can pipe it to jq.

jq command is useful for transformations that are too complex for jsonpath, it can be found at https://stedolan.github.io/jq/

Top comments (0)