Kubernetes Services are a fundamental abstraction that defines how pods are accessed within a cluster. While most people are familiar with ClusterIP, NodePort, and LoadBalancer services, Headless Services serve a very different and powerful purpose.
This article covers what a Kubernetes Headless Service is, why it exists, how it works, and when to use it
A Headless Service is a Kubernetes Service without a Cluster IP. Instead of a single virtual IP and load balancing, it provides direct pod IP access via DNS.
You create a headless service by setting:
spec: clusterIP: None
When this is done:
Traditional services abstract away individual pods and provide load balancing. However, some applications need to know exactly which pod they are talking to. There are some scenarios where more fine grained controlled is needed on which pod we want to connect for a specific need.
Common reasons can include:
Headless services enable service discovery without traffic routing.
A DNS query in normal Service
my-service.default.svc.cluster.local
returns a service IP:
10.96.0.15
Traffic is load-balanced across pods.
DNS query:
my-headless-service.default.svc.cluster.local
Returns:
10.244.1.5 10.244.2.8 10.244.3.12
Each IP corresponds to a pod, not the service.
The client has the control to decide:
To create a headless service, clusterIP needs to be set to None in the definition of a clusterIP server. Here is an example :
apiVersion: v1 kind: Service metadata: name: my-headless-service spec: clusterIP: None selector: app: my-app ports: - port: 80 targetPort: 8080
Key points:
Headless services are most commonly used with StatefulSets.
Why do we need it?
StatefulSets give each pod:
Example DNS names:
pod-0.my-headless-service.default.svc.cluster.local pod-1.my-headless-service.default.svc.cluster.local
This is ideal for:
Depending on the configuration, Kubernetes creates:
Example SRV lookup:
my-port.tcp.my-headless-service.default.svc.cluster.local
This allows advanced clients to discover:
Use a headless service when:
Do not use it when: