How to get list of pods which are “ready”?

我只是一个虾纸丫 提交于 2020-08-19 16:55:44

问题


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?


回答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"




回答2:


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'


来源:https://stackoverflow.com/questions/58992774/how-to-get-list-of-pods-which-are-ready

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!