Is it required to use a headless service for statefulsets?

11/24/2019

Have a couple of question on headless services:

  1. Is it required to use a headless service for statefulsets? Can I use the normal service.yml for the stateful sets also?
  2. Is it required to use a service & then a headless service? Can I just use headless service instead?
  3. Can I not use the pods to attach to the service instead of headless service?
-- Sandy
kubernetes
kubernetes-service
kubernetes-statefulset

2 Answers

11/25/2019
  1. You don't have to use a Headless Service for a StatefulSet, but it often makes sense to use one if you want to take advantage of the sticky identity of each Pod in a StatefulService (i.e. address a specific Pod by name rather than any of the Pods at random as it's the case with a normal Service).
  2. Not sure how you mean this. You create a Headless Service by defining a ClusterIP Service and setting the clusterIP field to None.
  3. You can use the Pod names directly too with some tools (e.g. kubectl port-forward), but the Headless Service creates DNS names for all the Pods so you can address them in a more general way.

References:

-- weibeld
Source: StackOverflow

11/24/2019

Headless service doesn't carry any clusterIP, it just takes you directly to the particular pod that you need to communicate to, via its hostname.

Let's take example -

Say you installed kafka statefulset, running 3 brokers in it, now you want to communicate to one broker specifically then that can be done via headless service,

any normal service can do the random distribution of the traffic ( if you have not added any specific affinities in place), the normal service can also do for you, it's all based on use case scenarios

-- Tushar Mahajan
Source: StackOverflow