Sobes.tech
Junior

Mis vahe on headless teenusel ja teiste teenusetüüpide vahel?

sobes.tech AI

Vastus AI-lt

Headless service in Kubernetes differs from ClusterIP by lacking a ClusterIP and, consequently, the load balancing provided by the service itself. Instead, DNS returns a set of pod IP addresses matching the service selector, allowing the client to directly choose the endpoint.

Regular service (ClusterIP, NodePort, LoadBalancer):

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP # or NodePort, LoadBalancer
  • Has its own ClusterIP.
  • Balances traffic between pods.
  • DNS returns the ClusterIP of the service.

Headless service:

apiVersion: v1
kind: Service
metadata:
  name: my-headless-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  clusterIP: None # main difference
  • Does not have a ClusterIP.
  • Does not perform load balancing.
  • DNS returns a list of pod IP addresses matching the selector.

Comparison:

Feature Regular service Headless service
ClusterIP Exists Does not exist
Load balancing Built-in Absent
DNS resolving Service's ClusterIP Pod IP addresses
Usage Standard Service discovery, databases, application state

Headless services are often used in the following scenarios:

  • Service discovery: Clients can get a list of all available pods and select one based on their internal logic.
  • Databases: Allows clients to connect directly to specific database replicas.
  • Stateful applications: Enables clients to address specific application instances that maintain state.