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?
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"
This is a solution without any external tools besides kubectl to return you the "Ready" field with other data of the pod. You can use grep to filter only the pods with "Ready" as "true"
This works for me. Anyhow I still will wait for an answer which uses pure kubectl (without grep)
I have edited the answer to provide a solution without using grep as well. The new command will return you the pod names that are "ready".
later versions of kubectl can now just do this - kubectl get pods --field-selector=status.phase=Running
@nrjohnstone, status.phase=Running doesn't mean pod ready.