Lesson 21 · Section 2: Core Concepts
Lesson 21: Services: LoadBalancer
How type LoadBalancer puts one public address in front of your Pods, who actually builds the load balancer, why EXTERNAL-IP stays pending on a bare cluster, and the fields and commands worth knowing for the exam. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why LoadBalancer Services exist
A NodePort Service makes your application reachable from outside the cluster, but it leaves the hard part to the
client:
- The client has to know a node's IP address. Nodes are replaced during upgrades and autoscaling, so the address it knew yesterday may be gone today.
- The port is a high number from 30000 to 32767, not 80 or 443.
- If the node the client picked goes down, nothing redirects the client to a healthy node.
What you really want is one stable address on a normal port that spreads connections over healthy nodes. That is a load balancer, and building one is different on every platform: an AWS NLB, an Azure Load Balancer, a Google Cloud forwarding rule, or a pair of virtual IPs announced on a bare-metal network.
Kubernetes does not try to be that load balancer. Instead, type: LoadBalancer is a request: "whoever runs
this cluster, please put a load balancer in front of this Service and tell me its address."
What a LoadBalancer Service is made of
Parts:
- Client (on the internet)
- Cloud load balancer (203.0.113.10:80)
- Node A (nodePort :31080), in Your cluster
- Node B (nodePort :31080), in Your cluster
- web Pod (10.244.1.7:8080), in Your cluster
- web Pod (10.244.2.4:8080), in Your cluster
Connections:
- Client to Cloud load balancer: :80
- Cloud load balancer to Node A: :31080
- Cloud load balancer to Node B: :31080
- Node A to web Pod: :8080
- Node B to web Pod: :8080
- Node A to web Pod: second hop
Each Service type builds on the one before, so a LoadBalancer Service has three ways in at the same time:
| Layer | Address in this lesson's example | Who can use it |
|---|---|---|
| ClusterIP | 10.96.0.20:80 and the DNS name web.shop | Pods inside the cluster |
| NodePort | <any node IP>:31080 | Anything that can reach a node |
| Load balancer | 203.0.113.10:80 | Whoever the load balancer is exposed to, usually the internet |
You can read all three from one line of kubectl get svc:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
web LoadBalancer 10.96.0.20 203.0.113.10 80:31080/TCP 3m
80:31080/TCP means "Service port 80, node port 31080". The load balancer's address is not part of the spec you
wrote. It appears in the status, filled in by whoever built the load balancer:
status:
loadBalancer:
ingress:
- ip: 203.0.113.10 # some clouds (AWS) give a hostname here instead of an ip
The fields that only matter for this type:
| Field | What it does | Default |
|---|---|---|
spec.type: LoadBalancer | Asks for an external load balancer | ClusterIP |
spec.externalTrafficPolicy | Cluster: any node forwards to any Pod. Local: a node only delivers to its own Pods and keeps the client IP | Cluster |
spec.loadBalancerSourceRanges | Client CIDRs allowed through the load balancer (where the provider supports it) | everyone |
spec.loadBalancerClass | Hands the Service to a specific implementation instead of the cluster's default one. Cannot be changed once set | unset |
spec.allocateLoadBalancerNodePorts | Set to false to skip node ports, for load balancers that send straight to Pod IPs | true |
spec.healthCheckNodePort | The node port the load balancer probes when the policy is Local | allocated for you |
status.loadBalancer.ingress | The address (IP or hostname) once the load balancer exists | empty |
How it really works
Parts:
- You (kubectl apply)
- API server (Service web)
- Cloud controller (service controller)
- Cloud API (AWS, Azure, GCP)
- Service status (ingress 203.0.113.10)
- Load balancer (203.0.113.10)
Connections:
- You to API server: apply
- API server to Cloud controller: watch
- Cloud controller to Cloud API: create
- Cloud API to Load balancer: provisions
- Cloud controller to Service status: writes address
When you apply the Service, the API server does only what it does for a NodePort Service: it allocates a cluster IP and a node port and stores the object. kube-proxy on every node programs the rules for both. Nothing in the control plane you installed with kubeadm knows how to create a load balancer.
The missing piece is the service controller inside the cloud-controller-manager, the component that links a cluster to one specific cloud. Its loop is simple:
- Watch the API server for Services with
type: LoadBalancer. - Call the cloud's API to create a load balancer whose listeners match the Service's
portsand whose targets are the cluster's nodes on the Service's node port. - Write the load balancer's address into
status.loadBalancer.ingress. - Keep it in sync: when nodes join or leave, or the Service's ports change, update the load balancer.
Provisioning is asynchronous. kubectl apply returns at once, EXTERNAL-IP shows <pending>, and the address
appears seconds or minutes later. kubectl describe svc shows the progress as events: EnsuringLoadBalancer, then
EnsuredLoadBalancer.
So the cloud load balancer is only a front door for the node port. From the node onward, the packet takes exactly the path described in the Services lesson: kernel rules written by kube-proxy pick a ready Pod from the EndpointSlice and rewrite the destination to it.
Under the hood · Cluster and Local: what happens to the client's address
With the default externalTrafficPolicy: Cluster, the load balancer may hand a connection to a node that runs none
of the Pods. That node forwards it to a Pod on another node (the "second hop" in the first diagram). For the reply
to come back the same way, the node replaces the packet's source address with its own (SNAT). The Pod therefore
sees a node IP, never the real client.
With externalTrafficPolicy: Local, a node delivers only to Pods running on itself, so no SNAT is needed and the
Pod sees the real client IP. To keep traffic away from nodes with no local Pod, Kubernetes allocates a
healthCheckNodePort; kube-proxy answers on it with HTTP 200 when the node has at least one ready local endpoint
and 503 when it has none, and the load balancer is configured to probe it.
The price of Local is balance: the load balancer spreads connections per node, so a node with one Pod gets
the same share as a node with three. Spread the Pods evenly (topology spread constraints or anti-affinity) when you
use it.
Parts:
- Client (198.51.100.7)
- Cloud load balancer (health-checks nodes)
- Node A (healthz: 200)
- Node B (healthz: 503)
- web Pod (sees 198.51.100.7)
Connections:
- Client to Cloud load balancer
- Cloud load balancer to Node A: traffic
- Cloud load balancer to Node B: no local Pod
- Node A to web Pod: no SNAT
Under the hood · Calling the external IP from inside the cluster
A Pod that calls 203.0.113.10 does not usually leave the cluster and come back. By default kube-proxy also
installs rules for the load balancer's IP on every node, so the connection is short-circuited straight to a Pod,
as if the Pod had called the ClusterIP. That is efficient, but it skips anything the load balancer itself does,
such as TLS termination or the PROXY protocol. For those load balancers the provider sets
status.loadBalancer.ingress[].ipMode: Proxy, which tells kube-proxy to leave the IP alone and let the traffic
travel through the real load balancer.
Under the hood · Deleting: the finalizer that protects your bill
The controller adds the finalizer service.kubernetes.io/load-balancer-cleanup to the Service. When you delete the
Service, it stays in Terminating until the cloud load balancer has been removed, and only then disappears. This
is why you should delete LoadBalancer Services before tearing down a cluster: if the cluster dies first,
nobody is left to remove the load balancers and they keep costing money.
Where it works and where it stays pending
| Cluster | What fulfils type: LoadBalancer |
|---|---|
| Managed cloud (EKS, AKS, GKE) | The cloud's controller: a real cloud load balancer per Service |
| kubeadm on bare VMs or metal | Nothing by default. EXTERNAL-IP stays <pending> until you install something like MetalLB |
| k3s | Built-in ServiceLB: uses the nodes' own addresses as the external IP |
| kind | The separate cloud-provider-kind tool |
| minikube | minikube tunnel, while it runs |
Two more things to weigh before using this type everywhere:
- One load balancer per Service. Ten public Services mean ten cloud load balancers and ten bills. For many HTTP applications behind one address, use one LoadBalancer Service in front of an Ingress controller or a Gateway, and route by host name and path there.
- It works at layer 4. A LoadBalancer Service forwards TCP or UDP connections. It knows nothing about URLs, host names or cookies.
By default the load balancer is public. Every cloud has an annotation that makes it internal (reachable only from your private network) instead. They are provider-specific, so you look them up rather than memorise them:
metadata:
annotations:
service.beta.kubernetes.io/aws-load-balancer-scheme: "internal" # AWS
service.beta.kubernetes.io/azure-load-balancer-internal: "true" # Azure
networking.gke.io/load-balancer-type: "Internal" # GKE
Using it
Example · Expose a Deployment through a load balancer
The Deployment web in namespace shop runs Pods labelled app: web that listen on 8080. Publish it on port 80.
kubectl -n shop expose deployment web --type=LoadBalancer --port=80 --target-port=8080
kubectl -n shop get svc web --watch # wait for EXTERNAL-IP, Ctrl+C to stop
On a cloud cluster the address arrives after a short wait and curl http://<EXTERNAL-IP> answers. On a cluster
with no implementation it stays <pending>; prove the Service through its node port instead:
kubectl -n shop get svc web # PORT(S) shows 80:31080/TCP
kubectl get nodes -o wide # pick any INTERNAL-IP
curl -s http://192.168.1.11:31080
Example · Turn an existing Service into a LoadBalancer, keep the client IP, limit who may call
A ClusterIP Service web already exists. Change its type in place; the cluster IP and the DNS name stay the same,
so clients inside the cluster notice nothing.
kubectl -n shop patch svc web -p '{"spec":{"type":"LoadBalancer","externalTrafficPolicy":"Local"}}'
The declarative form, with the source range added:
apiVersion: v1
kind: Service
metadata:
name: web
namespace: shop
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080
externalTrafficPolicy: Local
loadBalancerSourceRanges:
- 198.51.100.0/24
Check the result:
kubectl -n shop get svc web -o yaml | grep -E 'type:|externalTrafficPolicy|healthCheckNodePort'
healthCheckNodePort appears only because the policy is Local.
Example · Read the address in a script
Use JSONPath on the status. Ask for both fields, because a provider fills in either ip or hostname:
kubectl -n shop get svc web \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}{.status.loadBalancer.ingress[0].hostname}{"\n"}'
An empty line means the load balancer has not been provisioned (yet).
Tricks
Common mistakes
- Waiting for an external IP on a cluster that cannot provide one.
<pending>with no events means there is no load balancer implementation. Use the node port. - Expecting an error. Kubernetes accepts
type: LoadBalanceron any cluster. A missing implementation is silent. - Using
Localand losing traffic. WithexternalTrafficPolicy: Local, a node without a ready local Pod drops traffic sent to its node port. If you test<node IP>:<node port>by hand, pick a node that runs a Pod (kubectl get pods -o wide). - Surprise at the Pod logs. With the default
Clusterpolicy the application logs node IPs as clients. That is SNAT, not a bug; switch toLocalif the application needs the real address. - One load balancer for every small service. It works, and it is billed per load balancer. Put HTTP applications behind a shared Ingress controller or Gateway.
- Deleting the cluster first. Delete LoadBalancer Services before the cluster, or the cloud load balancers are orphaned.
Recap
type: LoadBalancer= ClusterIP + NodePort + a request for an external load balancer.- A controller outside the core (the cloud-controller-manager, MetalLB, ServiceLB) builds it and writes the
address into
status.loadBalancer.ingress. - No such controller means
EXTERNAL-IPstays<pending>; the node port and the cluster IP still work. externalTrafficPolicy: Localkeeps the client IP and avoids the second hop, at the cost of even balancing.- In the exam:
kubectl expose --type=LoadBalancer, read the node port fromPORT(S), and debug from the endpoints outward.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.