fascynacja
fascynacja

Reputation: 2816

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?

Upvotes: 17

Views: 30272

Answers (10)

itd-fsc
itd-fsc

Reputation: 1

I ended up using:

kubectl get pods -n $NAMESPACE --field-selector=status.phase=Running -l foo=bar -o json | jq -r 'first(.items[] | select(all(.status.containerStatuses[]; .ready == true)) | .metadata.name)'

(Yes, I know that .ready == true is the same as .ready, but it felt better readable to me)

This solution uses --field-selector=status.phase=Running to ignore pods in status completed/error/evicted and then selects the first pod that has all containers in status ready.

The only limitation this has, is that it also select Terminating pods that are still ready.

Upvotes: 0

Evans Tucker
Evans Tucker

Reputation: 461

To get a list of pods that are ready, run this:

kubectl get pods -A -o json | jq -r '.items[] | select(all(.status.containerStatuses[]; .ready == true)) | "\(.metadata.namespace) \(.metadata.name)"' | column -t

Using status.phase==Running as suggested by some of the other answers doesn't work, because pods in the Running phase aren't necessarily Ready. For example, pods in CrashLoopBackoff are still in the Running phase...

Here's the breakdown of the solution above:

Get all pods in all namespaces, output in JSON format:

kubectl get pods -A -o json

Pipe to jq where we look at the status of every container in all pods, because pods can have multiple containers. Then we select only pods where all containers are .ready == true, and output the namespace and name of the pod.

| jq -r '.items[] | select(all(.status.containerStatuses[]; .ready == true)) | "\(.metadata.namespace) \(.metadata.name)"'

Pipe through column -t to make the output nicer to read - similar to the output of kubectl get pods -A:

| column -t

Conversely, to get all pods that are not ready, you can run this:

kubectl get pods -A -o json | jq -r '.items[] | select(.status.containerStatuses[].ready == false) | "\(.metadata.namespace) \(.metadata.name)"' | sort -u | column -t

In this code, we are selecting all pods where any container is still .ready == false. Because pods can have multiple containers, and more than one of those containers could be unready, the output could contain the same pod name multiple times, so we pipe it through sort -u to remove all duplicates.

Upvotes: 0

stewartshea
stewartshea

Reputation: 378

@sjethvani

If you're willing to use jq... you can do FAR more interesting things with much better cross-platform support for the command. Something like this is much less limited than something like jsonpath

kubectl get pods -n online-boutique --field-selector=status.phase==Running -o=json | jq -r '.items[] | "---", "pod_name: " + .metadata.name, "containers:", (.spec.containers[] | "- container_name: " + .name, "  image_path: " + (.image | split(":")[0]), "  image_tag: " + (.image | split(":")[1])), "---"'

sample

---
pod_name: adservice-795589cf6f-b48ln
containers:
- container_name: server
  image_path: gcr.io/google-samples/microservices-demo/adservice
  image_tag: v0.5.1
---

Upvotes: 0

sjethvani
sjethvani

Reputation: 538

After reading answers posted here & using some of them as reference (so my solution is sort of derived from others answers), here is what I am using to figure out fully ready pods (i.e running (status.phase=Running) pods having all of its containers in ready state , (i.e containerStatuses[*].ready=true))

kubectl get pods -l app="myservice-service" --field-selector status.phase=Running -o 'custom-columns="NAME":.metadata.name,"READY":.status.containerStatuses[*].ready,"STATUS":.status.phase' | awk '$2 !~ /.*false.*/'
NAME                                   READY       STATUS
myservice-service-74dc69d4-7t662   true,true   Running
myservice-service-74dc69d4-d4mtz   true,true   Running
  • awk to remove pods having one or more nonready (i.e ready=false) containers
  • --field-selector for getting only Running pods
  • custom-columns for printing custom columns , important one is ready column

Upvotes: 2

user3999721
user3999721

Reputation:

This simple approach worked good for me.
This will list only completely Ready pods.

kubectl get pods <options> | grep -P "(\d+)\/\1\s+Running"

This will check both the conditions.

  1. STATUS = Running
  2. All containers in Pod also ready

Explanation of reg-ex:
(\d+) number one or more occurrence and parenthesis indicates 1st set
\/ followed by a slash character
\1 match again with the same value of first set
\s+ one or more spaces
Running the literal word "Running".

This will ensure that READY column have, both sides same number like 1/1 or 2/2 etc
and next status column should be equal to Running

Upvotes: 0

jgoelten
jgoelten

Reputation: 11

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

Upvotes: 0

P....
P....

Reputation: 18351

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

Upvotes: 9

Muhammad Abdul Raheem
Muhammad Abdul Raheem

Reputation: 1980

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

Upvotes: 25

pythondev
pythondev

Reputation: 9

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

Upvotes: 0

Vit
Vit

Reputation: 8411

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'

Upvotes: 6

Related Questions