Skip to main content
Service ConfigurationMediumSingle Choice

Service Configuration Question

Statement

How to create a headless service?

#kubernetes#headless-service#clusterip
A

By specifying .spec.ClusterIP: headless

B

By specifying .spec.clusterIP: None

Correct
C

By specifying .spec.ClusterIP: 0.0.0.0

D

By specifying .spec.ClusterIP: localhost

Resolution & Explanations

A headless service in Kubernetes is created by setting .spec.clusterIP to None. This instructs Kubernetes not to allocate a ClusterIP for the service, making it headless. Headless services are used for stateful applications like databases where direct pod-to-pod communication is needed, or for custom service discovery mechanisms.

How it Works

When a service is headless, Kubernetes does not assign a virtual IP (ClusterIP) to it. Instead, DNS resolution returns the IP addresses of the pods backing the service directly. This allows clients to connect to individual pods rather than load-balanced endpoints.

Why .spec.clusterIP: None is Correct

Setting clusterIP: None explicitly tells Kubernetes to create a headless service. This is the standard and documented way to define a headless service in Kubernetes.

Why Other Values are Incorrect

  • .spec.ClusterIP: headless: The value headless is not a valid setting for clusterIP. The only valid special value is None (case-sensitive).
  • .spec.ClusterIP: 0.0.0.0: This is not a valid value for clusterIP. It is not recognized by Kubernetes and would likely cause an error or be ignored.
  • .spec.ClusterIP: localhost: This is also not a valid value. localhost is not a valid ClusterIP and would not create a headless service.

In summary, the correct way to create a headless service is to set clusterIP: None in the service specification.