Services Explained: ClusterIP, NodePort, LoadBalancer, and Headless Services

Kubernetes services explained: stable IPs and DNS, ClusterIP, NodePort, LoadBalancer, headless Services and fixing empty endpoints.

Executive Summary: Pods come and go with new IP addresses, so clients need something stable to talk to. A Kubernetes Service provides a fixed virtual IP and DNS name and load-balances connections across the ready pods that match its label selector. ClusterIP exposes it inside the cluster, NodePort opens a port on every node, LoadBalancer asks the cloud for an external load balancer, and a headless Service skips the virtual IP and returns pod addresses directly. Most debugging comes down to one question: does the Service have endpoints?
Isometric service hub cube fanning out to four pods with ClusterIP, NodePort and LoadBalancer labels

A frontend pod talks to the billing API using an IP address copied from kubectl get pods -o wide. It works until the next deployment replaces the API pods, at which point every request times out. The fix is not a better way to look up pod IPs. It is to stop using them, and let a Service do the lookup continuously.

A Service is a Kubernetes object that defines a logical set of pods, selected by labels, and a policy for reaching them. The pods are the ones created by Deployments, as described in the workload model article. The EndpointSlice controller keeps an up-to-date list of the matching pods’ addresses, including only pods whose readiness probes pass, and kube-proxy (or an eBPF replacement) programs every node to route the Service’s virtual IP to those addresses.

A basic ClusterIP Service

apiVersion: v1
kind: Service
metadata:
  name: billing-api
  namespace: billing
spec:
  type: ClusterIP          # the default
  selector:
    app: billing-api
  ports:
    - name: http
      port: 80             # the Service's port
      targetPort: http     # the container port, by name or number

Other pods can now reach the API at http://billing-api from the same namespace, or http://billing-api.billing.svc.cluster.local from anywhere in the cluster. CoreDNS resolves the name to the Service’s cluster IP, which never changes for the life of the Service, and connections are spread across ready pods.

Using a named targetPort that refers to the container port name decouples the Service from the port number: if the container moves from 8080 to 9090, only the Deployment changes.

The Service types

Type Reachable from How Typical use
ClusterIP Inside the cluster Virtual IP and DNS name Service-to-service traffic
NodePort Any node’s IP on a high port Opens a port in 30000 to 32767 on every node Bare-metal setups, external load balancers you manage
LoadBalancer The internet or a private network Cloud provider creates a load balancer pointing at the nodes Exposing a TCP or UDP service directly
ExternalName Inside the cluster DNS CNAME to an external hostname Giving an external database a cluster-local name
Headless (clusterIP: None) Inside the cluster DNS returns the pod IPs directly StatefulSets, client-side load balancing

The types build on each other: a LoadBalancer Service is also a NodePort Service, which is also a ClusterIP Service. The cloud load balancer sends traffic to a node port, and kube-proxy forwards it to a pod.

LoadBalancer in practice

apiVersion: v1
kind: Service
metadata:
  name: billing-api-public
  annotations:
    service.beta.kubernetes.io/aws-load-balancer-scheme: internal   # provider-specific
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  selector:
    app: billing-api
  ports:
    - port: 443
      targetPort: https

Each LoadBalancer Service gets its own cloud load balancer, with its own cost. That is fine for a handful of non-HTTP services, such as a database proxy or an MQTT broker. For many HTTP services, an Ingress controller behind a single load balancer is far cheaper, which Kubernetes Ingress controllers cover. Load balancer behavior, such as internal versus public or the type of load balancer, is configured through provider-specific annotations, so check your cloud’s documentation.

externalTrafficPolicy

With the default Cluster policy, a node that receives external traffic may forward it to a pod on another node, which adds a hop and replaces the client’s source IP with the node’s. With Local, nodes only send traffic to pods on themselves, which preserves the client IP and avoids the extra hop; the cloud load balancer uses health checks to skip nodes without a ready pod. The trade-off is that traffic is balanced per node rather than per pod, so spread pods evenly.

Headless Services

spec:
  clusterIP: None
  selector:
    app: postgres

Without a virtual IP, a DNS lookup of the Service returns one record per ready pod. Combined with a StatefulSet, each pod also gets its own stable DNS name such as postgres-0.postgres.db.svc.cluster.local, which databases need for replication. Headless Services are also how clients that do their own load balancing discover all the backends.

Long-lived connections and gRPC

A ClusterIP Service balances connections, not requests. HTTP/1.1 clients that open many short connections spread naturally. gRPC and other HTTP/2 clients open one connection and multiplex every request over it, so all traffic from a client sticks to one pod, and new pods added by scaling get nothing. Fixes include client-side load balancing over a headless Service (gRPC supports this with a DNS resolver), periodically recycling connections with a maximum connection age on the server, or a service mesh or proxy that balances per request.

Other useful settings

  • sessionAffinity: ClientIP sends a client’s connections to the same pod, a crude form of sticky sessions. Prefer stateless pods.
  • internalTrafficPolicy: Local keeps in-cluster traffic on the same node, useful for node-local agents such as a logging or metrics daemon.
  • Topology-aware routing, enabled with the service.kubernetes.io/topology-mode: Auto annotation, prefers endpoints in the client’s own zone, reducing cross-zone latency and data transfer charges when pods are spread across zones.
  • publishNotReadyAddresses: true includes pods that are not ready, needed by some clustered databases during bootstrapping.

Debugging a Service

kubectl get svc billing-api -n billing
kubectl get endpointslices -n billing -l kubernetes.io/service-name=billing-api
kubectl describe svc billing-api -n billing          # look at the Endpoints line
kubectl get pods -n billing -l app=billing-api --show-labels
kubectl run tmp -n billing --rm -it --image=nicolaka/netshoot -- curl -sv http://billing-api/ready

An empty endpoints list explains most “connection refused” or “no route” errors, and it has only three causes:

  1. The selector does not match the pod labels, often a typo or a label changed in the Deployment.
  2. The pods exist but are not ready, because their readiness probe fails.
  3. The pods are in a different namespace; Services only select pods in their own namespace.

If endpoints exist but requests fail, check that targetPort matches the port the container actually listens on, and that the application binds 0.0.0.0 rather than 127.0.0.1. A network policy can also silently block traffic.

Frequently Asked Questions

What is a Kubernetes Service?

A Service gives a changing set of pods, selected by labels, a stable virtual IP and DNS name, and load-balances connections across the ones that are ready. Clients use the Service name instead of pod IPs, which change whenever pods are replaced.

What is the difference between ClusterIP, NodePort and LoadBalancer?

ClusterIP exposes a Service only inside the cluster. NodePort additionally opens a port between 30000 and 32767 on every node. LoadBalancer additionally provisions a cloud load balancer that forwards external traffic to those node ports.

What is a headless Service?

A Service with clusterIP set to None. It has no virtual IP, and DNS lookups return the IP addresses of the individual ready pods. StatefulSets use it to give each pod a stable DNS name.

Why does my Service have no endpoints?

The selector does not match any pod labels, the matching pods are not passing their readiness probes, or the pods are in a different namespace from the Service.

Stable names for unstable pods

Services are what make disposable pods usable: clients get a name that never changes, and Kubernetes keeps the list of healthy backends behind it up to date. Use ClusterIP for internal traffic, LoadBalancer sparingly for external TCP services, headless Services for stateful and client-balanced workloads, and check endpoints first whenever traffic does not arrive. For the full reference, see the Service documentation and the tutorial on connecting applications with Services.

Originally posted on 5 March 2024 and post last modified on 5 March 2024

DevOps Kubernetes Kubernetes Services Networking

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *