Get Ready status using kubectl -o=jsonpath

6/13/2019

I was trying to get the Ready status of pods by using -o=jsonpath. To be more clear of what I want, I would like to get the value 1/1 of the following example using -o=jsonpath.

NAME       READY     STATUS    RESTARTS   AGE
some_pod   1/1       Running   1          34d

I have managed to get some information such as the pod name or namespace.

kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{end}'

And I get somthing like:

some_namespace1 pod_name1

However, I don't know how to get the Ready status. What I would like to have is an aoutput similar to this:

some_namespace1 pod_name1 1/1

I know I can use bash commands like cut:

kubectl get pods --all-namespaces| tail -1 | cut -d' ' -f8

However, I would like to get it by using kubectl

-- pcampana
kubernetes

2 Answers

6/13/2019

I think this isn't directly reported in the Kubernetes API.

If you kubectl get pod ... -o yaml (or -o json) you'll get back an object matching a List (not included in the API docs) where each item is a Pod in the Kubernetes API, and -o jsonpath values follow that object structure. In particular a PodStatus has a list of ContainerStatus, each of which may or may not be ready, but the API itself doesn't return the counts as first-class fields.

There are a couple of different JSONPath implementations. I think Kubernetes only supports the syntax in the Kubernetes documentation, which doesn't include any sort of "length" function. (The original JavaScript implementation and a ready Googlable Java implementation both seem to, with slightly different syntax.)

The best I can come up with playing with this is to report all of the individual container "ready" statuses

kubectl get pods \
  -o $'jsonpath={range .items[*]}{.metadata.name}\t{.status.containerStatuses[*].ready}\n{end}'

(

#x27;...' is bash/zsh syntax) but this still requires some post-processing to get back the original counts.

-- David Maze
Source: StackOverflow

6/13/2019

You can get all the pods status using the following command:

kubectl get pods -o jsonpath={.items[*].status.phase}

Similar commands you can use for the name

kubectl get pods -o jsonpath={.items[*].metadata.name}

EDIT:

You need to compare the .status.replicas and .status.readyReplicas to get how many ready replicas are there.

-- Prafull Ladha
Source: StackOverflow