Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 78 ยท Services and Networking

NodePort, LoadBalancer and ExternalName

Compare the Kubernetes Service types: ClusterIP, NodePort, LoadBalancer, ExternalName and headless Services, plus externalTrafficPolicy and cost trade-offs.

A plain ClusterIP Service is reachable only from inside the cluster, which is exactly right for a database or an internal API. Sooner or later, though, something outside has to get in: a browser, a mobile app, a partner's server. Kubernetes offers several ways to open that door, and they are selected with a single field, spec.type.

This lesson walks through the Service types in the order they build on each other, explains the traffic-policy setting that decides whether client IPs survive, introduces headless Services (a Service without a virtual IP), and ends with a Python stand-in that allocates NodePort numbers the way the control plane does.

All manifests and commands here are read-only because the site has no cluster, and LoadBalancers in particular need a cloud provider. The Python stand-in reproduces the allocation rules, not the networking.

Four ways to expose Pods

ClusterIP Inside the cluster only.
NodePort A port opened on every node.
LoadBalancer A cloud load balancer in front.
ExternalName A DNS alias to something else.

Each type builds on the one before it, except ExternalName.

1 / 2

The type ladder

flowchart LR
  C["Client outside"] --> LB["LoadBalancer<br/>(cloud IP)"]
  LB --> NP["NodePort<br/>(30000-32767 on each node)"]
  NP --> CIP["ClusterIP<br/>(virtual IP)"]
  CIP --> P["Pods"]

A LoadBalancer Service includes a NodePort, which includes a ClusterIP.

Each type adds a layer on top of the previous one. That is why kubectl get service for a LoadBalancer shows a cluster IP and a node port as well as an external address.

NodePort

A NodePort Service opens the same port on every node in the cluster and forwards traffic that arrives there to the Service. By default the port is chosen from the range 30000 to 32767 (the cluster administrator can change the range).

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

If you leave nodePort out, the control plane picks a free port from the range for you. After applying, http://<any-node-ip>:30080 reaches the application, even on nodes that run none of its Pods, because each node forwards the traffic to a Pod elsewhere.

NodePort is simple and needs nothing from the environment, which makes it good for local clusters, tests and on-premises setups where you place your own load balancer in front of the nodes. Its drawbacks: the port numbers are awkward, firewalls must allow the whole range, you must give clients the addresses of nodes, and node addresses change when nodes are replaced. A hand-picked nodePort can collide with another Service, in which case the API server rejects the second one.

LoadBalancer

A LoadBalancer Service asks the platform for an external load balancer. On a managed cloud (EKS, GKE, AKS and others) a controller that is part of the cloud integration creates a real load balancer, points it at the nodes' NodePorts and writes its address back into the Service.

YAML
apiVersion: v1
kind: Service
metadata:
  name: web
  annotations:
    service.beta.kubernetes.io/example-annotation: "provider-specific"
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 443
      targetPort: 8443
  loadBalancerSourceRanges:
    - 203.0.113.0/24
Terminal
kubectl get service web
TEXT
NAME   TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)         AGE
web    LoadBalancer   10.96.14.20    198.51.100.25    443:31842/TCP   2m

Things to know:

  • The address appears under EXTERNAL-IP after a short delay. Until then it shows <pending>. On a laptop cluster without a cloud, it stays pending forever unless you add a helper (a tunnel feature in your local tool, or an on-premises implementation such as MetalLB).
  • Behavior beyond the basics (health checks, TLS, internal-only load balancers) is controlled by provider-specific annotations. They are not portable between clouds.
  • loadBalancerSourceRanges restricts which client networks may connect, where the provider supports it.
  • Each LoadBalancer Service usually creates its own cloud load balancer, which costs money and an IP address. For a dozen HTTP applications you normally put an Ingress behind a single one rather than creating twelve.

externalTrafficPolicy

When traffic arrives on a node through a NodePort or LoadBalancer, the node can forward it to any Pod of the Service, including Pods on other nodes. The field externalTrafficPolicy controls this:

Value Behavior Trade-offs
Cluster (default) Forward to any ready Pod in the cluster Even spread, but an extra network hop, and the client's source IP is replaced by the node's address
Local Forward only to Pods on the node that received the traffic Keeps the real client IP and avoids the hop, but spreading depends on how Pods are placed, and nodes with no local Pod must refuse the traffic

