简体   繁体   English

从内部运行的应用程序读取k8s pod的IP

[英]Read IP of k8s pod from application running inside it

I have two k8s pods running. 我有两个运行的k8s吊舱。 Each pod has a single container inside each of them. 每个吊舱内部都有一个容器。 My app will be running inside each container. 我的应用程序将在每个容器中运行。 I want to make a direct HTTP call from my app in Pod1 to my app in Pod2. 我想直接从Pod1中的应用程序到Pod2中的应用程序进行HTTP调用。

Assume that I have some sort of persistent storage held outside the clusters. 假设我在集群外部拥有某种持久性存储。 So my logic is looking something like this. 所以我的逻辑看起来像这样。

App in Pod1 -> get Pod1 IP address -> save to external storage

App in Pod2 -> read Pod1 IP address from storage -> complete HTTP call to App1 in Pod1

How can I get this IP address from within my application. 如何从应用程序中获取此IP地址。 I am using Java (Play Framework). 我正在使用Java(播放框架)。

You can expose Pod IP (or any other attribute of the Pod descriptor) to the Pod's environment variables by putting to your Pod manifest something like this: 您可以通过在Pod清单中放置类似以下内容的方式,将Pod IP(或Pod描述符的任何其他属性)暴露给Pod的环境变量:

env:
- name: MY_POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP

That being said, the answer by Yonah is likely what you need really. 话虽如此,Yonah的答案很可能是您真正需要的。 Service discovery is a first class citizen in Kubernetes. 服务发现是Kubernetes中的头等公民。

In kubernetes there is a concept called services built specifically for the purpose you are trying to use. 在kubernetes中,有一个称为服务的概念专门针对您要使用的目的而构建。 For example a front-end and backend both deployed in kubernetes can access each other using the service name. 例如,部署在kubernetes中的前端和后端都可以使用服务名称相互访问。 In your case create services for both pods and you should be able to access your pod from any other pod. 在您的情况下,为两个Pod创建服务,您应该可以从任何其他Pod访问您的Pod。 Aside from this benefit services have many more benefits and should be used. 除了这种好处,服务还具有更多好处,应该使用。 https://kubernetes.io/docs/concepts/services-networking/service/ Good luck! https://kubernetes.io/docs/concepts/services-networking/service/祝您好运!

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

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