Networking: Services, DNS, Ingress and policies · 5 MIN READ

Service types: NodePort, LoadBalancer, ExternalName and headless Services

Pick the right Service type for each job, reach a NodePort and a LoadBalancer from outside the cluster, and explain what externalTrafficPolicy changes.

A ClusterIP is reachable only from inside the cluster. Letting traffic in from outside, pointing an in-cluster name at an external host, or letting clients see every pod each needs a different Service type. The types stack: every LoadBalancer is also a NodePort, and every NodePort is also a ClusterIP.

kubernetes · A MAP OF THE IDEA

  1. Inside only: ClusterIP
  2. From a cloud LB: LoadBalancer
  3. DNS alias: ExternalName
  4. Every pod: clusterIP None

Useful when

  • The types stack, so one Service serves inside and outside callers.
  • Headless and ExternalName solve discovery without any proxying.

Watch out

  • Without a load-balancer controller EXTERNAL-IP stays <pending>.
  • ExternalName changes DNS only; Host headers and TLS names still differ.

Implementation & operation cost: A cloud load balancer per LoadBalancer Service, which is why HTTP apps usually share one through an Ingress or a Gateway.

01

ClusterIP and NodePort

ClusterIP is the default and the right choice for anything only other pods call. type: NodePort keeps the ClusterIP and also reserves one port, from 30000-32767 by default, on every node. kube-proxy on each node forwards that port to the Service, so a client can use any node's IP, including a node that runs none of the pods; that node forwards the connection over the pod network. If you omit nodePort the API server picks a free one. Set it explicitly only when something outside needs a fixed number, and expect a conflict error if another Service already holds it. High ports on node addresses that change are awkward for users, so NodePorts mostly serve as plumbing that a load balancer or a test setup points at.

yaml
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: NodePort
  selector:
    app: web
  ports:
  - port: 80
    targetPort: 8080
    nodePort: 30080
Read the code

Three ports, three meanings: nodePort 30080 is opened on every node's IP, port 80 is the ClusterIP port that pods use, and targetPort 8080 is where the container listens. kubectl get svc shows the pair as 80:30080/TCP. In the lab, kubectl create service nodeport web --tcp=80:8080 --node-port=30080 creates the same object with a port named 80-8080.

02

LoadBalancer: an external address from a controller

type: LoadBalancer asks for an external load balancer, but Kubernetes itself does not create one. On AWS, GCP or Azure the cloud controller manager provisions a cloud load balancer, points it at the node ports (or straight at pod IPs on some clouds), and writes its address into status.loadBalancer.ingress, which kubectl shows as EXTERNAL-IP. On bare metal MetalLB fills that role; on kind, cloud-provider-kind does. Without such a controller the Service shows <pending> forever, the first thing to check when an EXTERNAL-IP never appears. In the GopherForge lab a simulated controller assigns 172.18.255.x addresses. Each LoadBalancer Service is usually a separate, billed cloud resource, so many HTTP services normally share one entry point through an Ingress or a Gateway instead.

03

ExternalName and headless Services

type: ExternalName has no selector, no ClusterIP and no proxying: CoreDNS answers the Service name with a CNAME to spec.externalName. Pods can call payments today and switch to an in-cluster Service later without a configuration change. Only DNS changes, so HTTP Host headers and TLS certificates must still match the external name, which often breaks HTTPS. A headless Service sets clusterIP: None. It keeps its selector and endpoints, but there is no virtual IP and kube-proxy ignores it; DNS returns the IPs of all ready pods instead. Use it when clients must reach individual pods: StatefulSet members with stable names, a database primary and its replicas, or gRPC clients that balance across pods themselves.

yaml
apiVersion: v1
kind: Service
metadata:
  name: payments
spec:
  type: ExternalName
  externalName: api.payments.example.com
---
apiVersion: v1
kind: Service
metadata:
  name: web-headless
spec:
  clusterIP: None
  selector:
    app: web
  ports:
  - port: 8080
Read the code

payments is only a DNS alias: kubectl get svc shows api.payments.example.com as its EXTERNAL-IP and no ports. web-headless has None as its CLUSTER-IP, yet its endpoints are the same pod addresses that web uses; only the way clients reach them differs. nslookup web-headless from a pod returns one address per ready pod instead of a single virtual IP.

04

externalTrafficPolicy: Cluster or Local

For NodePort and LoadBalancer Services, externalTrafficPolicy decides what a node does with traffic from outside. Cluster, the default, lets every node accept and forward to any ready pod in the cluster: load spreads evenly, but the extra hop adds latency and the node source-NATs the packet, so the pod sees a node IP instead of the client's. Local delivers only to pods on the receiving node: the client IP is preserved and there is no second hop, but nodes without a local pod drop the traffic. Cloud load balancers learn which nodes to use through a health check on healthCheckNodePort, and they spread traffic per node, not per pod, so uneven pod placement means uneven load. Choose Local when you need real client IPs for logs or allow-lists.

05

Try it in the lab terminal

Start a cluster with a control plane and two workers, run three web pods behind a NodePort, and reach it through two different node IPs, one of them the control plane, which runs no web pod at all. Then add a LoadBalancer Service for the same pods and call its external IP from your terminal. Finish with an ExternalName alias and a headless Service, and compare how kubectl get svc and the endpoints present them.

bash
cluster create --workers 2
kubectl create deployment web --image=gopherforge/web:v1 --replicas=3 --port=8080
kubectl create service nodeport web --tcp=80:8080 --node-port=30080
kubectl rollout status deploy/web
kubectl get nodes -o wide
kubectl get pods -o wide
curl -s http://172.18.0.2:30080/
curl -s http://172.18.0.4:30080/version
kubectl expose deployment web --name=web-lb --type=LoadBalancer --port=80 --target-port=8080
kubectl get svc web-lb
curl -s http://172.18.255.200/
cat > more-services.yaml <<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: payments
spec:
  type: ExternalName
  externalName: api.payments.example.com
---
apiVersion: v1
kind: Service
metadata:
  name: web-headless
spec:
  clusterIP: None
  selector:
    app: web
  ports:
  - port: 8080
EOF
kubectl apply -f more-services.yaml
kubectl get svc
kubectl get endpoints web-headless
Read the code

The nodes are 172.18.0.2 (control plane), 172.18.0.3 and 172.18.0.4, and get pods -o wide places the web pods only on the two workers. The curl to the control plane's 172.18.0.2:30080 still answers "gopherforge web v1 served by web-<hash>-<id>", because every node forwards its node port. web-lb gets EXTERNAL-IP 172.18.255.200 plus a node port of its own, shown as 80:<nodePort>/TCP, and curl to that IP reaches a pod. get svc then lists the ExternalName and the headless Service, whose endpoints are the three web pods on 8080.

TAKE THIS WITH YOU

Use ClusterIP for in-cluster callers, NodePort as plumbing, LoadBalancer when a controller can give you an external address, ExternalName for DNS aliases and headless Services when clients need every pod. externalTrafficPolicy: Local trades even spreading for the client source IP.

Check your understanding

A StatefulSet of three database pods needs each member to be reachable under its own stable DNS name. Which Service should its serviceName point to?

Go to the source

Progress stays in this browser. No account needed.