<p>I have a Service in a Kubernetes cluster which has a label selector, several pods have this label, therefore are "connected" to the Service. Is it possible to retrieve the internal IP addresses of these pods using the Service IP?</p>

<p>I know that service endpoints can be retrieved in the Kubernetes CLI, however, I was wondering whether this could be achieved in Java? i.e., Nslookup.</p>

<p>Maybe an addition to this question would be: How do you perform a DNS query on a headless service, do you pass the service name?</p>

<p>Thanks,</p>

Yrneh
<p>You can get the endpoints from the API server. For example to get the IP addresses for the first endpoint returned by the API server:</p>

<pre><code>$ curl -s -k -H 'Authorization: Bearer &lt;REDACTED&gt;' \
  https://&lt;apiserver-address&gt;:6443/api/v1/endpoints | \
  jq .items[0].subsets[0].addresses[].ip
</code></pre>

<p>You can also use this to retrieve the node names where your pods are running calling the above endpoint. </p>

<pre><code>$ curl -s -k -H 'Authorization: Bearer &lt;REDACTED&gt;' \
  https://&lt;apiserver-address&gt;:6443/api/v1/endpoints | \
  jq .items[0].subsets[0].addresses[].nodeName
</code></pre>

<p>This can also be done using your favorite language API like <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">Java</a>, <a href="https://github.com/kubernetes/client-go" rel="nofollow noreferrer">Go</a> and <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">Python</a></p>

<p>Described <a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#srv-records" rel="nofollow noreferrer">here</a> the SRV records that you can use to find the CLUSTER-IP address of your service (not your pods IP addresses). From a pod:</p>

<pre><code> $ dig srv _portname._tcp.servicename.default.svc.cluster.local
 ...
 servicename.default.svc.cluster.local. 5 IN A  10.x.x.x &lt;== service IP
 ...
</code></pre>
