Skip to content
lorebow

How MetalLB works

The controller and the speaker, how a Service gets its address, how one node or many get chosen to announce it, and what MetalLB leaves to Kubernetes.

KFounder and editor Written for MetalLB v0.16.2 1,108 words 5 min read Find it on the map

MetalLB is two programs that never talk to each other directly. Both watch the Kubernetes APIApplication Programming Interface: the requests one program accepts from others, like the Kubernetes API or a cloud provider’s API.: the controller decides which address a Service gets, and the speakers decide which node tells the network about it. Everything else on this page follows from that split.

#The parts in metallb-system

Each tab in the figure is one scenario:

TabWhat it shows
Hand out an addressThe controller, one pod for the whole cluster, picks a free address from your pool and writes it on the Service.
Layer 2One speaker per node watches the same Service; they agree that node-2 owns .240, and only it answers the laptop.
BGPEach speaker hands its routes to the frr-k8s pod on its node, and FRR advertises them to your router.
Bad configThe webhook, served by the controller pod, rejects a broken pool before it is stored.

Layer 2 mode means answering ARPAddress Resolution Protocol: how an IPv4 machine asks who has an address and learns which network card to send to. and NDPNeighbor Discovery Protocol: the IPv6 version of ARP., the questions machines ask to find who has an address; BGP mode means advertising routes to your routers. The speaker runs on the host networkThe pod shares the node's own network interfaces and addresses, so it can answer ARP and open BGP sessions as the node itself.. The FRR-K8s backend default in v0.16 runs FRRFRRouting: open-source routing software that speaks BGP, BFD and more, like the software in many routers., the routing software, beside it; with the smaller "native" backend the speaker speaks BGPBorder Gateway Protocol: how routers tell each other which addresses they can reach, and through whom. itself and there's no frr-k8s-daemon.

PartRuns asIts one job
controllerDeployment, one podHands out addresses from your pools, and serves the validating webhook
speakerDaemonSet, one pod per nodeTells the network where an address is: answers ARP and NDP, or passes routes to BGP
frr-k8sDaemonSet, one pod per nodeRuns FRR, which holds the BGP sessions. FRR-K8s backend only

Your configuration is a set of custom resources in the same namespace: IPAddressPool says which addresses MetalLB may hand out, and L2Advertisement or BGPAdvertisement says how to announce them. With no pools, MetalLB starts and does nothing.

#From kubectl apply to the first reply

Each tab in the figure is one scenario:

TabWhat it shows
Assign and announceThe normal path: the controller takes the first free address, the speakers agree which node announces it, and the first request reaches a pod.
No free addressEvery address in the pool is taken. The Service waits with an AllocationFailed event until an address frees up or the pool grows.
Pool changesSomeone edits the pool so the Service’s address is no longer in it. The controller moves the Service to a new address, and a speaker announces that one.

#How the controller picks an address

For each Service with type: LoadBalancer and no address yet, the controller tries, in this order:

  1. An address you asked for, with the metallb.io/loadBalancerIPs annotation (or the older spec.loadBalancerIP). If no pool holds it, or another Service already uses it, assignment fails.
  2. A pool you asked for, with the metallb.io/address-pool annotation.
  3. Pools that claim the Service: pools whose serviceAllocation names its namespace or matches its labels, highest priority first.
  4. Any other pool with autoAssign left on (the default).

Inside a pool it takes the first free address of the range, so the first Service in 192.168.1.240-192.168.1.250 gets .240. It writes the address into the Service's status and adds an IPAllocated event.

It keeps addresses

A Service keeps its address across restarts of MetalLB. Only if you edit or delete the pool so the address is no longer allowed does the controller clear it, with a ClearAssignment event, and pick a new one.

It never gives up

With no free address, the Service gets an AllocationFailed warning and stays <pending>. The controller tries again whenever a Service is deleted or the configuration changes.

#How the speakers pick a node

Each speaker sees every Service the controller has given an address to, and works out on its own whether its node should announce it.

Then the two modes differ:

Layer 2: one node

  • Every speaker sorts the nodes that are left by sha256(node name + "#" + address).
  • It announces only if its own node comes first. All speakers run the same sum, so they agree without talking.
  • The hashA calculation that turns its input, here a node name and an address, into a number that looks random but is always the same for the same input. includes the address, so each address has its own order of nodes. The order belongs to the IP, not to a node, and different Services land on different nodes.
  • When a node fails, memberlistA library that keeps a list of live members by gossiping between them, so a dead node is noticed in seconds. (port 7946) tells the others and the next node in the order takes over.

BGP: every node that's left

  • Each speaker advertises a route for the address to its BGP peers.
  • The routers keep all the routes and spread connections across the nodes.
  • When a node fails, its BGP session drops and its route is withdrawn.
  • Nothing has to be elected, so there's no leader to lose.

Each speaker that announces a Service adds a nodeAssigned event to it, for example announcing from node "node-2" with protocol "layer2". Layer 2 mode and BGP mode each get a page of their own next.

#What MetalLB doesn't do

  • It doesn't carry traffic. Once a packet reaches a node, kube-proxyThe part of Kubernetes on every node that forwards a Service’s traffic to one of its pods. or your network plugin takes it to a pod, the same as for a ClusterIPA Service's address inside the cluster, reachable only from pods and nodes.. Reaching the address from a node therefore proves nothing about MetalLB.
  • It doesn't check your app. It only knows whether the Service has ready endpointsThe pods behind a Service that are running and pass their readiness check, so they may receive traffic.. HTTPHypertext Transfer Protocol: how browsers and web servers talk. routing, TLSTransport Layer Security: the encryption behind HTTPS. and retries belong to an ingress controllerAn HTTP proxy inside the cluster (an Ingress controller or a Gateway API implementation) that routes requests to Services by host name and path. or a Gateway behind it.

#Watch it happen

With MetalLB installed and a pool configured:

bash
kubectl -n metallb-system get deployment,daemonset
kubectl -n metallb-system get ipaddresspools,l2advertisements
kubectl get service web
kubectl describe service web
kubectl -n metallb-system get servicel2statuses

get service shows the address under EXTERNAL-IP. The events at the end of describe service name both parts at work: IPAllocated from metallb-controller and nodeAssigned from metallb-speaker. In layer 2 mode, servicel2statuses has one object per announced Service, naming the node that announces it and its interfaces.

#In the source

All links are to metallb/metallb at v0.16.2:

  • controller/service.go: convergeBalancer keeps or clears an address, and allocateIPs tries a requested address, then a requested pool, then the rest.
  • internal/allocator/allocator.go: Allocate checks pinned pools before the others; getIPFromCIDR walks a range from its first address.
  • speaker/layer2_controller.go: ShouldAnnounce builds the candidate list, and sortL2Candidates sorts it by preference, then by the sha256 hash.
  • speaker/main.go: where the speaker records the nodeAssigned event.

Referenced by