简体   繁体   中英

Read IP of k8s pod from application running inside it

I have two k8s pods running. 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.

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. I am using Java (Play Framework).

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:

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

That being said, the answer by Yonah is likely what you need really. Service discovery is a first class citizen in Kubernetes.

In kubernetes there is a concept called services built specifically for the purpose you are trying to use. For example a front-end and backend both deployed in kubernetes can access each other using the service name. In your case create services for both pods and you should be able to access your pod from any other pod. Aside from this benefit services have many more benefits and should be used. https://kubernetes.io/docs/concepts/services-networking/service/ Good luck!

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