Layer 4 Load Balancers Route Connections, Layer 7 Load Balancers Route Requests
A layer 4 load balancer sees IP addresses and TCP/UDP ports, and forwards connections. A layer 7 load balancer understands the application protocol (HTTP host names, paths, headers, cookies) and routes each request. On a bare-metal Kubernetes cluster MetalLB is layer 4 and an ingress controller such as ingress-nginx is layer 7; they aren't alternatives but two halves of one design.
What Each Layer Can Decide
| Layer 4 (MetalLB, kube-proxy) | Layer 7 (ingress-nginx, Traefik) | |
|---|---|---|
| Sees | source and destination IP, port | HTTP method, Host header, path, headers |
| Routes by | address and port | domain name, URL path, header values |
| TLS | passes it through | can terminate it and see the request |
| Typical job | give a Service an IP reachable from the LAN | send app1.example.lan and app2.example.lan to different apps |
The Usual Combination
Giving each web app its own LoadBalancer IP works, but wastes addresses from a small pool and means one certificate setup per app. Instead:
- MetalLB gives one IP to the ingress controller's Service.
- DNS records for every app point to that IP.
- The ingress controller reads the
Hostheader (and path) of each request and forwards it to the right Service, from Ingress rules.
References
- Kubernetes MOC - hub for Kubernetes notes.
- Networking MOC - hub for networking notes.
- MetalLB Layer 2 Mode Fails Over a Service IP, It Does Not Spread Traffic - the layer 4 half on bare metal.
- Kubernetes documentation: Ingress - host- and path-based routing rules.
Review cards
Flashcard What does a layer 4 load balancer use to forward connections?
A layer 4 load balancer forwards connections based on source and destination IP addresses and TCP/UDP ports.
Flashcard How does a layer 7 load balancer handle TLS compared to a layer 4 load balancer?
A layer 7 load balancer can terminate TLS and inspect the request, while a layer 4 load balancer simply passes TLS through.
Flashcard Why give the ingress controller one MetalLB IP instead of one LoadBalancer IP per web app?
Every app's DNS name points to that one IP and the ingress routes by Host header, so adding an app means an Ingress object and a DNS record, not a new address and certificate setup.