Kubernetes’ headless service is a useful tool for building distributed applications, allowing direct access to specific pods, especially in complex load-balancing situations.
In this guide, we learn how Kubernetes Headless services work and how you can create one, as well as why they are crucial for stateful workloads, such as databases, distributed systems, and message queries.
Let’s get right into it!
What Is a Kubernetes Headless Service?
A Kubernetes headless Service groups multiple Pod endpoints into one resource. You can set up different ways to access this group. By default, a stable cluster IP address is provided, which clients within the cluster can use to reach the Pods in the Service. When a client sends a request to this stable IP, it gets directed to one of the Pods in the Service.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: metrics
department: engineering
ports:
...
A Service uses a selector to identify its member Pods. For a Pod to be included in the Service, it must have all the labels defined in the selector. A label is simply a key/value pair linked to an object.
The Service manifest below includes a selector with two labels. It indicates that any Pod with both the app: metrics label and the department: engineering label qualifies as a member of this Service.
Get exclusive access to all things tech-savvy, and be the first to receive
the latest updates directly in your inbox.
In Kubernetes, a Service is a way to expose a network application running as one or more Pods in your cluster.
A key goal of Services in Kubernetes is to allow you to keep your existing application without changing its service discovery method. You can run code in Pods, whether it’s designed for cloud-native environments or older, containerized applications. A Service makes these Pods accessible over the network for client interactions.
Example: If your workload communicates via HTTP, you may opt to use an Ingress to manage how web traffic accesses that workload. Ingress is not classified as a Service type, but it serves as the gateway for your cluster. An Ingress allows you to combine your routing rules into one resource, enabling you to expose various parts of your workload, which operate independently in your cluster, through a single listener.
Headless Service Kubernetes Features?
No Cluster IP: Headless services don’t have a cluster-internal IP address.
Pod DNS Entries: Each pod associated with the headless service gets a DNS entry formatted as …svc.cluster.local. For example:
pod-1.my-headless-service.default.svc.cluster.local
Applications: Headless services are often used with StatefulSets, facilitating direct communication with pods.
Support for Stateful Applications: Headless services are useful in situations where it is important to maintain pod state, allowing for direct interaction with them.
Headless Services are beneficial in many situations, such as:
- Service discovery: A headless Service Kubernetes can be used for Service discovery. To do this, create a Service with a name and a selector. The DNS record for the headless service includes all the IPs of the Pods that match the selector. Clients can use these DNS records to locate the IP addresses of the Pods linked to the Service.
- Direct Pod access: Clients can connect straight to the Pods linked with a headless Service, which is helpful for Services needing direct access to the underlying Pods, like load balancers and DNS servers.
- Flexibility: Kubernetes Headless services can create various topologies, including load balancers, DNS servers, and distributed databases.
YAML Example: How To Create a Headless Service
apiVersion: v1
kind: Service
metadata:
name: web
spec:
clusterIP: None
selector:
app: web
ports:
- port: 80
name: http
This sets up a ClusterIP Service named “my-web-app” that directs incoming traffic to Pods labeled “app: my-web-app”. The Service operates on port 80 and sends traffic to the Pods’ port 8080.
The default Service type in Kubernetes is “ClusterIP” if no type is specified.
To create a headless Service, we must include the “clusterIP” field and assign it the value “None“.

apiVersion: v1
kind: Service
metadata:
name: my-web-app
spec:
selector:
app: my-web-app
clusterIP: None
ports:
port: 80
targetPort: 8080
The YAML setup establishes a headless Service that represents Pods marked with “app: my-web-app”. It does not have a cluster IP, which lets clients decide how to connect to these Pods instead of routing traffic, and it offers a list of all Pods.
The ClusterIP Service makes it easier to route requests, whereas the headless Service offers a list of recognized Pods and their IP addresses for clients to direct and manage network requests.
Once Createss, Verify Your Headless Service
You can check it by using this command:
kubectly get service headless-svc
Conclusion
While Kubernetes headless services might seem like niche components, they play a crucial role in the realm of stateful applications. These services provide a robust foundation for StatefulSets, enabling them to harness dependable networking and DNS discovery. This functionality is especially vital for managing distributed workloads, orchestrating queue systems, and operating clustered databases.
In essence, headless services elevate the resilience and efficiency of applications that require a stable and consistent network presence.
FAQ’s
1. What exactly is a Kubernetes headless service?
A Kubernetes headless Service is a type of Service that does not assign a cluster IP address. Instead, it uses DNS to expose the IP addresses of the Pods associated with the Service. This configuration allows you to connect directly to the Pods, eliminating the need for a proxy.
2: Why should you use StatefulSets with a headless service?
Headless services provide each pod with a stable, resolvable network identity, which is essential for StatefulSets.
3: Is it possible to use a headless service Kubernetes for load balancing?
No, headless services do not support load balancing. For that purpose, you should use LoadBalancer or ClusterIP services.
4: How do headless services manage DNS?
Kube-DNS facilitates direct access by returning the A/AAAA records for each pod that the service has selected.
5: Are headless services safe to use?
They are as secure as your pod network. Always implement RBAC and suitable network policies when exposing pods directly.