Skip to content

Kubernetes Services: ClusterIP, NodePort, and LoadBalancer

Pick the right Service type for the job — ClusterIP for in-cluster, NodePort for diagnostics, LoadBalancer for external traffic, and what each one costs you.

A Service in Kubernetes is a stable network identity for a set of Pods. Pods come and go, IPs change, but a Service gives the consumer a fixed virtual IP and DNS name. The three Service types you’ll meet on day one are ClusterIP, NodePort, and LoadBalancer. This article explains what each one is, when each is the right answer, and the operational details that bite — especially the difference between externalTrafficPolicy: Cluster and Local, which most teams set wrong.

How to verify

These commands show you what’s in the cluster and what each Service is doing:

kubectl get svc -A -o wide
kubectl get endpoints -A
kubectl -n kube-system get pods -l k8s-app=kube-proxy

get svc shows the Service type, ClusterIP, external IPs, and target ports. get endpoints shows which Pod IPs back each Service — empty endpoints are the classic “the Service exists but routes nowhere” failure mode. kube-proxy is the agent that programs iptables/IPVS rules on every node; if it’s CrashLoopBackOff, Service traffic does not work cluster-wide.

To check from inside the cluster:

kubectl run debug --rm -it --image=nicolaka/netshoot -- /bin/bash
# inside the pod:
# curl http://web.default.svc.cluster.local
# dig +short web.default.svc.cluster.local

What’s happening

A Service is a virtual IP. There is no process listening on that IP — the IP is a rule in every node’s kernel. When a Pod sends a packet to the Service IP, kube-proxy (in iptables or IPVS mode) DNATs it to one of the matching backend Pods. The Service’s selector is a label query; the Service controller turns that into an Endpoints (or EndpointSlice) object that lists the actual Pod IPs. When Pods come and go, the Endpoints update, the iptables/IPVS rules update, and the Service IP keeps working.

ClusterIP is the default. The Service gets a virtual IP from the service CIDR (--service-cluster-ip-range), reachable only inside the cluster. This is what you want for service-to-service traffic.

NodePort allocates a port (default range 30000-32767) on every node. Hit any node’s IP on that port and traffic is forwarded to the Service. ClusterIP is still created underneath. NodePort is useful for quick external access during bring-up or for layer-4 ingress, but it’s a sharp tool — node IPs are not stable, and exposing them directly to the internet is rarely what you want.

LoadBalancer asks the cluster’s cloud-controller-manager (or MetalLB on bare metal) to provision an external L4 load balancer pointing at the NodePort. NodePort and ClusterIP are still created underneath. This is the standard way to take external traffic on a managed cluster.

externalTrafficPolicy changes the path: Cluster (default) does an extra hop — node → another node’s Pod — which loses the client source IP. Local routes only to Pods on the same node, preserves the source IP, but unbalances traffic if Pods aren’t evenly spread.

The procedure

  1. A ClusterIP Service for service-to-service traffic. Selector matches the Deployment’s Pod labels.

    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      type: ClusterIP
      selector:
        app: web
      ports:
        - name: http
          port: 80          # the Service port
          targetPort: 8080  # the Pod port

    In-cluster DNS gives you web.<namespace>.svc.cluster.local.

  2. A NodePort Service for layer-4 access from outside, typically behind your own L4 LB or for diagnostics. Pin the port if you need it predictable:

    apiVersion: v1
    kind: Service
    metadata:
      name: web
    spec:
      type: NodePort
      selector: { app: web }
      ports:
        - port: 80
          targetPort: 8080
          nodePort: 30080
  3. A LoadBalancer Service for external traffic on a cloud or MetalLB. Set externalTrafficPolicy: Local when client IP matters (geo-blocking, rate limiting, audit logs).

    apiVersion: v1
    kind: Service
    metadata:
      name: web
      annotations:
        service.beta.kubernetes.io/aws-load-balancer-type: nlb
    spec:
      type: LoadBalancer
      externalTrafficPolicy: Local
      selector: { app: web }
      ports:
        - port: 443
          targetPort: 8443
  4. Test the Service from inside the cluster before involving DNS or ingress:

    kubectl run curl --rm -it --image=curlimages/curl -- \
      curl -sv http://web.default.svc.cluster.local
  5. Inspect the iptables rules a Service produced (on a node):

    sudo iptables-save -t nat | grep -E 'KUBE-SVC|KUBE-SEP' | head

    You’ll see one KUBE-SVC-* chain per Service and one KUBE-SEP-* per endpoint Pod.

  6. Migrate a Service to a headless one (clusterIP: None) when consumers need direct Pod IPs (gRPC client-side LB, StatefulSets):

    spec:
      clusterIP: None
      selector: { app: kafka }
      ports: [{ port: 9092 }]

Common pitfalls

  • A Service with an empty Endpoints list means the selector matches nothing — the most common cause is a typo, the second is Pods that are not Ready (readinessProbe is failing).
  • externalTrafficPolicy: Cluster is the default, which silently loses the client IP — apps that log “all requests came from 10.0.0.x” are usually this.
  • Port-collision: two Services can’t share a NodePort; if you don’t pin nodePort, Kubernetes picks one randomly and your firewall rules don’t follow.
  • LoadBalancer on a cluster without a cloud-controller-manager (bare metal, no MetalLB) leaves the Service stuck in <pending> forever.
  • Session affinity (sessionAffinity: ClientIP) is L4 only and uses source IP — behind a load balancer that hides the client IP, it pins every client to the same backend.

We operate Service topology for clients so the right type lives in the right place — ClusterIP for everything that doesn’t need to leave the cluster, ingress for HTTP, LoadBalancer only where it’s actually needed. See /en/services/managed-operations/.