Home > Blockchain >  My understanding of headless service in k8s and two questions to verify
My understanding of headless service in k8s and two questions to verify

Time:03-19

I am learning the headless service of kubernetes.

I understand the following without question (please correct me if I am wrong):

  • A headless service doesn't have a cluster IP,
  • It is used for communicating with stateful app
  • When client app container/pod communicates with a database pod via headless service the pod IP address is returned instead of the service's.

What I don't quite sure:

  • Many articles on internet explaining headless service is vague in my opinion. Because all I found only directly state something like :

If you don't need load balancing but want to directly connect to the pod (e.g. database) you can use headless service

But what does it mean exactly?

So, following are my thoughts of headless service in k8s & two questions with an example

Let's say I have 3 replicas of PostgreSQL database instance behind a service, if it is a regular service I know by default request to database would be routed in a round-robin fasion to one of the three database pod. That's indeed a load balancing.

Question 1:

If using headless service instead, does the above quoted statement mean the headless service will stick with one of the three database pod, never change until the pod dies? I ask this because otherwise it would still be doing load balancing if not stick with one of the three pod. Could some one please clarify it?

Question 2:

I feel no matter it is regular service or headless service, client application just need to know the DNS name of the service to communicate with database in k8s cluster. Isn't it so? I mean what's the point of using the headless service then? To me the headless service only makes sense if client application code really needs to know the IP address of the pod it connects to. So, as long as client application doesn't need to know the IP address it can always communicate with database either with regular service or with headless service via the service DNS name in cluster, Am I right here?

CodePudding user response:

A normal Service comes with a load balancer (even if it's a ClusterIP-type Service). That load balancer has an IP address. The in-cluster DNS name of the Service resolves to the load balancer's IP address, which then forwards to the selected Pods.

A headless Service doesn't have a load balancer. The DNS name of the Service resolves to the IP addresses of the Pods themselves.

This means that, with a headless Service, basically everything is up to the caller. If the caller does a DNS lookup, picks the first address it's given, and uses that address for the lifetime of the process, then it won't round-robin requests between backing Pods, and it will not notice if that Pod disappears. With a normal Service, so long as the caller gets the Service's (cluster-internal load balancer's) IP address, these concerns are handled automatically.

A headless Service isn't specifically tied to stateful workloads, except that StatefulSets require a headless Service as part of their configuration. An individual StatefulSet Pod will actually be given a unique hostname connected to that headless Service. You can have both normal and headless Services pointing at the same Pods, though, and it might make sense to use a normal Service for cases where you don't care which replica is (initially) contacted.

CodePudding user response:

A headless service will return all Pod IPs that are associated through the selector. The order is not stable, so if a client is making repeated DNS queries and uses only the first returned IP, this will result in some kind of load balancing as well.

Regarding your second question: That is correct. In general, if a client does not need to know all instances - and handle the unstable IPs - a regular service provides more benefits.

  • Related