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

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?



Solution 1:[1]

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".

Solution 2:[2]

Generic answer for all resource type which prints READY state when queried using kubectl get <resource-name> command.

kubectl get pod |grep -P '\s+([1-9]+[\d]*)\/\1\s+'

Example:

kubectl get pod
NAME   READY  STATUS     RESTARTS  AGE
app_1   1/1    Running    0         77m
app_2   1/1    Running    0         77m
app_3   0/1    Completed  0         77m
app_4   1/1    Running    0         77m
app_5   8/8    Running    0         77m
app_6   4/4    Running    1         77m
app_7   1/1    Running    0         77m
app_8   1/1    Running    0         77m
app_9   1/1    Running    0         77m
app_10  1/1    Running    0         77m
app_11  1/1    Running    0         77m
app_12  1/1    Running    0         77m
app_13  1/1    Running    0         75m
app_14  2/2    Running    0         77m
app_15  2/2    Running    0         77m
app_16  2/2    Running    0         76m
app_17  4/8    Running    0         77m
app_18  1/1    Running    0         77m
app_19  1/1    Running    0         77m
app_20  1/1    Running    0         77m
app_21  1/1    Running    0         77m
app_22  2/2    Running    0         77m
app_23  3/3    Running    0         77m
app_24  1/1    Running    0         77m
app_25  1/1    Running    0         77m
app_26  1/1    Running    0         77m
app_27  1/1    Running    0         77m
app_28  2/2    Running    0         77m

Sample output:

kubectl get pod| grep -P '\s+([1-9]+)\/\1\s+'
app_1   1/1    Running    0         77m
app_2   1/1    Running    0         77m
app_4   1/1    Running    0         77m
app_5   8/8    Running    0         77m
app_6   4/4    Running    1         77m
app_7   1/1    Running    0         77m
app_8   1/1    Running    0         77m
app_9   1/1    Running    0         77m
app_10  1/1    Running    0         77m
app_11  1/1    Running    0         77m
app_12  1/1    Running    0         77m
app_13  1/1    Running    0         75m
app_14  2/2    Running    0         77m
app_15  2/2    Running    0         77m
app_16  2/2    Running    0         76m
app_18  1/1    Running    0         77m
app_19  1/1    Running    0         77m
app_20  1/1    Running    0         77m
app_21  1/1    Running    0         77m
app_22  2/2    Running    0         77m
app_23  3/3    Running    0         77m
app_24  1/1    Running    0         77m
app_25  1/1    Running    0         77m
app_26  1/1    Running    0         77m
app_27  1/1    Running    0         77m
app_28  2/2    Running    0         77m

To print resources not in ready state:

kubectl get pod |grep -Pv '\s+([1-9]+[\d]*)\/\1\s+'
NAME   READY  STATUS     RESTARTS  AGE
app_3   0/1    Completed  0         77m
app_17  4/8    Running    0         77m

Those interested in the grep command, recommending to read the concept of "capture groups" and "back referencing" in regular expressions. However, a brief description is added here.

\s+([1-9]+)\/\1\s+

Explanation:

\s matches any whitespace character + matches the previous token between one and unlimited times, as many times as possible

1st Capturing Group ([1-9]+)

Match a single character present in the list below [1-9] + matches the previous token between one and unlimited times, as many times as possible 1-9 matches a single character in the range between 1 and 9 \/ matches the character / literally \1 matches the same text as most recently matched by the 1st capturing group, which in this case is [1-9]+

\s matches any whitespace character + matches the previous token between one and unlimit

Solution 3:[3]

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'

Solution 4:[4]

for i in $(k get po -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}')
    do
        if [[ $(kubectl  get po $i -o jsonpath='{.status.containerStatuses[*].ready}') == 'true'  ]]
            then 
                echo $i
        fi
    done

Solution 5:[5]

You could simply use the field-selector option from the native kubectl CLI to filter out non-running pods:

kubectl get pods --field-selector status.phase=Running

By using the native CLI you can use the custom column filter as part of the same single command for additional output customization:

kubectl get pods --field-selector status.phase=Running --no-headers -o custom-columns=":metadata.name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 030
Solution 2
Solution 3 Muhammad Abdul Raheem
Solution 4 eng.mrgh
Solution 5 jgoelten