BGP mode
How every node advertises a Service's address to your routers, how the routers spread connections across nodes, and why a node going away resets connections.
In BGPBorder Gateway Protocol: how routers tell each other which addresses they can reach, and through whom. mode, every eligible node tells your routers: "send traffic for this address to me." Each node's speaker keeps a BGP session with one or more routers and advertises a route for every Service address. When the routers accept several equal routes, traffic is spread across the nodes. That makes BGP mode a real load balancer, where layer 2 only provides failover.
Each tab in the figure is one scenario:
| Tab | What it shows |
|---|---|
| Peer and advertise | Each node opens a BGP session to the router and advertises a route to .240. The router keeps all three routes. |
| Spread connections | The router hashes each new connection to pick a node, so connections spread out, but every packet of one connection goes to the same node. |
| Node lost | node-3 goes down and its route is withdrawn. The router’s hash now sends many existing connections to a different node, and they reset once. |
The words in the figure:
| Word | What it means |
|---|---|
| ASNAutonomous System Number: the ID of one network's routing domain. 64512–65534 are private, like private IP ranges. | Which network a BGP speaker belongs to. MetalLB uses its own (myASN), the router has another (peerASN) |
| Peer, session | Two BGP speakers connected over TCPTransmission Control Protocol: the reliable, connection-based way most traffic travels, such as web requests. port 179. MetalLB calls the router a BGPPeer |
| UPDATE | A message that advertises a route ("192.168.1.240/32 is reachable through me") or withdraws one |
| Next hop | The address to send packets to for that route: here, the node's own IP |
| ECMPEqual-cost multi-path: a router keeps several equally good routes to one address and spreads connections across them. | The router keeps every equal route and uses them all |
| Hold timer | How long a session may stay silent before the router drops it and its routes |
| BFD | Bidirectional Forwarding Detection: tiny hello packets every few hundred milliseconds, so a dead node is noticed in under a second instead of after the hold timer |
#What MetalLB advertises
- One route per address, as a
/32(or/128for IPv6) by default. ABGPAdvertisementcan widen it withaggregationLength, and add communities or a local preference. - From every node that may announce: nodes with a ready pod for the Service (any pod, anywhere, with the default
Clusterpolicy; a pod on that node withLocal), matching the advertisement's node selectors, and peering with that router. - With the node as next hop. Packets arrive at the node, and kube-proxyThe part of Kubernetes on every node that forwards a Service’s traffic to one of its pods. takes them to a pod as for any Service.
Unlike layer 2, nothing is elected: every eligible node advertises at once.
#How the router spreads connections
Routers spread traffic per connection, not per packet. For each packet they hash a few header fields and use the result to pick a next hop. Every packet of one connection has the same fields, so it always lands on the same node.
3-tuple hash
- Protocol, source IP, destination IP
- Every connection from one client goes to the same node
- Few clients means uneven load
5-tuple hash
- The same, plus source and destination ports
- Different connections from one client spread across nodes
- Closer to an even split: prefer it when your router offers it
Spreading single packets would be worse in both directions: packets of one TCP connection would arrive out of order, and two nodes could send the same connection to two different pods.
#When a node goes away
Router hashes are usually not stable. When the number of next hops changes, because a node failed, was drained, or stopped having pods with Local, 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. of most existing connections now points at a different node. That node has never seen the connection, so it answers with a reset. Clients see "connection reset by peer" once, then reconnect. There's no ongoing loss, just a one-time clean break.
How fast the router notices depends on the session. The native backend uses a 90-second hold timer unless you set holdTime; with the FRR-based backends, a BFDBidirectional Forwarding Detection: a light hello protocol that notices a dead link in well under a second. profile on the peer brings that down to under a second.
Ways to make the break smaller, from metallb.io:
- Resilient ECMP. Some routers have a more stable hash ("resilient ECMP" or "resilient LAGLink aggregation group: several network links bundled to act as one.") that moves only the connections of the node that left.
- An ingress controller in front. Let MetalLB send traffic to an ingress or Gateway, and change your apps behind it freely. Only changes to the ingress pods themselves cause resets.
- Pin and plan. Keep the Service's pods on a fixed set of nodes, and change them when traffic is low.
- Two Services, one move. Bring up the new set under a second address, move clients with DNSDomain Name System: turns names like shop.example.com into IP addresses., then retire the old one.
- Retry on the client. Apps that retry a reset connection hide the break completely.
#Layer 2 or BGP
| Layer 2 | BGP | |
|---|---|---|
| Spreads load across nodes | No, one node per address | Yes, per connection |
| Needs from the network | Nothing | Routers that speak BGP, with multipath |
| When a node fails | A few seconds; its connections are lost | Hold timer or BFD; many connections reset once |
| Clients on other networks | Through a router on the same LANLocal area network: the machines on the same switches, such as one office, home or rack. | Natively |
#Which software speaks BGP
MetalLB can run BGP three ways. FRR-K8s default since v0.16 runs FRRFRRouting: open-source routing software that speaks BGP, BFD and more, like the software in many routers., a full routing suite, in its own DaemonSetA Kubernetes workload that runs one copy of a pod on every node, or on every selected node. and adds BFD, IPv6 and the option to share sessions with other tools. Native is MetalLB's own small BGP speaker inside the speaker pod, without BFD or IPv6 BGP. FRR mode deprecated runs FRR as a sidecar and will be removed. The choice is made at install time; the next page compares them.
#Watch it happen
With the default FRR-K8s backend, check the sessions and what each node advertises:
kubectl -n metallb-system get bgppeers
kubectl -n metallb-system get bgpsessionstates
kubectl -n metallb-system get servicebgpstatuses
kubectl -n metallb-system exec ds/frr-k8s-daemon -c frr -- vtysh -c "show bgp summary"
kubectl -n metallb-system exec ds/frr-k8s-daemon -c frr -- vtysh -c "show bgp ipv4 unicast"The DaemonSet is frr-k8s-daemon when installed from the manifests; with HelmA package manager for Kubernetes: a chart bundles all of an app’s objects, installed with one command and settings from a values file. it's named after the release, metallb-frr-k8s for a release called metallb. bgpsessionstates should show Established for every node and peer, and servicebgpstatuses lists which nodes advertise each Service to which peers. On the router, the route for the address should list one next hop per node, for example show ip route 192.168.1.240 on a Cisco- or FRR-style CLICommand-line interface: the text commands you type on a router or server.. Take a node down and the router's list shrinks by one.
#In the source
All links are to metallb/metallb at v0.16.2:
speaker/bgp_controller.go:ShouldAnnouncedecides whether this node advertises a Service (healthy endpointsThe pods behind a Service that are running and pass their readiness check, so they may receive traffic.,Localpolicy, node selectors, the exclude label).api/v1beta1/bgpadvertisement_types.go:aggregationLengthdefaults to 32 andaggregationLengthV6to 128.internal/bgp/native/native.go: the native speaker, with its 90-second default hold time.internal/bgp/frrk8s/frrk8s.go: turns MetalLB's peers and advertisements into FRR-K8s configuration.
Referenced by