StatefulSet(s) AKA sts are controller objects in Kubernetes. Let’s check the apiVersion and kind values for this object.

We need to first create a headless service and then associate that with the stateful set.

Headless service is nothing but a service AKA svc without a ClusterIP. It’s ClusterIP has to be specified as None. Let’s define a headless service configuration.

Let’s create the service

To check the service’s status, there shouldn’t be a ClusterIP as it’s a headless service

However there shouldn’t be any endpoints for the service yet, as the Pods have not yet started. Endpoint(s) AKA ep are nothing but Pod IPs and Ports mapped with the service. Endpoints are also Kubernetes objects and they hold the same name as the service, hence the Endpoint name in this case is also svc27

Let’s define an sts configuration, and map the created service here

To create the StatefulSet

Let’s check sts27’s status, it shows READY 3/3, which says all the 3 Pods are running

The service should now have endpoints as the Pods are created, there are 3 endpoints i.e. 3 Pods, however there are no ports specified in the endpoints because we have not mentioned any ports in the service configuration

Let’s check these 3 Pods now, they have ordered names, the first pod has suffix -0, second one with suffix -1 and third one with suffix -2. Hence the names would start with 0 and go in ascending order. The numbers 0, 1, 2 are called ordinal indexes

If we delete any of the Pod, a new Pod will be created with the same name. Let’s try deleting the first pod sts27-0, we should see that the StatefulSet should create another Pod with the same name

Also note that the containers inside the Pods would also have the same hostnames as the Pod names. Let’s login to the container and check it’s hostname. Note that ‘hostname’ is the command to check the hostname of the container, however it’s hostname can also seen from the prompt, the prompt ‘root@sts27-0:’ tells the hostname is sts27-0

The Pods will have DNS names with the format StatefulSet.Service, for example the Pod sts27-2 would have the DNS name as sts27-2.svc27. We can use the nslookup utility to check DNS names and corresponding IPs. We will do this from within the container sts27-0

If nslookup is not working, it can be installed as follows

Let’s now try to check the DNS entry for one of the Pods’ IP

So the complete DNS name of the Pod has this format StatefulSet.Service.default.svc.cluster.local

As we are aware however the same DNS name also has a shorter alias which takes the form StatefulSet.Service for example sts27-2.svc27

As we are aware that the Pod’s name doesn’t change even when restarted, thus the domain name is also not going to change and is going to be stable, however the Pod IPs might change

--end-of-post--