Layer 2 mode
How one node answers ARP or NDP for a Service's address, how the speakers agree which node without talking, what happens when that node dies, and where layer 2 runs out.
In layer 2 mode, one node at a time owns each Service address. Its speaker answers the LANLocal area network: the machines on the same switches, such as one office, home or rack.'s "who has this IP?" questions with the node's own network card, so from the outside the node simply looks like it has one more IP address. Nothing else is needed: no special switch, no router configuration, any Ethernet network will do.
Each tab in the figure is one scenario:
| Tab | What it shows |
|---|---|
| Answer ARP | The everyday case: the laptop asks who has .240, only the owner, node-3, answers, and the laptop sends its traffic there. |
| Owner fails | node-3 dies. The other speakers notice through memberlist, node-2 is next in line, and its gratuitous ARPAddress Resolution Protocol: how an IPv4 machine asks who has an address and learns which network card to send to. tells the laptop where .240 went. |
| Stubborn client | An old client ignores the gratuitous ARP and keeps sending to the dead node until its cached answer expires. |
The words in the figure:
| Word | What it means |
|---|---|
| MAC addressA network card's hardware address, like 3c:ec:ef:12:34:56. Switches deliver Ethernet frames by MAC, not by IP. | What an ARP reply gives back: which card to send frames to |
| ARP request | "Who has 192.168.1.240?", broadcast to every machine on the LAN (IPv4) |
| NDPNeighbor Discovery Protocol: the IPv6 version of ARP. neighbor solicitation | The same question for an IPv6 address, sent to a multicast group |
| ARP cacheA short list every machine keeps of recent IP-to-MAC answers, so it doesn't ask before every packet. | Where the client keeps the answer, usually for minutes |
| Gratuitous ARP | An ARP announcement nobody asked for: "192.168.1.240 is now at this MACMedia Access Control address: a network card’s hardware address, like 3c:ec:ef:12:34:56. Switches deliver frames by MAC." |
#How the speakers agree on the owner
Nothing is elected by vote, and nothing is stored. For each address, every speaker takes the list of nodes that could announce it, sorts it by sha256(node name + "#" + address), and announces only if its own node comes first. All speakers compute the same order, so exactly one of them answers.
Because the address is part of 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., different Services end up on different nodes. These are the real orders for six addresses on a three-node cluster:
| Address | Order | Owner | If node-3 fails |
|---|---|---|---|
| 192.168.1.240 | node-3, node-2, node-1 | node-3 | node-2 |
| 192.168.1.241 | node-1, node-2, node-3 | node-1 | node-1 (no change) |
| 192.168.1.242 | node-3, node-2, node-1 | node-3 | node-2 |
| 192.168.1.243 | node-1, node-2, node-3 | node-1 | node-1 (no change) |
| 192.168.1.244 | node-1, node-2, node-3 | node-1 | node-1 (no change) |
| 192.168.1.245 | node-2, node-3, node-1 | node-2 | node-2 (no change) |
Two things follow from the sort:
- Removing a node only moves the addresses it owned. Everything else keeps its owner.
- Adding a node moves an address only if the new node sorts first for it.
The list only holds nodes that may announce: with ready pods for the Service, matching the advertisement's nodeSelectors, and, with externalTrafficPolicy: Local, running one of its pods. An L2Advertisement can also set v0.16.2 preferredNodeSelectors, which sort chosen nodes first and leave the hash to break ties.
#What the order means
Take 192.168.1.245 from the table: its order is node-2, node-3, node-1.
#node-2 answers
It's first, so node-2's speaker answers ARP for .245 with node-2's MAC. node-3 and node-1 stay quiet.
#node-2 goes down, node-3 takes over
The others drop node-2 from the list. node-3 is now first, so it answers, and sends gratuitous ARP to say so.
#node-2 comes back, and takes it back
The list is whole again, node-2 is first again, so .245 moves back to node-2. That's a second short move.
A few things people expect that aren't so:
- The order for an address never drifts. It comes from a hash, so it's the same tomorrow, after a restart, and on every speaker. It only changes when the list of nodes changes, or with preferred nodes.
- Different addresses land on different nodes.
.240belongs to node-3 while.245belongs to node-2, which is how layer 2 spreads Services over nodes. - Only layer 2 has an order. In BGP mode every eligible node advertises at once, and the router spreads the traffic; nothing waits its turn.
#What happens when the owner fails
The second scenario in the figure, step by step:
1. Notice
The speakers check each other with memberlistA library that keeps a list of live members by gossiping between them, so a dead node is noticed in seconds. on port 7946, 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.. A node that stops answering is marked dead within seconds.
2. Re-sort and announce
Every speaker re-sorts without the dead node. The new first node starts answering, and sends gratuitous ARP at once, then every 1.1 seconds for 5 seconds.
Clients that honour gratuitous ARP update their cache straight away, and the address is back within a few seconds. Windows, macOS and Linux all do. Connections that were open through the dead node are lost either way.
#Where layer 2 runs out
What it does well
- Works on any Ethernet network, with no router changes
- Almost nothing to configure: a pool and an L2Advertisement
- No limit on the number of addresses, unlike VRRP's 255
Where it falls short
- All traffic for an address enters through one node, so one node's bandwidth is the ceiling
- Failover depends on clients honouring gratuitous ARP
- Clients (or their router) must be on the same LAN as the nodes
Layer 2 is a failover mechanism, not a load balancer. kube-proxyThe part of Kubernetes on every node that forwards a Service’s traffic to one of its pods. still spreads traffic across all the pods once it reaches the owner, but every byte goes through that one node first. If that's a problem, use BGP mode.
#Periodic announcements
By default the owner only sends gratuitous ARP when it takes an address, and otherwise answers when asked. Since v0.16.2 new it can also repeat them on a timer. Some switches forget where a MAC lives faster than the router forgets the ARP answer, which causes short, regular outages with no failover at all. For those networks, set the --gratuitous-arp-interval flag, or speaker.gratuitousARPInterval in the HelmA package manager for Kubernetes: a chart bundles all of an app’s objects, installed with one command and settings from a values file. chart, in seconds. 0, the default, turns it off.
Pick the largest interval that is still shorter than your switch's MAC ageing time. It applies to every address on the node, so a very short one on a busy node sends a lot of broadcast traffic.
#Compared with Keepalived
MetalLB layer 2
- Notices dead nodes with memberlist
- No virtual router IDs to plan
- As many addresses as your pools hold
- Only works inside one Kubernetes cluster
Keepalived (VRRP)
- Nodes send VRRPVirtual Router Redundancy Protocol: how routers, and Keepalived, share one address and move it when one fails. adverts to each other
- Each group needs a virtual router ID
- At most 255 groups per network
- Works with any VRRP-aware device
To a client, the two look the same: an address that sometimes moves from one machine to another.
#Watch it happen
From another machine on the same LAN, ask who has the address and look at the cache:
arping -I eth0 192.168.1.240
ip neigh show 192.168.1.240Every reply should carry the same MAC: the owner's network card. In the cluster, see which node owns it, and watch the ARP traffic on that node:
kubectl -n metallb-system get servicel2statuses
kubectl describe service web
sudo tcpdump -n -i eth0 arp host 192.168.1.240The ALLOCATED NODE column and the nodeAssigned event in describe service both name the owner. Stop that node and run arping again: within seconds the MAC changes to the next node's, and tcpdump on that node shows the burst of gratuitous ARP.
#In the source
All links are to metallb/metallb at v0.16.2:
speaker/layer2_controller.go:ShouldAnnouncebuilds the candidate list;sortL2Candidatessorts it by preference score, then by the sha256 hash.internal/layer2/arp.goandndp.go: answer requests for owned addresses with the receiving interface's MAC, and build gratuitous packets.internal/layer2/announcer.go:spamLoopsends the 1.1-second, 5-second burst;periodicGARPLoopthe optional timer.internal/speakerlist/speakerlist.go: memberlist setup and the list of live speakers.
Referenced by