简体   繁体   English

我的集群中的 pod 可以获取服务中的 pod 列表吗?

[英]Can a pod within my cluster get a list of pods in a service?

Suppose I have a Service within my cluster called workers .假设我的集群中有一个名为workersService From kubectl , I can list the pods in that service using the selector the service uses:kubectl ,我可以使用服务使用的选择器列出该服务中的 pod:

kubectl get pods --selector app=worker

Can a pod within the cluster get a list of that service's pods?集群中的 pod 能否获取该服务的 pod 列表?

You can achieve it quite easily with Kubernetes Python Client library .您可以使用Kubernetes Python Client library轻松实现它。 The following code can be run locally as it uses your .kube/config file for authentication:以下代码可以在本地运行,因为它使用您的.kube/config文件进行身份验证:

from  kubernetes import client , config
config.load_kube_config()
v1 = client.CoreV1Api()
list_pods = v1.list_pod_for_all_namespaces(label_selector="app=worker")
for item in list_pods.items:
    print(item.metadata.name)

Before that you will need to install the mentioned library, which can be done with pip :在此之前,您需要安装上述库,可以使用pip完成:

pip3 install kubernetes

For production use I would recommend integrating it with your python docker image and of course instead of using .kube/config file, you will need to create a ServiceAccount for your client Pod so it can authenticate to kubernetes API . For production use I would recommend integrating it with your python docker image and of course instead of using .kube/config file, you will need to create a ServiceAccount for your client Pod so it can authenticate to kubernetes API . This is nicely described in this answer , provided by krelst .这在krelst提供的这个答案中得到了很好的描述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何提供对 pod 的访问,以便它可以列出和获取命名空间/集群中的其他 pod 和其他资源 - How to provide access to a pod so that it can list and get other pods and other resource in the namespaces/cluster ClusterIP 服务无法从集群 pod 中访问 - ClusterIP service Not accessible from within the cluster pods 具有默认 ServiceAccount 的 pod 可以在另一个集群中部署 pod 吗? - Can a pod with default ServiceAccount deploy pods in another cluster? 在不使用k8s服务的情况下将Pod从一个群集连接到另一个群集中的Pod - Connect Pods from one cluster to a pod in aother cluster without using k8s service 如何创建服务帐户以从 Kubernetes 集群中获取 pod 列表? - How to create a service account to get a list of pods from inside a Kubernetes cluster? Kubernetes:如何从一个 pod 中获取其他 pod 的名称? - Kubernetes: How to get other pods' name from within a pod? failed to list *v1.Pod: pods is disabled: 用户“system:serviceaccount:monitoring”无法在集群范围内的 API 组“”中列出资源“pods” - failed to list *v1.Pod: pods is forbidden: User "system:serviceaccount:monitoring" cannot list resource "pods" in API group "" at the cluster scope 从外部Pod到集群内部访问kubernetics服务 - Accessing kubernetics service from outside pod but within the cluster 在Kubernetes集群中无法获取Pod信息 - Can't get Pod info in the kubernetes cluster 如何获取未链接到任何服务的Pod列表 - How to get list of pods that are not linked to any service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM