Day 14: keepalived and VRRP
Goal: Two gateways share a virtual IP address. If the active one fails, the other takes over, and the clients barely notice. You measure the failover on a clean stop and on a hard failure, let keepalived monitor the uplink and create a split brain with two gateways claiming the same address · Duration: approx. 75 min · Prerequisite: Days 3 and 4
Briefing
- A client has exactly one default gateway. If that router fails, the client is cut off from the rest of the world, even if a second router stands two meters away. VRRP (Virtual Router Redundancy Protocol) solves this: several routers form a group, one is MASTER and holds the virtual address (VIP), the others are BACKUP and wait.
- The MASTER sends a sign of life (advertisement) to the multicast
address
224.0.0.18every second. If it is missing for three intervals, the BACKUP with the highest priority takes over. - keepalived is the standard implementation on Linux. It puts the VIP on the interface as an additional address and announces it via gratuitous ARP so that clients and switches learn the new MAC address.
┌──────────── sw (Bridge br0) ────────────┐
│ │ │
┌───────┴──────┐ ┌───────┴──────┐ ┌──────┴─────┐
│ gw1 .2 │ │ gw2 .3 │ │ cl .10 │
│ Prio 150 │ │ Prio 100 │ │ GW 10.60.0.1│
│ VIP 10.60.0.1│ │ │ └────────────┘
└───────┬──────┘ └──────┬───────┘
10.61.1.0/30 eth1 eth1 10.61.2.0/30
└────┐ ┌──────┘
┌─┴───┴─┐
│ inet │ 10.70.0.100 (“the Internet”)
└───────┘
Drill 1: Two Gateways, One LAN
The LAN is a bridge in its own namespace sw, like a
switch (Day 3). inet stands for the internet: it has a link
to each gateway and sends replies to the LAN over both.
# on netz
for n in sw gw1 gw2 cl inet; do sudo ip netns add $n; sudo ip -n $n link set lo up; done
sudo ip -n sw link add br0 type bridge
sudo ip -n sw link set br0 up
for n in gw1 gw2 cl; do
sudo ip -n sw link add p-$n type veth peer name eth0 netns $n
sudo ip -n sw link set p-$n master br0
sudo ip -n sw link set p-$n up
sudo ip -n $n link set eth0 up
done
sudo ip -n gw1 addr add 10.60.0.2/24 dev eth0
sudo ip -n gw2 addr add 10.60.0.3/24 dev eth0
sudo ip -n cl addr add 10.60.0.10/24 dev eth0
sudo ip -n cl route add default via 10.60.0.1
sudo ip -n gw1 link add eth1 type veth peer name eth1 netns inet
sudo ip -n gw2 link add eth1 type veth peer name eth2 netns inet
sudo ip -n gw1 addr add 10.61.1.2/30 dev eth1
sudo ip -n inet addr add 10.61.1.1/30 dev eth1
sudo ip -n gw2 addr add 10.61.2.2/30 dev eth1
sudo ip -n inet addr add 10.61.2.1/30 dev eth2
sudo ip -n gw1 link set eth1 up
sudo ip -n gw2 link set eth1 up
sudo ip -n inet link set eth1 up
sudo ip -n inet link set eth2 up
sudo ip -n inet addr add 10.70.0.100/32 dev lo
sudo ip -n gw1 route add default via 10.61.1.1
sudo ip -n gw2 route add default via 10.61.2.1
sudo ip netns exec inet sysctl -qw net.ipv4.conf.all.ignore_routes_with_linkdown=1
sudo ip -n inet route add 10.60.0.0/24 nexthop via 10.61.1.2 dev eth1 nexthop via 10.61.2.2 dev eth2
for n in gw1 gw2 inet; do sudo ip netns exec $n sysctl -qw net.ipv4.ip_forward=1; done
sudo ip netns exec gw1 ping -c1 -W1 10.70.0.100 | grep -o '1 received'
sudo ip netns exec cl ping -c1 -W1 10.70.0.100 | grep receivedExpected:
1 received
1 packets transmitted, 0 received, 100% packet loss, time 0ms
The gateways reach the internet, the client does not: its gateway
10.60.0.1 does not exist yet.
ignore_routes_with_linkdown in inet makes sure
that a route over a dead link is no longer used (Day 4).
Save your progress? Sign up for free
Drill 2: Setting Up keepalived
One configuration per gateway, only the priority differs:
# on netz
sudo tee /etc/keepalived/gw1.conf >/dev/null <<'EOF'
vrrp_instance LAN {
state BACKUP
interface eth0
virtual_router_id 60
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass lab-vrrp
}
virtual_ipaddress {
10.60.0.1/24
}
}
EOF
sudo sed 's/priority 150/priority 100/' /etc/keepalived/gw1.conf | sudo tee /etc/keepalived/gw2.conf >/dev/null| Line | Meaning |
|---|---|
state BACKUP |
Initial state. Both start as BACKUP, the priority decides who becomes MASTER |
virtual_router_id 60 |
Group number, 1 to 255, must be unique in the LAN |
priority |
higher wins |
advert_int 1 |
sign of life every second |
auth_pass |
a password in plain text. Protects against mistakes, not against attacks |
keepalived can switch into a namespace by itself:
-s gw1. -P starts only the VRRP part, without
the load balancer functions:
# on netz
sudo keepalived -P -s gw1 -f /etc/keepalived/gw1.conf
sudo keepalived -P -s gw2 -f /etc/keepalived/gw2.conf
sleep 5
sudo ls /run/keepalived/gw1
sudo ip -n gw1 -br addr show eth0
sudo ip -n gw2 -br addr show eth0
sudo journalctl -t Keepalived_vrrp_gw1 --no-pager | tail -2 | cut -c1-120
sudo ip netns exec cl ping -c1 -W1 10.70.0.100 | grep -o '1 received'Expected:
keepalived.pid
vrrp.pid
eth0@if3 UP 10.60.0.2/24 10.60.0.1/24 fe80::d05e:9ff:feed:f476/64
eth0@if4 UP 10.60.0.3/24 fe80::828:a7ff:fe0c:28c6/64
Nov 24 01:10:13 netz Keepalived_vrrp_gw1[24722]: (LAN) Entering BACKUP STATE (init)
Nov 24 01:10:16 netz Keepalived_vrrp_gw1[24722]: (LAN) Entering MASTER STATE
and 1 received. gw1 won after three seconds
and carries the VIP in addition to its own address. Each instance has
its process files under /run/keepalived/<namespace>/
and its own identifier in the journal
(Keepalived_vrrp_gw1). The signs of life in the LAN:
# on netz
sudo ip netns exec cl tcpdump -i eth0 -n -c 2 vrrp 2>/dev/null
sudo ip netns exec cl ip neigh show 10.60.0.1
sudo ip -n gw1 link show eth0 | grep etherExpected:
01:10:34.543785 IP 10.60.0.2 > 224.0.0.18: VRRPv2, Advertisement, vrid 60, prio 150, authtype simple, intvl 1s, length 20
01:10:35.544233 IP 10.60.0.2 > 224.0.0.18: VRRPv2, Advertisement, vrid 60, prio 150, authtype simple, intvl 1s, length 20
10.60.0.1 dev eth0 lladdr d2:5e:09:ed:f4:76 REACHABLE
link/ether d2:5e:09:ed:f4:76 brd ff:ff:ff:ff:ff:ff link-netns sw
For the client, 10.60.0.1 has the MAC address of
gw1. By default keepalived uses the real MAC of the MASTER,
not the virtual MAC from the VRRP standard. On failover the client
therefore has to learn a new MAC, which is why the new MASTER sends
gratuitous ARP.
Save your progress? Sign up for free
Drill 3: Clean Stop and Return
From now on a ping every 10 milliseconds measures, as on Day 11.
First keepalived on gw1 is stopped cleanly, for example for
maintenance:
# on netz
sudo ip netns exec cl ping -i 0.01 -c 800 -q 10.70.0.100 > /tmp/p.txt &
sleep 2; sudo kill $(sudo cat /run/keepalived/gw1/keepalived.pid)
wait; tail -2 /tmp/p.txt
sudo ip -n gw2 -br addr show eth0
sudo ip netns exec cl ip neigh show 10.60.0.1Expected:
800 packets transmitted, 800 received, 0% packet loss, time 8802ms
rtt min/avg/max/mdev = 0.043/0.121/1.024/0.045 ms
eth0@if4 UP 10.60.0.3/24 10.60.0.1/24 fe80::828:a7ff:fe0c:28c6/64
10.60.0.1 dev eth0 lladdr 0a:28:a7:0c:28:c6 REACHABLE
Not a single packet lost. When shutting down,
keepalived sends a last sign of life with priority 0: “I’m leaving, take
over.” gw2 does not wait three seconds but takes over
immediately. The client has learned the MAC of gw2.
gw1 comes back:
# on netz
sudo ip netns exec cl ping -i 0.01 -c 800 -q 10.70.0.100 > /tmp/p.txt &
sleep 2; sudo keepalived -P -s gw1 -f /etc/keepalived/gw1.conf
wait; tail -2 /tmp/p.txt
sudo ip -n gw1 -br addr show eth0 | grep -c 10.60.0.1Expected:
800 packets transmitted, 800 received, 0% packet loss, time 8796ms
rtt min/avg/max/mdev = 0.053/0.131/2.735/0.104 ms
1
gw1 has the higher priority and takes the VIP back
(preemption), again without loss. If you do not want
that, for example because a freshly started router should learn its
routes first, set nopreempt or
preempt_delay 60.
Tried it:
kill -9on keepalived is not a test for a failure. Without a chance to clean up, the VIP stays ongw1,gw2additionally takes it over after three seconds, and both gateways carry the same address. The ping notices nothing, becausegw1keeps forwarding. If you want to simulate a dead router, you have to take away its link.
Save your progress? Sign up for free
Drill 4: The Hard Failure
gw1 loses its link to the switch, as if the power were
gone or the cable pulled:
# on netz
sudo ip netns exec cl ping -i 0.01 -c 1000 -q 10.70.0.100 > /tmp/p.txt &
sleep 2; sudo ip -n sw link set p-gw1 down
wait; tail -2 /tmp/p.txt
sudo journalctl -t Keepalived_vrrp_gw2 --no-pager | tail -1 | cut -c1-120Expected:
1000 packets transmitted, 738 received, 26.2% packet loss, time 11010ms
rtt min/avg/max/mdev = 0.045/0.134/5.460/0.203 ms
Nov 24 01:11:38 netz Keepalived_vrrp_gw2[24726]: (LAN) Entering MASTER STATE
262 packets, 2.6 seconds. gw2 no longer hears signs of
life and waits out the Master Down Interval: three
times advert_int plus a small delay that depends on its own
priority (the higher, the shorter). With a 1-second interval and
priority 100, that is a good 3.6 seconds from the last sign of life,
which was already one second old here. It gets shorter with a smaller
advert_int; fractions of a second are only allowed by VRRP
version 3 (vrrp_version 3). Link back, gw1
takes over again:
# on netz
sudo ip -n sw link set p-gw1 up
sleep 5
sudo ip -n gw1 -br addr show eth0Expected:
eth0@if3 UP 10.60.0.2/24 10.60.0.1/24 fe80::d05e:9ff:feed:f476/64
Save your progress? Sign up for free
Drill 5: The Uplink Fails
Now gw1 stays healthy in the LAN, but its link to the
internet fails:
# on netz
sudo ip -n gw1 link set eth1 down
sleep 3
sudo ip -n gw1 -br addr show eth0
sudo ip netns exec cl ping -c3 -W1 10.70.0.100 | grep -E 'From|received'
sudo ip -n gw1 link set eth1 upExpected:
eth0@if3 UP 10.60.0.2/24 10.60.0.1/24 fe80::d05e:9ff:feed:f476/64
From 10.60.0.2 icmp_seq=1 Destination Net Unreachable
From 10.60.0.2 icmp_seq=2 Destination Net Unreachable
From 10.60.0.2 icmp_seq=3 Destination Net Unreachable
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2044ms
gw1 stays MASTER; VRRP only sees the LAN. It accepts
every packet and has no way out anymore, while gw2 with a
working uplink waits next to it. track_interface makes the
uplink a condition: if it fails, the instance goes into the FAULT state
and gives up the VIP.
# on netz
sudo sed -i 's/ advert_int 1/ advert_int 1\n track_interface {\n eth1\n }/' /etc/keepalived/gw1.conf /etc/keepalived/gw2.conf
sudo sed -n '6,9p' /etc/keepalived/gw1.conf
sudo kill -HUP $(sudo cat /run/keepalived/gw1/keepalived.pid) $(sudo cat /run/keepalived/gw2/keepalived.pid)
sleep 3
sudo ip netns exec cl ping -i 0.01 -c 800 -q 10.70.0.100 > /tmp/p.txt &
sleep 2; sudo ip -n gw1 link set eth1 down
wait; tail -2 /tmp/p.txt
sudo ip -n gw2 -br addr show eth0
sudo journalctl -t Keepalived_vrrp_gw1 --no-pager | tail -2 | cut -c1-120Expected:
advert_int 1
track_interface {
eth1
}
800 packets transmitted, 559 received, +2 errors, 30.125% packet loss, time 8800ms
rtt min/avg/max/mdev = 0.044/0.125/0.817/0.044 ms
eth0@if4 UP 10.60.0.3/24 10.60.0.1/24 fe80::828:a7ff:fe0c:28c6/64
Nov 24 01:12:13 netz Keepalived_vrrp_gw1[25027]: Netlink reports eth1 down
Nov 24 01:12:13 netz Keepalived_vrrp_gw1[25027]: (LAN) Entering FAULT STATE
SIGHUP reloads the configuration without giving up the
VIP. The uplink fails, gw1 goes into FAULT,
gw2 takes over. 241 packets, 2.4 seconds: in the FAULT
state gw1 simply falls silent, without a farewell at
priority 0, and gw2 waits out the Master Down Interval.
Better than a gateway without an exit that stays MASTER forever.
Tried it: Instead of FAULT,
track_interfacecan lower the priority (eth1 weight -60, i.e. 90 instead of 150). Here that took longer, a good 5 seconds, than the FAULT route; according to the journal, four seconds passed between the lowered priority andgw1switching to BACKUP.
Uplink back:
# on netz
sudo ip -n gw1 link set eth1 up
sleep 4
sudo ip -n gw1 -br addr show eth0 | grep -c 10.60.0.1Expected: 1
Besides interfaces, keepalived also monitors scripts
(vrrp_script with track_script), such as a
ping to the internet or pg_isready for a database. That is
the approach from “System Design hands-on” (Day 14), where the VIP
depends on the state of PostgreSQL.
Save your progress? Sign up for free
Exercise: Speed drills
- Which gateway is currently MASTER? Check without looking at the journal. Target time: 30 s
- Which group and which priority are in the signs of life in the LAN? Target time: 20 s
- Which MAC address does the gateway have from the client’s point of view? Target time: 15 s
Solution
# on netz
for g in gw1 gw2; do sudo ip -n $g -br addr show eth0 | grep -q 10.60.0.1 && echo "MASTER: $g"; done
sudo ip netns exec cl tcpdump -i eth0 -n -c 1 vrrp 2>/dev/null | grep -o 'vrid [0-9]*, prio [0-9]*'
sudo ip netns exec cl ip neigh show 10.60.0.1Expected:
MASTER: gw1
vrid 60, prio 150
10.60.0.1 dev eth0 lladdr d2:5e:09:ed:f4:76 REACHABLE
Save your progress? Sign up for free
Challenge
A colleague “hardens” gw2 with a firewall rule:
# on netz
sudo ip netns exec gw2 nft add table inet wand
sudo ip netns exec gw2 nft add chain inet wand rein '{ type filter hook input priority 0; }'
sudo ip netns exec gw2 nft add rule inet wand rein ip protocol vrrp drop
clearShortly afterwards, monitoring reports “VRRP state change on gw2,” and individual connections from the LAN break off. Find out what is going on and prove it from the client.
Hint: How many devices answer an ARP request for
10.60.0.1?
Solution
# on netz
for g in gw1 gw2; do echo "$g: $(sudo ip -n $g -br addr show eth0)"; done
sudo journalctl -t Keepalived_vrrp_gw2 --no-pager | tail -1 | cut -c1-120
sudo ip netns exec cl arping -c 4 -I eth0 10.60.0.1 2>&1 | head -5
sudo ip netns exec cl tcpdump -i eth0 -n -c 2 vrrp 2>/dev/nullExpected:
gw1: eth0@if3 UP 10.60.0.2/24 10.60.0.1/24 fe80::d05e:9ff:feed:f476/64
gw2: eth0@if4 UP 10.60.0.3/24 10.60.0.1/24 fe80::828:a7ff:fe0c:28c6/64
Nov 24 01:13:20 netz Keepalived_vrrp_gw2[24726]: (LAN) Entering MASTER STATE
ARPING 10.60.0.1
42 bytes from 0a:28:a7:0c:28:c6 (10.60.0.1): index=0 time=21.309 usec
42 bytes from d2:5e:09:ed:f4:76 (10.60.0.1): index=1 time=64.789 usec
42 bytes from 0a:28:a7:0c:28:c6 (10.60.0.1): index=2 time=18.944 usec
42 bytes from d2:5e:09:ed:f4:76 (10.60.0.1): index=3 time=92.663 usec
01:13:26.356519 IP 10.60.0.3 > 224.0.0.18: VRRPv2, Advertisement, vrid 60, prio 100, authtype simple, intvl 1s, length 20
01:13:26.356768 IP 10.60.0.2 > 224.0.0.18: VRRPv2, Advertisement, vrid 60, prio 150, authtype simple, intvl 1s, length 20
Split brain: Both gateways are MASTER and carry the
VIP. gw2 drops the signs of life from gw1,
considers it dead and takes over. Two MAC addresses answer an ARP
request alternately, and the client sends its packets sometimes here,
sometimes there. Signs of life from both are visible in the LAN at the
same time: that must never happen in a VRRP group. Here both paths
happen to lead into the same internet; with NAT or stateful firewalls
(Day 15), connections break.
# on netz
sudo ip netns exec gw2 nft delete table inet wand
sleep 3
for g in gw1 gw2; do echo "$g: $(sudo ip -n $g -br addr show eth0)"; done
sudo journalctl -t Keepalived_vrrp_gw2 --no-pager | tail -1 | cut -c1-120Expected:
gw1: eth0@if3 UP 10.60.0.2/24 10.60.0.1/24 fe80::d05e:9ff:feed:f476/64
gw2: eth0@if4 UP 10.60.0.3/24 fe80::828:a7ff:fe0c:28c6/64
Nov 24 01:13:40 netz Keepalived_vrrp_gw2[24726]: (LAN) Entering BACKUP STATE
For VRRP, every firewall on the gateways needs a rule that allows
protocol 112 from the other members. If you cannot let multicast through
(some clouds, some switches), switch keepalived to direct packets with
unicast_peer.
Save your progress? Sign up for free
Checkpoint
# on netz
sudo ip netns exec cl ping -c1 -W1 10.70.0.100 | grep -o '1 received'
pgrep -c keepalivedExpected:
1 received
4
Two instances with two processes each (main process and VRRP process).
# on netz
grep -c track_interface /etc/keepalived/gw1.conf /etc/keepalived/gw2.conf
sudo ip netns exec gw2 nft list ruleset | wc -lExpected:
/etc/keepalived/gw1.conf:1
/etc/keepalived/gw2.conf:1
0
Clean-up
# on netz
sudo kill $(sudo cat /run/keepalived/gw1/keepalived.pid) $(sudo cat /run/keepalived/gw2/keepalived.pid)
sleep 2
sudo rm /etc/keepalived/gw1.conf /etc/keepalived/gw2.conf
for n in sw gw1 gw2 cl inet; do sudo ip netns del $n; done
rm -f /tmp/p.txt
ip netns list | wc -l
pgrep -a keepalived || echo "kein keepalived"
ls /etc/keepalivedExpected:
0
kein keepalived
keepalived.conf.sample
Practice note
- Maintenance with a clean stop, tests with a link
failure.
systemctl stop keepalivedhands over without loss. A real failure costs the Master Down Interval, around 3.6 seconds with default values. - Monitor what the gateway is there for. A MASTER
without an uplink or without the service the VIP serves is worse than a
dead one, because nobody takes over.
track_interfaceandvrrp_scriptbelong in every configuration. - Split brain is the typical VRRP failure. The cause
is almost always a firewall or a switch that does not let the signs of
life through. Recognizable by two MAC addresses in
arpingand by signs of life from several routers in the capture. - Choose preemption deliberately. Taking back is
convenient but costs a second failover. For routers that must first
learn routes or synchronize connection state after startup,
preempt_delayis the safe choice.