Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 227
fascynacja Avatar asked Nov 22 '19 10:11

fascynacja


People also ask

How do you know when pods are ready?

If the output from a specific pod is desired, run the command kubectl describe pod pod_name --namespace kube-system . The Status field should be "Running" - any other status will indicate issues with the environment. In the Conditions section, the Ready field should indicate "True".

Which command gets list of pods?

To list one or more pods, replication controllers, services, or daemon sets, use the kubectl get command.


Video Answer


3 Answers

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

like image 66
P.... Avatar answered Oct 24 '22 16:10

P....


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

like image 29
Muhammad Abdul Raheem Avatar answered Oct 24 '22 17:10

Muhammad Abdul Raheem


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'
like image 4
Vit Avatar answered Oct 24 '22 17:10

Vit