How to get list of pods which are "ready"?

11/22/2019

I am using kubectl in order to retrieve a list of pods:

 kubectl get pods --selector=artifact=boot-example -n my-sandbox  

The results which I am getting are:

NAME                           READY   STATUS    RESTARTS   AGE
boot-example-757c4c6d9c-kk7mg   0/1     Running   0          77m
boot-example-7dd6cd8d49-d46xs   1/1     Running   0          84m
boot-example-7dd6cd8d49-sktf8   1/1     Running   0          88m

I would like to get only those pods which are "ready" (passed readinessProbe). Is there any kubectl command which returns only "ready" pods? If not kubectl command, then maybe some other way?

-- fascynacja
kubectl
kubernetes
kubernetes-pod

2 Answers

11/22/2019

You can use this command:

kubectl -n your-namespace get pods -o custom-columns=NAMESPACE:metadata.namespace,POD:metadata.name,PodIP:status.podIP,READY-true:status.containerStatuses[*].ready | grep true

This will return you the pods with containers that are "ready".

To do this without grep, you can use the following commands:

kubectl -n your-namespace get pods -o go-template='{{range $index, $element := .items}}{{range .status.containerStatuses}}{{if .ready}}{{$element.metadata.name}}{{"\n"}}{{end}}{{end}}{{end}}'

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

This will return you the pod names that are "ready"

-- Muhammad Abdul Raheem
Source: StackOverflow

11/22/2019

You can try this command which uses jq to transform kubectl json output as you require.

kubectl get pods --all-namespaces -o json  | jq -r '.items[] | select(.status.phase = "Ready" or ([ .status.conditions[] | select(.type == "Ready") ] | length ) == 1 ) | .metadata.namespace + "/" + .metadata.name'
-- VKR
Source: StackOverflow