Service Types

Difficulty: Intermediate

Question

What are the different types of Services in Kubernetes, and when would you use ClusterIP, NodePort, LoadBalancer or ExternalName?

Answer

A Service gives a changing set of Pods one stable virtual IP and DNS name, and load-balances traffic across the Pods that match its label selector. There are four types: ClusterIP (internal only, the default), NodePort (opens a port on every node), LoadBalancer (provisions a cloud load balancer) and ExternalName (a DNS alias to something outside the cluster).

How a Service routes traffic

  1. The Service selects Pods by label, for example app: checkout.
  2. The EndpointSlice controller keeps a list of the IPs of matching Pods that are Ready.
  3. kube-proxy on every node programs iptables, IPVS or nftables rules so traffic to the Service IP is sent to one of those Pod IPs.
  4. CoreDNS publishes checkout.payments.svc.cluster.local pointing to the ClusterIP.

Because only Ready Pods are in the EndpointSlice, a failing readiness probe automatically removes a Pod from rotation.

The four Service types

TypeReachable fromHow it worksTypical use
ClusterIPInside the cluster onlyVirtual IP from the service CIDRService-to-service calls, databases
NodePortNode IP + port (30000-32767)Builds on ClusterIP and opens the port on all nodesDev clusters, bare metal, behind your own LB
LoadBalancerThe internet or VPCBuilds on NodePort and asks the cloud for an external LBExposing one service publicly on EKS/GKE/AKS
ExternalNameInside the clusterReturns a CNAME, no proxyingPointing to a managed DB like an RDS hostname

The types are layered: a LoadBalancer Service also has a NodePort and a ClusterIP (although cloud controllers can skip the NodePort with allocateLoadBalancerNodePorts: false).

Choosing the right one

  • Most services in a microservice system (say, Swiggy's order, restaurant and delivery services) should be ClusterIP and only talk to each other inside the cluster.
  • For public HTTP traffic, prefer a single Ingress or Gateway in front of many ClusterIP Services instead of one LoadBalancer per service. Each cloud load balancer costs money every hour.
  • Use LoadBalancer directly for non-HTTP protocols (raw TCP, gRPC without an L7 gateway, UDP) or for the ingress controller itself.
  • NodePort is rarely used directly in cloud production; it exposes every node and uses awkward high ports.
  • Headless (clusterIP: None) is not a separate type but a ClusterIP variant that returns Pod IPs directly from DNS, used by StatefulSets.

Gotchas

  • A selector that matches no Pods (typo in a label) gives a Service with zero endpoints, so connections are refused. Check with kubectl get endpointslices -l kubernetes.io/service-name=checkout.
  • port is the Service port, targetPort is the container port. Mixing them up is the most common Service bug.
  • ExternalName does no load balancing or port mapping and can break TLS hostname checks because the client still uses the Service name.
  • externalTrafficPolicy: Local preserves the client source IP but only sends traffic to nodes that run a Pod.
How Service types build on each other. Each outer type adds a way in on top of the one below it.
  1. LoadBalancer: Cloud LB with a public or VPC IP forwards to node ports.
  2. NodePort: Same port, 30000-32767, opened on every node.
  3. ClusterIP: Stable virtual IP and DNS name inside the cluster.
  4. EndpointSlice: List of Ready Pod IPs matched by the selector.
  5. Pods: The actual containers serving traffic.

Code examples

ClusterIP and LoadBalancer Services

apiVersion: v1
kind: Service
metadata:
  name: checkout
spec:
  type: ClusterIP          # default, internal only
  selector:
    app: checkout          # must match Pod labels exactly
  ports:
    - port: 80             # port clients use on the Service
      targetPort: 8080     # port the container listens on
---
apiVersion: v1
kind: Service
metadata:
  name: payments-db
spec:
  type: ExternalName       # DNS CNAME, no proxying
  externalName: payments.abc123.ap-south-1.rds.amazonaws.com

Pods call http://checkout and kube-proxy spreads requests over Ready checkout Pods. payments-db resolves to the RDS hostname, so code can use a cluster-local name.

Debugging a Service with no traffic

kubectl get svc checkout -o wide
kubectl get endpointslices -l kubernetes.io/service-name=checkout
kubectl get pods -l app=checkout --show-labels
# Test from inside the cluster
kubectl run tmp --rm -it --image=busybox:1.36 -- wget -qO- http://checkout

Empty endpoints usually mean a label mismatch or Pods that are not Ready.

Key points

  • ClusterIP is the default and is internal only.
  • NodePort uses the 30000-32767 range on every node; LoadBalancer builds on it.
  • ExternalName is just a DNS CNAME; it does not proxy or load-balance.
  • Only Ready Pods appear in EndpointSlices, so readiness probes control traffic.
  • Use one Ingress or Gateway for many HTTP services instead of many LoadBalancers.

Common follow-up questions

  • What is the difference between port, targetPort and nodePort?
  • What is a headless Service and when do you need one?
  • How does kube-proxy implement Services in iptables mode versus IPVS mode?
  • Why might you set externalTrafficPolicy to Local?

Interview tips

  • Explain that the types are nested (LoadBalancer includes NodePort includes ClusterIP); it shows you know the mechanics.
  • Bring up cost: one cloud load balancer per service adds up, which is why Ingress or Gateway API exists.
  • If asked to debug, start with endpoints and labels; that is where most Service issues live.

Concepts covered

ClusterIP, NodePort, LoadBalancer, ExternalName, EndpointSlice

More Kubernetes & Orchestration questions