<p>I have a statefulset with 3 members. They are accessible from inside the cluster with something like:</p>

<pre><code>podname-{0..n}.service.default.svc.cluster.local
</code></pre>

<p>I'm using the Kubernetes API from a Controller. I just created the Statefulset with:</p>

<pre class="lang-go prettyprint-override"><code>import (
    "k8s.io/client-go/kubernetes"
    appsv1 "k8s.io/api/apps/v1"
)
...

s := appsv1.StatefulSet{ /* some data */ }
kubernetes.AppsV1().StatefulSets(nameSpace).Create(s)
</code></pre>

<p>Let's assume everything worked correctly, and after a few minutes I have 3 running Pods. How can I get the Pod DNS entries for the newly created Pods?</p>

<p>I know I can build the DNS with:</p>

<pre><code>fmt.Sprintf("%s-%d.%s.%s.svc.cluster.local", s.Name, idx, service, S.Namespace)
</code></pre>

<p>But I'm finding some issues:</p>

<ol>
<li>I will have to calculate the index depending on the amount of replicas</li>
<li>I need to know about the service name used for this <code>StatefulSet</code></li>
<li>I'm assuming the cluster domain is <code>cluster.local</code> which is not necessarily true</li>
</ol>

<p>What I think it should exists (but I'm not sure if it really exists), is an API, that given a StatefulSet will allow me to know the DNS names of the Replicas that were created. Does an API like this exists?</p>

<p>When you create the headless <code>Service</code> with the <code>StatefulSet</code>, it should create DNS SRV entries. You can query that to get a list of all host names of the pods corresponding to the headless <code>Service</code> without knowing the number. For example, if using Python you can do:</p>

<pre><code>&gt;&gt;&gt; import dns
&gt;&gt;&gt; for item in dns.resolver.query('your-app-name-headless', 'SRV'): item
...
&lt;DNS IN SRV rdata: 10 25 0 2555bb89.your-app-name-headless.your-project.svc.cluster.local.&gt;
&lt;DNS IN SRV rdata: 10 25 0 830bdb18.your-app-name-headless.your-project.svc.cluster.local.&gt;
&lt;DNS IN SRV rdata: 10 25 0 aeb532de.your-app-name-headless.your-project.svc.cluster.local.&gt;
&lt;DNS IN SRV rdata: 10 25 0 a432c21f.your-app-name-headless.your-project.svc.cluster.local.&gt;
</code></pre>

<p>See:</p>

<ul>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#srv-records" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#srv-records</a></li>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/services-networking/service/#headless-services</a></li>
</ul>
