如何在每个 Pod 中获取所有 Kubernetes Pod 的 IP?

huangapple go评论59阅读模式
英文:

How to get all Kubernetes pod IP in each pods?

问题

我们有一个 Kubernetes 集群,其中每个服务运行着 3 个 Pod。
我们有一个使用情况,我希望每个 Pod 都知道其他 Pod 的 IP 地址。

例如:
P1 --> 这个 Pod 应该知道自己以及 P2、P3 Pod 的 IP 地址
P2 --> 这个 Pod 应该知道自己以及 P1、P3 Pod 的 IP 地址
P3 --> 这个 Pod 应该知道自己以及 P2、P3 Pod 的 IP 地址


我在 Google 上找到了以下内容,但似乎我们只能获得 Pod 本身的 IP 地址,无法获取其他两个正在运行的 Pod 的 IP 地址。

- name: POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP

是否有办法在我的 Java 代码中获取这些信息?

英文:

We have Kubernetes cluster where each service has 3 pod running.
we got a use case where i want each pod knows the ip of other pods.

For Example:
P1 --> this pod should know ip of itself, p2, p3 pod ip's 
P2 --> this pod should know ip of itself, p1, p3 pod ip's 
p3 --> this pod should know ip of itself, p2, p3 pod ip's 

I found this on google but looks like we only get IP of pod itself it won't give the ip of other two pod running.

          - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP

Is there a way i can get this info in my java code?

答案1

得分: 6

使用如你所提到的 env 可能不足够,因为 Pod 应该是临时资源,会被添加和删除。

但是下面的命令可以获取这些值:

$kubectl get pods -o=jsonpath="{range .items[*]}{.status.podIP}{','}{end}"

你没有提到这些 Pod 是否属于同一个服务,但如果是这种情况(我认为是这样),你可以使用 Selectors

$kubectl get pods -l="app=my-service" -o=jsonpath="{range .items[*]}{.status.podIP}{','}{end}"
x.y.z.a,b.c.d.e,

这里的两个 IP 分别是 x.y.z.a 和 b.c.d.e。

在相应的语言(比如 Java)API 库中应该能找到等效的方法。

如果你可以接受请求发送到任何一个 Pod,你可以查看 Kube DNS

英文:

Using env as you mentioned might not suffice as pods are supposed to be temporary resources and get added and deleted.

But below command can fetch you the values

$kubectl get pods -o=jsonpath="{range .items[*]}{.status.podIP}{','}{end}"

You did not mention if the pods belong to the same service but if that is the case(which I think), you could use Selectors

$kubectl get pods -l="app=my-service" -o=jsonpath="{range .items[*]}{.status.podIP}{','}{end}"
x.y.z.a,b.c.d.e,

Here the two IPs are x.y.z.a and b.c.d.e

You should be able to find equivalent in the corresponding language(java) api libraries

If you are okay to be able to let the request go to any pod, you can look at Kube DNS

答案2

得分: 1

你可以获取终端的子集以获取所有的 Pod IP。终端的名称与您的服务名称相同。
使用 kubectl get ep ep-name -n your-ns -o yaml 了解详细信息。

对于 Java 代码,您可以使用 Kubernetes Java 客户端(官方链接 / fabric8 库),或通过 k8s API 进行访问。


更新:

kubectl get ep ep-name -n your-ns -o jsonpath='range .subsets[*]}{range .addresses[*]}{.ip}{ "\t" }{end}'

可以获取所有 Pod 的 IP。使用 fabric8 库,您可以参考这个 示例

Endpoints endpoints = client.endpoints().inNamespace(namespace).withName("ep-name").get();
endpoints.getSubsets().getAddresses().forEach(endpointAddress -> log.info(endpointAddress.getIp()));

如果您的控制器是 StatefulSet,则可以使用无头服务与其他 StatefulSet Pod 进行连接。

英文:

You can get the subsets of the endpoint to get all of pods IP. The endpoint has the same name as you svc.
Using kubectl get ep ep-name -n your-ns -o yaml for details.

For Java code, you can use k8s java client(Officail/fabric lib) or access by k8s API.


Update:

kubectl get ep ep-name -n your-ns -o jsonpath='{range .subsets[*]}{range .addresses[*]}{.ip}{"\t"}{end}'

can get all pods IP. Using fabric8 lib you can reference this example

Endpoints endpoints = client.endpoints().inNamespace(namespace).withName("ep-name").get();
endpoints.getSubsets().getAddresses().forEach(endpointAddress -> log.info(endpointAddress.getIp()));

If your controller is StatefulSet, can use headless service to connect with other StatefulSet pods.

答案3

得分: 0

如果您正在寻找一个针对 Pod 副本具有确定性目标地址的解决方案,我可能会选择使用部署了无头服务的 StatefulSet。
这样的设置将为您提供类似于以下格式的集群内 DNS 地址:
pod-[1,2,3].svc-name.default.cluster.local
然后,您可以使用这些 DNS 地址使 Pods 彼此通信。链接:https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id

英文:

If you're looking for a solution that has deterministic destination address for a replica of pods, I'd probably go with a statefulset deployed with a headless service.
Such a setup will give you intra cluster DNS address like
pod-[1,2,3].svc-name.default.cluster.local
You could then use these DNS addresses to make pods talk to each other. https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id

答案4

得分: 0

Sure, here's the translation:

如果您需要有关Pod的大量信息...
kubectl get pods -n <命名空间> --output=wide
包括IP、状态、主机节点等。

英文:

In case you need lots of info on the pods...
kubectl get pods -n &lt;namespace&gt; --output=wide
Including IP, status, host node, etc.

huangapple
  • 本文由 发表于 2020年9月22日 17:15:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64006641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定