With Local, a cloud load balancer asks every node a health-check question (Kubernetes allocates a healthCheckNodePort for it) and sends traffic only to nodes that actually run a ready Pod. Use Local when the application needs the client address (for allow lists or logs), and spread Pods across nodes so no single node gets a disproportionate share.

A sibling field, internalTrafficPolicy, does the same for traffic that comes from inside the cluster: Local keeps it on the caller's own node.

ExternalName

An ExternalName Service is not a proxy at all. It has no selector, no cluster IP and no endpoints. It makes the cluster's DNS answer a lookup of the Service name with a CNAME record that points at another DNS name.

YAML
apiVersion: v1
kind: Service
metadata:
  name: billing-db
  namespace: shop
spec:
  type: ExternalName
  externalName: db.example.com

A Pod that resolves billing-db.shop.svc.cluster.local receives db.example.com as an alias and connects there. This is useful to give an external dependency a stable internal name, so that you can later replace it with an in-cluster Service without touching client configuration.

Remember what it does not do: it does not map ports, does not load balance, and does not rewrite protocol-level names. If the target speaks HTTP or HTTPS, the client sends the original host name (billing-db) in the request headers or TLS handshake, which the target may reject. ExternalName works best for plain protocols where the host name does not matter, such as database connections.

Headless Services

Setting clusterIP: None creates a headless Service: no virtual IP and no proxying. Instead the DNS name resolves directly to the IP addresses of the ready Pods behind it.

YAML
apiVersion: v1
kind: Service
metadata:
  name: db
spec:
  clusterIP: None
  selector:
    app: db
  ports:
    - name: postgres
      port: 5432

A lookup of db returns several A records, one per Pod, and the client chooses. This is what you want when clients must know which instance they talk to: a database cluster with a primary and replicas, or a StatefulSet where each Pod has a stable name such as db-0.db. The same DNS record also publishes SRV records with port numbers, which you will meet in DNS and Service Discovery. Because there is no load balancing, the application or its client library has to deal with choosing and failover.

Stand-in: allocating NodePorts

When you create a NodePort Service the API server must hand out a port that is inside the configured range and not used by another Service, or reject your explicit request. The stand-in below reproduces that bookkeeping, including pseudo-random selection (seeded so the output is repeatable here; the real cluster picks differently every time).

Python NodePort allocation rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only about 2,700 ports are available for the entire cluster, so the range is a shared, limited resource. Another reason to prefer one Ingress or Gateway over many NodePorts.

Stand-in: which type fits?

A second stand-in turns the advice of this lesson into a tiny decision function, so you can try your own situations. It is a teaching aid, not an official rule.

Python Choose a Service type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Creating one LoadBalancer per application and being surprised by the bill.
  • Expecting EXTERNAL-IP to appear on a local cluster that has no load balancer implementation.
  • Using externalTrafficPolicy: Local while all Pods sit on one node; the other nodes fail their health checks, so a single node takes all the load.
  • Pointing an ExternalName at an HTTPS endpoint and getting certificate or host-header errors.
  • Relying on provider annotations as though they were Kubernetes standards.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the default NodePort range?

    The API server allocates NodePorts from 30000-32767 unless the administrator changes the range.
  2. 2. Which Service type works by returning a CNAME instead of proxying traffic?

    ExternalName has no selector, cluster IP or endpoints; cluster DNS answers with a CNAME to the configured name.
  3. 3. You need the original client IP address inside the Pod. Which setting helps?

    With Local, traffic stays on the node that received it and is not source-NATed to another node, so the client address is preserved. The cost is uneven spreading.
  4. 4. What does clusterIP: None create?

    A headless Service has no virtual IP and no proxying. DNS returns the addresses of the ready Pods so clients can pick one.
  5. 5. Why is one LoadBalancer Service per web application often a poor design?

    Every LoadBalancer Service normally provisions its own cloud resource and IP. An Ingress controller behind one load balancer routes by host and path to many Services.

Summary

  • Service types build up: ClusterIP, then NodePort (a port on every node), then LoadBalancer (a cloud load balancer in front).
  • NodePort uses 30000-32767 by default; LoadBalancer needs a provider and often provider-specific annotations.
  • externalTrafficPolicy: Local keeps client IPs and avoids a hop, at the price of uneven spreading.
  • ExternalName is a DNS alias; a headless Service (clusterIP: None) returns Pod IPs directly.
  • For many HTTP apps, prefer one Ingress or Gateway over many LoadBalancers.

Next: how those names get resolved inside the cluster in DNS and Service Discovery.