Kubernetes: pulling out the ready status of individual containers using kubectl

kubectl will give you a sythesized column showing how many container instances in a pod are READY with the default ‘get pods’ command.  But if you are dealing with json output and need this information, then you can extract it using jsonpath or jq.

Here is an example output from ‘get pods’ showing the READY state for a pod with a single container (1/1), as well as a pod with a sidecar (2/2).

$ kubectl get pods 
NAME                  READY STATUS  RESTARTS AGE
tiny-tools-ds-mpfdw   1/1   Running 0        21h
nginx-96f455bf9-dzvzv 2/2   Running 0        4m12s

Here is this same information pulled using jsonpath.  Notice the nginx pod has two status values corresponding to each of its containers.

# use jsonpath to pull out container status ready state
$ kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{" "}{.status.containerStatuses[*].ready}{"\n"}{end}'

tiny-tools-ds-mpfdw true
nginx-96f455bf9-pckpn true true

Using “{range .items[*]}” and “{end}” allows the output of a pod to be single-lined.

If you wanted to pull this information as json with arrays, then you could use jq like below.

$ kubectl get pods -o=jsonpath='{.items[*]}' | jq -r '[.metadata.name, [.status.containerStatuses[].ready] ]'
[
  "tiny-tools-ds-mpfdw",
  [
    true
  ]
]
[
  "nginx-96f455bf9-pckpn",
  [
    true,
    true
  ]
]

And if you preferred dictionaries, you could use jq like below.

$ kubectl get pods -o=jsonpath='{.items[*]}' | jq -r '{ name: .metadata.name, status: [.status.containerStatuses[].ready] }'
{
  "name": "tiny-tools-ds-mpfdw",
  "status": [
    true
  ]
}
{
  "name": "nginx-96f455bf9-pckpn",
  "status": [
    true,
    true
  ]
}