简体   繁体   中英

How to list names of all pods serving traffic behind a service in kubernetes

我想列出所有实际在 kubernetes 服务后面提供流量的 pod 的名称。我的问题是如何通过执行单个 kubectl 命令来实现这一点。

There are two ways to list the pods behind a service.

The easier way but with two commands

Find the selector by running the below command

kubectl get services -o=wide

Output:

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
hello-world-service   ClusterIP   172.21.xxx.xx   <none>        80/TCP    13m   run=nginx

Pass the selector to the command below

kubectl get pods --selector=run=nginx -o=name

To see the exact pod names without pod/

kubectl get pods --selector=run=nginx  --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

In a single command but using the endpoints information for the service hello-world-service

kubectl get endpoints hello-world-service -o=jsonpath='{.subsets[*].addresses[*].ip}' | tr ' ' '\n' | kubectl get pods --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

这个命令有效

kubectl get ep servicename -o=jsonpath='{.subsets[*].addresses[*].ip}' | tr ' ' '\n' | xargs -I % kubectl get pods -o=name --field-selector=status.podIP=%

This should work, "-o=name" is for displaying only pod name.

kubectl get pods -o=name --all-namespaces | grep {service-name}

Replace {service-name} with your service name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM