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

<blockquote>
<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>
</blockquote>
<p>I would suggest here to use the liveness probe and readiness probe for the POD whenever pod get join respective DNS entry will be auto-updated rather checking the liveness using custom solution.</p>
<p>to getting the list of all PODs IP you can use the Kubernetes service with type <strong>headless</strong>. you have to use <code>spec.clusterIP</code> as <strong>None</strong>.</p>
<p>Example service YAMl :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongodb-headless-service
  namespace: infrastructure
spec:
  type: NodePort
  clusterIP: None
  selector:
    app: mongodb
  ports:
  - name: mongodb
    port: 27017
    targetPort: 27017
    protocol: TCP
  selector:
    app: mongodb
</code></pre>
<p>from your custom solution code you have send request to kuberneets service which type if <strong>headless</strong> : <code>&lt;servicename&gt;.&lt;namespace&gt;.svc.cluster.local</code>.</p>
<p>code example for mongo :</p>
<pre><code>System.Net.IPAddress[] ipAddresses = Dns.GetHostAddresses(&quot;mongodb-headless-service&quot;);
string connectionString = &quot;&quot;;
foreach(IPAddress in ipAddresses)
{
  if(connectionString = &quot;&quot;)
    connectionString = &quot;mongodb://&quot;;
  else
    connectionString += &quot;,&quot;;
  connectionString += $&quot;{IPAddress.ToString()}:27017&quot;;
}
connectionString += &quot;/database&quot;;
var client = new MongoClient(connectionString);
</code></pre>
<p>extra document : <a href="https://medium.com/swlh/discovering-running-pods-by-using-dns-and-headless-services-in-kubernetes-7002a50747f4" rel="nofollow noreferrer">https://medium.com/swlh/discovering-running-pods-by-using-dns-and-headless-services-in-kubernetes-7002a50747f4</a></p>
