Read IP of k8s pod from application running inside it

5/14/2018

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).

-- john
kubernetes

2 Answers

5/15/2018

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.

-- Alexandr Lurye
Source: StackOverflow

5/14/2018

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!

-- Yonah Dissen
Source: StackOverflow