<p>I have a statefulSet with two replicas.
It's headless service name is <strong>"gov-svc"</strong>
It's -></p>

<ol>
<li>.metadata.name: sts</li>
<li>.metadata.namespace: default</li>
<li>.spec.serviceName: gov-svc</li>
<li>.spec.template.spec.subdomain: gov-svc</li>
<li>.spec.replicas: 2</li>
</ol>

<p>Before running statefulSet</p>

<pre><code>kubectl get pods --all-namespaces
NAMESPACE     NAME                                    READY   STATUS             RESTARTS   AGE
kube-system   coredns-99b9bb8bd-qdnsb                 1/1     Running            0          4h
kube-system   etcd-minikube                           1/1     Running            0          4h
kube-system   kube-addon-manager-minikube             1/1     Running            0          4h
kube-system   kube-apiserver-minikube                 1/1     Running            0          4h
kube-system   kube-controller-manager-minikube        1/1     Running            1          4h
kube-system   kube-proxy-b9np6                        1/1     Running            0          4h
kube-system   kube-scheduler-minikube                 1/1     Running            0          4h
kube-system   kubernetes-dashboard-7db4dc666b-bsk8k   1/1     Running            0          4h
kube-system   storage-provisioner
</code></pre>

<p>After running both of the pods of this statefulSet, from pod <code>sts-0</code>, <code>ping</code> results:</p>

<pre><code>$ ping  sts-0.gov-svc.default.svc.cluster.local
PING sts-0.gov-svc.default.svc.cluster.local (172.17.0.11): 56 data bytes
64 bytes from 172.17.0.11: seq=0 ttl=64 time=0.051 ms
64 bytes from 172.17.0.11: seq=1 ttl=64 time=0.444 ms
^C
--- redis-cluster-exp-0-0.redis-cluster-exp.default.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.051/0.247/0.444 ms
</code></pre>

<p>But when I try to <code>ping sts-1</code> from <code>sts-0</code>, it says:</p>

<pre><code>$ ping sts-1.gov-svc.default.svc.cluster.local
ping: bad address 'sts-1.gov-svc.default.svc.cluster.local'
</code></pre>

<p>I need to ping other pods successfully by hostname. How can I do it?</p>

Shudipta Sharma
<p>You need to create <code>headless service</code> to be able to ping replicas from each to other within a <code>StatefulSet</code>. Something like:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: gov-svc-headless
  labels:
    your_label: your_value
spec:
  selector:
    your_label: your_value
  ports:
  - port: your_port
    name: transport
    protocol: TCP
  clusterIP: None &lt;---
</code></pre>

<p>Note:</p>

<blockquote>
  <p>With selectors For headless services that define selectors, the
  endpoints controller creates Endpoints records in the API, and
  modifies the DNS configuration to return A records (addresses) that
  point directly to the Pods backing the Service.</p>
</blockquote>

<p>Note:</p>

<blockquote>
  <p>or headless services that do not define selectors, the endpoints
  controller does not create Endpoints records. However, the DNS system
  looks for and configures either:</p>
  
  <p>CNAME records for ExternalName-type services. A records for any
  Endpoints that share a name with the service, for all other types.</p>
</blockquote>

<p>More info: <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></p>
