Discovering Running Pods By Using DNS and Headless Services in Kubernetes

When retrieving all service’s connected pods is desired

Mattias te Wierik
The Startup

--

A service connected to three pod instances

In a Kubernetes cluster, the service resource is being used as an abstraction to expose your running pods as a (single) entry point in the network of your cluster. The service acts as a Service Discovery mechanism and enables you to communicate easily with upstream pods without the need to know the exact individual IP addresses of individual pods.

However, in some situations it’s desired to be able to retrieve the IP addresses of all the pods that are connected to a certain service. When your pods are stateful, like a deployed database, individual pods do need to communicate with their fellow instances. In this situation, your pods need to know which other members are part of the database cluster. Here is where the headless service comes to the rescue!

What is a headless service?

When deploying services, you have the possibility to set three different service types to specify which kind of service you want. These are:

  • ClusterIP: the default type to expose the server only on an cluster-internal ip address
  • NodePort: allows to expose the service through a static port on the node.

--

--