1

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?

Mohit Singh
  • 401
  • 1
  • 10
  • 30
  • Not without interrogating the Kubernetes API or converting your deployment to a StatefulSet. [Kubernetes getting sibling-pod IP/properties from the same deployment/replicaset](https://stackoverflow.com/questions/51235932/kubernetes-getting-sibling-pod-ip-properties-from-the-same-deployment-replicaset) is extremely similar; [Message-passing between all pods in a replica set](https://stackoverflow.com/questions/59417033/message-passing-between-all-pods-in-a-replica-set) has more concrete suggestions. – David Maze Sep 22 '20 at 10:44
  • 1
    Why does one POD need to know the IP of the others? – Abhijit Sarkar Sep 23 '20 at 05:29
  • Yes, "we got a use case where i want each pod knows the ip of other pods", can you explain why this is needed? IP-addresses will change, e.g. during deployment or rescheduling. – Jonas Sep 23 '20 at 05:40
  • 1
    we have 3 pod running and we want to change the log level dynamically without restart the application. so we have one spring boot application the take service name (svc) name as input and with this name will find the pod ip and make the changes to the log level with call rest controller with ip address – Mohit Singh Sep 27 '20 at 14:40

4 Answers4

6

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

Aditya Guru
  • 646
  • 2
  • 10
  • 18
1

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.

FakeAlcohol
  • 860
  • 7
  • 28
0

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

tuxiedev
  • 144
  • 2
0

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

jrbe228
  • 356
  • 3
  • 11