<p>I am learning the headless service of kubernetes.</p>
<p>I understand the following without question (please correct me if I am wrong):</p>
<ul>
<li>A headless service doesn't have a cluster IP,</li>
<li>It is used for communicating with stateful app</li>
<li>When client app container/pod communicates with a database pod via headless service the pod IP address is returned instead of the service's.</li>
</ul>
<p>What I don't quite sure:</p>
<ul>
<li>Many articles on internet explaining headless service is vague in my opinion. Because all I found only directly state something like :</li>
</ul>
<blockquote>
<p>If you don't need load balancing but want to directly connect to the
pod (e.g. database) you can use headless service</p>
</blockquote>
<p>But what does it mean exactly?</p>
<p><strong>So, following are my thoughts of headless service in k8s &amp; two questions with an example</strong></p>
<p>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.</p>
<p><strong>Question 1:</strong></p>
<p>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?</p>
<p><strong>Question 2:</strong></p>
<p>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?</p>

<p>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.</p>
<p>A headless Service doesn't have a load balancer.  The DNS name of the Service resolves to the IP addresses of the Pods themselves.</p>
<p>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.</p>
<p>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 <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="noreferrer">given a unique hostname connected to that headless Service</a>.  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.</p>
