Introduction to MetalLB
What MetalLB is, why a bare-metal Kubernetes cluster needs it, and the two ways it makes a Service's address reachable.
MetalLB gives LoadBalancer Services a real IP address on clusters that don't run on a cloud. You give it a range of addresses you own. When a Service asks for a load balancer, MetalLB picks an address from that range and tells your network where to send traffic for it, using three protocols your network already speaks:
| Stands for | In one line | |
|---|---|---|
| ARP | Address Resolution Protocol | An IPv4 machine asks "who has this IP?" and learns which network card to send to |
| NDP | Neighbor Discovery Protocol | The same question and answer, for IPv6 |
| BGP | Border Gateway Protocol | Routers tell each other which addresses they can reach, and through whom |
#Why a bare-metal cluster needs it
A Service of type: LoadBalancer asks for an address that clients outside the cluster can reach. Kubernetes doesn't create one itself. It waits for a controller to do it, and the only ones it ships with call a cloud provider's APIApplication Programming Interface: the requests one program accepts from others, like the Kubernetes API or a cloud provider’s API.: AWSAmazon Web Services, Amazon’s cloud., Azure, Google Cloud and the rest. On your own servers nothing answers, so the Service stays <pending> for good.
Each tab in the figure is one scenario:
| Tab | What it shows |
|---|---|
| On a cloud | What Kubernetes was built for. The cloud controller asks the provider for a load balancer and writes its public address onto the Service. |
| Bare metal | The same Service on your own servers. Nothing answers, so EXTERNAL-IP stays <pending> and clients have nothing to connect to. |
| With MetalLB | MetalLB gives the Service an address from its pool and answers for it on node-1. Traffic goes straight to the node, then on to a pod. |
Without MetalLB you're left with two weaker options:
| Option | What you get | Why it falls short |
|---|---|---|
NodePort | A port from 30000–32767 opened on every node | Clients need a node's address and an odd port; if that node goes down, they have to switch to another one by hand |
externalIPs | Kubernetes accepts traffic for addresses you list | Nothing makes the network send those addresses to a node; you route them yourself, and nothing moves them when a node fails |
LoadBalancer with MetalLB | One stable address per Service, on a normal port | MetalLB hands it out, announces it, and moves it when a node fails |
#What MetalLB does
1. Hands out an address
You give MetalLB pools of addresses. Each LoadBalancer Service gets one, and gives it back when it's deleted. If the pools run out, Services wait.
2. Announces it
An address is no use until the network knows which machine has it. MetalLB answers ARP or NDP for it, or advertises it to your routers over BGP.
How MetalLB works follows one Service from kubectl apply to its first reply.
#Two ways to announce an address
Each tab in the figure is one scenario:
| Tab | What it shows |
|---|---|
| Layer 2 | One node answers "who has 192.168.1.240?" and gets all the traffic. When it dies, another node takes over and tells the network with a gratuitous ARP, an announcement nobody asked for. |
| BGP | Every node tells the router "send 192.168.1.240 to me". The router spreads connections across all three, and stops using a node when its route disappears. |
| Layer 2 mode | BGP mode | |
|---|---|---|
| How | One node answers ARP or NDP for the address | Every node advertises a route to your routers |
| You need | Any Ethernet network, nothing special | Routers that speak BGP, and that you control |
| Traffic | All of it enters through one node | Spread across nodes, one connection at a time |
| When a node fails | Another node takes the address over in a few seconds | Its route is withdrawn; connections that were going to it reset |
| Main limit | One node's bandwidth | Connections reset whenever the set of nodes changes |
Most home labs and small clusters start with layer 2. BGP is for data centres where you already run BGP and want the load spread with ECMPEqual-cost multi-path: a router keeps several equally good routes to one address and spreads connections across them..
#What you need
- A Kubernetes cluster that doesn't already have a network load balancer. k3s's built-in one (ServiceLB) has to be turned off, for example.
- A network plugin that works with MetalLB. Most do; Calico and kube-router need care in BGP mode.
- Some IPv4 or IPv6 addresses for MetalLB to hand out, that nothing else on the network uses.
- For layer 2: TCPTransmission Control Protocol: the reliable, connection-based way most traffic travels, such as web requests. and UDPUser Datagram Protocol: a lighter way to send single packets, without a connection. port 7946 open between nodes. The speakers use it to notice when a node goes away, with memberlistA library that keeps a list of live members by gossiping between them, so a dead node is noticed in seconds..
- For BGP: one or more routers that speak BGP.
#How mature it is
MetalLB calls itself beta, mostly because of its configuration format, and has been run in production by many companies for years. What that means in practice:
- Configuration changes are announced first. When a field changes, one release understands both the old and the new form, so you can update your objects before upgrading again. The release notes say what to change.
- Kubernetes versions: the maintainers keep MetalLB working with the Kubernetes versions that are still supported; older ones get best effort.
- A small team. MetalLB was started by one person, then handed to a small group of volunteer maintainers. There is no company behind it, so bug reports and fixes from users matter.
#In the source
MetalLB lives at github.com/metallb/metallb. The pieces these docs link to:
controller/: hands out addresses.speaker/: announces them, withlayer2_controller.goandbgp_controller.gofor the two modes.api/: the custom resources (IPAddressPool,L2Advertisement,BGPPeerand the rest).charts/metallb/andconfig/manifests/: the Helm chart and the plain manifests.configsamples/: example configurations.
Two more repositories: metallb/frr-k8s, the BGP backend MetalLB uses by default, and metallb/metallb-operator, an OperatorA program in the cluster that installs and manages an app from a custom object, here a MetalLB object. that installs and upgrades MetalLB.