<p>We have a java application (microservice) that would be deployed on kubernetes using k8s deployment and one of the requirements is that each pod needs to know all the other pods running that application. So, in other words, we would need a way by which application layer retrieve all the pods that are part of the k8s deployment.</p>
<p>One custom implementation could be to have a persistant data store like database and have each of the pods send a hearbeat entry to indicate its liveness and manage the entries by listening to application lifecycle events</p>
<p>I know that any clustering technology satisfies this requirement. But I am looking for a minimal readymade library that does only this node discovery (and/or) management in kubernetes ?</p>

<p>You can use a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">headless service</a>. This service returns the IP addresses of the selected pods at a dns lookup instead of load balancing the requests.</p>
<p>First create the service. The important thing is to set the <code>spec.clusterIP</code> to <code>None</code>.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: headless-service
spec:
  clusterIP: None
  selector:
    ...
  ports:
    - ...
</code></pre>
<p>Afterwards you can get your IPs in java e.g. via:</p>
<pre><code>InetAddress address = InetAddress.getByName(&quot;headless-service&quot;); 
System.out.println(address.getHostAddress());
</code></pre>
