First, let's remove our existing elasticsearch Deployment Object:
$ kubectl delete deployment elasticsearch
Now, the final step is to create our StatefulSet, which provides each Pod with a unique identity, and link it to the Service, which gives each Pod a subdomain. We do this by specifying the name of the Service as the spec.serviceName property in our StatefulSet manifest file:
...
spec:
replicas: 3
serviceName: elasticsearch
...
Now, the Service linked to the StatefulSet will get a domain with the following structure:
<service-name>.<namespace>.svc.<cluster-domain>
Our Service's name is elasticsearch. By default, Kubernetes will use the default namespace, and cluster.local as the Cluster Domain. Therefore, the Service Domain for our Headless Service is elasticsearch.default.svc.cluster.local.
Each Pod within the Headless Service will have its own subdomain, which has the following structure:
<pod-name>.<service-domain>
Or if we expand this out:
<statefulset-name>-<ordinal>.<service-name>.<namespace>.svc.<cluster-domain>
Therefore, our three replicas would have the subdomains:
elasticsearch-0.elasticsearch.default.svc.cluster.local
elasticsearch-1.elasticsearch.default.svc.cluster.local
elasticsearch-2.elasticsearch.default.svc.cluster.local