Skip to content
Linux networking · Chapter 17 of 20 Day 15: The vRouter

Day 15: The vRouter

Goal: Two Linux namespaces become a highly available gateway for a company LAN, the way it runs as a virtual router in many offices, data centers and clouds: BGP to the provider with FRR, a virtual address with keepalived, NAT with nftables, DHCP and DNS with dnsmasq. You measure the failover, see what gets lost along the way, and find a fault that only shows up when things go wrong · Duration: approx. 120 min · Prerequisite: Days 4, 6, 12 and 14

Briefing

A vRouter is not a product of its own but a role: Linux with the tools from this course, assembled into a device that connects a network to the outside world. Today you build it twice, so that the failure of one device does not cut off the LAN. You already know every part:

Task Tool learned on
Forwarding and routing table kernel, ip route, forwarding Day 4
Learn the default route from the provider FRR with BGP, filters Day 12
one gateway address for two devices keepalived, VRRP Day 14
private addresses to the internet nftables masquerade, conntrack Days 2, 5
addresses and names for clients dnsmasq as DHCP and DNS server Day 6

This also settles the term from the preface: Linux routing is what the kernel and FRR do (Day 4 and Part B). The vRouter is the device you build from it today.

                       ┌───────────────── sw (Bridge br0), LAN 192.168.10.0/24 ─────────────────┐
                       │                          │                                              │
              ┌────────┴─────────┐       ┌────────┴─────────┐                            ┌───────┴───────┐
              │ vr1 .2  Prio 150 │       │ vr2 .3  Prio 100 │                            │ c1 (DHCP)     │
              │ VIP .1  dnsmasq  │       │ dnsmasq          │                            │ GW + DNS = .1 │
              │ NAT  FRR AS65010 │       │ NAT  FRR AS65010 │                            └───────────────┘
              └────────┬─────────┘       └────────┬─────────┘
         198.51.100.0/30 eth1               eth1 198.51.100.4/30
                       └──────────┐   ┌───────────┘
                               ┌──┴───┴──┐
                               │   isp   │ AS 65100, web server 203.0.113.80:8080
                               └─────────┘

The addresses 198.51.100.0/24 and 203.0.113.0/24 are reserved for documentation and stand in for real public addresses here.

Drill 1: LAN, Provider and the Internet

# on netz
for n in sw vr1 vr2 c1 isp; 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 vr1 vr2 c1; 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 vr1 addr add 192.168.10.2/24 dev eth0
sudo ip -n vr2 addr add 192.168.10.3/24 dev eth0
sudo ip -n vr1 link add eth1 type veth peer name eth1 netns isp
sudo ip -n vr2 link add eth1 type veth peer name eth2 netns isp
sudo ip -n vr1 addr add 198.51.100.2/30 dev eth1
sudo ip -n isp addr add 198.51.100.1/30 dev eth1
sudo ip -n vr2 addr add 198.51.100.6/30 dev eth1
sudo ip -n isp addr add 198.51.100.5/30 dev eth2
sudo ip -n vr1 link set eth1 up
sudo ip -n vr2 link set eth1 up
sudo ip -n isp link set eth1 up
sudo ip -n isp link set eth2 up
sudo ip -n isp addr add 203.0.113.80/32 dev lo
sudo ip netns exec vr1 sysctl -qw net.ipv4.ip_forward=1
sudo ip netns exec vr2 sysctl -qw net.ipv4.ip_forward=1
mkdir -p ~/vrouter/web
echo "Hallo aus dem Internet" > ~/vrouter/web/index.html
head -c 5000000 /dev/urandom > ~/vrouter/web/gross.bin
sudo ip netns exec isp sudo -u ubuntu python3 -m http.server 8080 --directory /home/ubuntu/vrouter/web >/dev/null 2>&1 &

c1 has no address yet; it gets one via DHCP. The provider does not know the LAN and never will: everything that comes from the LAN carries the address of a vRouter after NAT.

Save your progress? Sign up for free

Drill 2: BGP to the Provider

FRR with bgpd in all three routers, as on Day 12:

# on netz
for r in vr1 vr2 isp; do
  sudo mkdir -p /etc/frr/$r
  sudo sed 's/^bgpd=no/bgpd=yes/' /etc/frr/daemons | sudo tee /etc/frr/$r/daemons >/dev/null
  sudo touch /etc/frr/$r/frr.conf /etc/frr/$r/vtysh.conf
  sudo chown -R frr:frr /etc/frr/$r
  sudo systemctl start frr@$r
done

The provider sends each customer only a default route (default-originate) and accepts nothing. This is how most business connections with BGP work:

# on netz
sudo vtysh -N isp -c 'configure terminal' -c 'ip prefix-list KUNDE seq 10 deny 0.0.0.0/0 le 32' -c 'route-map VON-KUNDE permit 10' -c 'match ip address prefix-list KUNDE' -c 'exit' -c 'route-map NACH-KUNDE deny 10' -c 'exit' -c 'router bgp 65100' -c 'bgp router-id 198.51.100.1' -c 'neighbor 198.51.100.2 remote-as 65010' -c 'neighbor 198.51.100.6 remote-as 65010' -c 'address-family ipv4 unicast' -c 'neighbor 198.51.100.2 default-originate' -c 'neighbor 198.51.100.6 default-originate' -c 'neighbor 198.51.100.2 route-map VON-KUNDE in' -c 'neighbor 198.51.100.6 route-map VON-KUNDE in' -c 'neighbor 198.51.100.2 route-map NACH-KUNDE out' -c 'neighbor 198.51.100.6 route-map NACH-KUNDE out' -c 'end' -c 'write memory' >/dev/null

default-originate sends the default route past the outbound route-map, which otherwise blocks everything. Both vRouters accept only the default route and announce nothing:

# on netz
sudo vtysh -N vr1 -c 'configure terminal' -c 'ip prefix-list NUR-DEFAULT seq 10 permit 0.0.0.0/0' -c 'route-map VON-ISP permit 10' -c 'match ip address prefix-list NUR-DEFAULT' -c 'exit' -c 'route-map NACH-ISP deny 10' -c 'exit' -c 'router bgp 65010' -c 'bgp router-id 198.51.100.2' -c 'neighbor 198.51.100.1 remote-as 65100' -c 'address-family ipv4 unicast' -c 'neighbor 198.51.100.1 route-map VON-ISP in' -c 'neighbor 198.51.100.1 route-map NACH-ISP out' -c 'end' -c 'write memory' >/dev/null
sudo vtysh -N vr2 -c 'configure terminal' -c 'ip prefix-list NUR-DEFAULT seq 10 permit 0.0.0.0/0' -c 'route-map VON-ISP permit 10' -c 'match ip address prefix-list NUR-DEFAULT' -c 'exit' -c 'route-map NACH-ISP deny 10' -c 'exit' -c 'router bgp 65010' -c 'bgp router-id 198.51.100.6' -c 'neighbor 198.51.100.5 remote-as 65100' -c 'address-family ipv4 unicast' -c 'neighbor 198.51.100.5 route-map VON-ISP in' -c 'neighbor 198.51.100.5 route-map NACH-ISP out' -c 'end' -c 'write memory' >/dev/null
sleep 10
sudo vtysh -N vr1 -c 'show bgp ipv4 unicast summary' | sed -n '/^Neighbor/,/^$/p'
sudo ip -n vr1 route show default
sudo ip -n vr2 route show default
sudo ip netns exec vr1 curl -s http://203.0.113.80:8080/

Expected:

Neighbor        V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down State/PfxRcd   PfxSnt Desc
198.51.100.1    4      65100         8         6        1    0    0 00:00:09            1        0 N/A

default nhid 10 via 198.51.100.1 dev eth1 proto bgp metric 20
default nhid 10 via 198.51.100.5 dev eth1 proto bgp metric 20
Hallo aus dem Internet

One network received (the default route), none sent. Each vRouter has its own default route via its own connection.

Save your progress? Sign up for free

Drill 3: The Gateway Address

keepalived as on Day 14, with one improvement: instead of only monitoring the interface to the provider, a script checks every second whether the default route via BGP is there. That also catches the case where the link is up but the provider has ended the session.

# on netz
sudo tee /etc/keepalived/vr1.conf >/dev/null <<'EOF'
global_defs {
    script_user root
    enable_script_security
}
vrrp_script uplink {
    script "/bin/sh -c \"ip route show default proto bgp | grep -q via\""
    interval 1
    fall 2
    rise 2
}
vrrp_instance LAN {
    state BACKUP
    interface eth0
    virtual_router_id 10
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass lab-vrrp
    }
    track_script {
        uplink
    }
    virtual_ipaddress {
        192.168.10.1/24
    }
}
EOF
sudo sed 's/priority 150/priority 100/' /etc/keepalived/vr1.conf | sudo tee /etc/keepalived/vr2.conf >/dev/null
sudo keepalived -P -s vr1 -f /etc/keepalived/vr1.conf
sudo keepalived -P -s vr2 -f /etc/keepalived/vr2.conf
sleep 6
sudo ip -n vr1 -br addr show eth0
sudo ip -n vr2 -br addr show eth0
sudo journalctl -t Keepalived_vrrp_vr1 --no-pager | tail -3 | cut -c1-130

Expected:

eth0@if3         UP             192.168.10.2/24 192.168.10.1/24 fe80::985f:2dff:fef1:a327/64
eth0@if4         UP             192.168.10.3/24 fe80::ccd8:a6ff:fecb:6cc8/64
Nov 24 01:16:45 netz Keepalived_vrrp_vr1[25941]: VRRP_Script(uplink) succeeded
Nov 24 01:16:45 netz Keepalived_vrrp_vr1[25941]: (LAN) Entering BACKUP STATE
Nov 24 01:16:49 netz Keepalived_vrrp_vr1[25941]: (LAN) Entering MASTER STATE
Line Meaning
script_user root, enable_script_security scripts run as root; in return keepalived checks that nobody else can modify them
interval 1, fall 2, rise 2 check every second, FAULT after two failures, ready again after two successes
track_script the instance depends on the result of the script

The script runs in the namespace of the respective vRouter, because keepalived itself runs there (-s).

Save your progress? Sign up for free

Drill 4: DHCP and DNS

Two DHCP servers in the same LAN? Yes, if they split the addresses: vr1 hands out .100 to .149, vr2 .150 to .199 (split scope). Both name the VIP as gateway and DNS server. If one fails, the other keeps handing out addresses from its half.

# on netz
sudo tee /etc/dnsmasq-vr1.conf >/dev/null <<'EOF'
# vRouter vr1: DHCP für die erste Hälfte, DNS auf allen Adressen von eth0 (auch der VIP)
interface=eth0
bind-dynamic
except-interface=lo
no-hosts
no-resolv
domain=firma.lan
local=/firma.lan/
host-record=web.labor,203.0.113.80
host-record=vr1.firma.lan,192.168.10.2
host-record=vr2.firma.lan,192.168.10.3
dhcp-range=192.168.10.100,192.168.10.149,12h
dhcp-option=option:router,192.168.10.1
dhcp-option=option:dns-server,192.168.10.1
dhcp-leasefile=/var/lib/misc/dnsmasq-vr1.leases
pid-file=/run/dnsmasq-vr1.pid
log-dhcp
log-facility=/var/log/dnsmasq-vr1.log
EOF
sed -e 's/^# vRouter vr1: DHCP für die erste/# vRouter vr2: DHCP für die zweite/' -e 's/^dhcp-range=192.168.10.100,192.168.10.149/dhcp-range=192.168.10.150,192.168.10.199/' -e 's/dnsmasq-vr1\./dnsmasq-vr2./' /etc/dnsmasq-vr1.conf | sudo tee /etc/dnsmasq-vr2.conf >/dev/null
grep -E '^(dhcp-range|host-record=vr|dhcp-leasefile|pid-file|log-facility)' /etc/dnsmasq-vr2.conf
sudo dnsmasq --test --conf-file=/etc/dnsmasq-vr1.conf
sudo ip netns exec vr1 dnsmasq --conf-file=/etc/dnsmasq-vr1.conf
sudo ip netns exec vr2 dnsmasq --conf-file=/etc/dnsmasq-vr2.conf
sleep 1
sudo ip netns exec vr1 ss -ulnp | grep dnsmasq | grep -v '\['

Expected:

host-record=vr1.firma.lan,192.168.10.2
host-record=vr2.firma.lan,192.168.10.3
dhcp-range=192.168.10.150,192.168.10.199,12h
dhcp-leasefile=/var/lib/misc/dnsmasq-vr2.leases
pid-file=/run/dnsmasq-vr2.pid
log-facility=/var/log/dnsmasq-vr2.log
dnsmasq: syntax check OK.
UNCONN 0      0                          192.168.10.2:53        0.0.0.0:*    users:(("dnsmasq",pid=26277,fd=8))
UNCONN 0      0                          192.168.10.1:53        0.0.0.0:*    users:(("dnsmasq",pid=26277,fd=6))
UNCONN 0      0                          0.0.0.0%eth0:67        0.0.0.0:*    users:(("dnsmasq",pid=26277,fd=4))

The decisive difference from Day 6 is bind-dynamic instead of bind-interfaces: dnsmasq listens on every address of eth0, including one that is added later. vr1 is already listening on the VIP .1. When vr2 becomes MASTER, it gets the VIP, and its dnsmasq binds it right away. The challenge shows what happens otherwise.

Now c1 gets an address. As on Day 6 with its own resolv.conf and without the DNS hook, plus --noipv4ll: without this option, dhcpcd fell back to a link-local address 169.254.x.x on the first attempt, even though vr2 had already confirmed the address.

# on netz
sudo mkdir -p /etc/netns/c1
printf 'nameserver 192.168.10.1\nsearch firma.lan\n' | sudo tee /etc/netns/c1/resolv.conf >/dev/null
sudo ip netns exec c1 dhcpcd -4 -1 -t 30 --noipv4ll -C resolv.conf eth0 2>&1 | grep -E 'offered|ignoring|leased'
sudo ip -n c1 route

Expected (which vRouter answers first is random):

eth0: offered 192.168.10.168 from 192.168.10.3
eth0: ignoring offer of 192.168.10.118 from 192.168.10.2
eth0: leased 192.168.10.168 for 43200 seconds
default via 192.168.10.1 dev eth0 proto dhcp src 192.168.10.168 metric 1002
192.168.10.0/24 dev eth0 proto dhcp scope link src 192.168.10.168 metric 1002

Both made an offer; c1 takes the first offer and declines the second. The gateway is the VIP.

Save your progress? Sign up for free

Drill 5: NAT and the Way Out

Masquerade on both vRouters, toward the provider:

# on netz
for v in vr1 vr2; do
  sudo ip netns exec $v nft add table ip nat
  sudo ip netns exec $v nft add chain ip nat post '{ type nat hook postrouting priority srcnat; }'
  sudo ip netns exec $v nft add rule ip nat post oifname eth1 masquerade
done
sudo ip netns exec vr1 nft list ruleset

Expected:

table ip nat {
    chain post {
        type nat hook postrouting priority srcnat; policy accept;
        oifname "eth1" masquerade
    }
}

The whole path, from the name to the reply, and what the provider sees:

# on netz
sudo ip netns exec c1 dig +short web.labor
sudo ip netns exec c1 dig +short vr2.firma.lan
sudo ip netns exec isp tcpdump -i any -n -c 1 'tcp port 8080 and tcp[tcpflags] & tcp-syn != 0' 2>/dev/null &
sleep 1; sudo ip netns exec c1 curl -s http://web.labor:8080/; wait
sudo ip netns exec vr1 conntrack -L -p tcp --dport 8080 2>/dev/null | head -1

Expected:

203.0.113.80
192.168.10.3
Hallo aus dem Internet
01:18:30.107056 eth1  In  IP 198.51.100.2.49678 > 203.0.113.80.8080: Flags [S], seq 62042099, win 64240, options [mss 1460,sackOK,TS val 73879902 ecr 0,nop,wscale 7], length 0
tcp      6 118 TIME_WAIT src=192.168.10.168 dst=203.0.113.80 sport=49678 dport=8080 src=203.0.113.80 dst=198.51.100.2 sport=8080 dport=49678 [ASSURED] mark=0 use=1

dnsmasq on the VIP resolved the name, vr1 routed and translated: the provider sees 198.51.100.2, not 192.168.10.168. The conntrack entry in vr1 remembers the translation so that the reply finds its way back. Only vr1 has this memory, which will matter in a moment.

Save your progress? Sign up for free

Drill 6: vr1’s Provider Connection Fails

A ping every 10 milliseconds to the internet, and at the same time a download with limited speed so that it is still running during the failure:

# on netz
sudo ip netns exec c1 ping -i 0.01 -c 1000 -q 203.0.113.80 > /tmp/p.txt &
sudo ip netns exec c1 curl -s -o /dev/null --limit-rate 500k -m 20 -w 'curl: %{http_code} %{size_download} Byte, Fehler %{exitcode}\n' http://web.labor:8080/gross.bin > /tmp/c.txt 2>&1 &
sleep 3; sudo ip -n isp link set eth1 down
wait; tail -2 /tmp/p.txt; cat /tmp/c.txt
sudo journalctl -t Keepalived_vrrp_vr1 --no-pager | tail -3 | cut -c1-130
sudo ip -n vr2 -br addr show eth0

Expected:

1000 packets transmitted, 838 received, +2 errors, 16.2% packet loss, time 11021ms
rtt min/avg/max/mdev = 0.066/0.139/1.151/0.052 ms
curl: 200 4898008 Byte, Fehler 56
Nov 24 01:18:45 netz Keepalived_vrrp_vr1[25941]: Script `uplink` now returning 1
Nov 24 01:18:46 netz Keepalived_vrrp_vr1[25941]: VRRP_Script(uplink) failed (exited with status 1)
Nov 24 01:18:46 netz Keepalived_vrrp_vr1[25941]: (LAN) Entering FAULT STATE
eth0@if4         UP             192.168.10.3/24 192.168.10.1/24 fe80::ccd8:a6ff:fecb:6cc8/64

The chain: link gone → BGP session gone → default route gone → script fails twice → vr1 in FAULT → vr2 takes over the VIP. 162 packets, 1.6 seconds.

And the download: Fehler 56 (error 56, Failure when receiving data), aborted shortly before the end. The connection ran through the NAT of vr1 with the address 198.51.100.2. vr2 does not know it. Its packets go out with 198.51.100.6, a stranger to the web server, and the replies to 198.51.100.2 no longer reach anyone. New connections work immediately, existing ones break. The proof that new connections now come from vr2:

# on netz
sudo ip netns exec isp tcpdump -i any -n -c 1 'tcp port 8080 and tcp[tcpflags] & tcp-syn != 0' 2>/dev/null &
sleep 1; sudo ip netns exec c1 curl -s http://web.labor:8080/; wait

Expected:

Hallo aus dem Internet
01:18:54.030802 eth2  In  IP 198.51.100.6.53860 > 203.0.113.80.8080: Flags [S], seq 858536678, win 64240, options [mss 1460,sackOK,TS val 73903826 ecr 0,nop,wscale 7], length 0

The name web.labor was resolved too, this time by dnsmasq on vr2, at the same address .1.

With NAT, breaking existing connections is unavoidable as long as the translation is tied to the address of one device. The remedy in larger installations: conntrackd synchronizes the table between both vRouters, and both translate to a shared, also virtual, public address.

Save your progress? Sign up for free

Drill 7: Return

# on netz
sudo ip netns exec c1 ping -i 0.01 -c 1500 -q 203.0.113.80 > /tmp/p.txt &
sleep 2; sudo ip -n isp link set eth1 up
wait; tail -2 /tmp/p.txt
sudo journalctl -t Keepalived_vrrp_vr1 --no-pager | tail -1 | cut -c1-130
sudo ip -n vr1 -br addr show eth0

Expected:

1500 packets transmitted, 1500 received, 0% packet loss, time 16534ms
rtt min/avg/max/mdev = 0.052/0.141/0.988/0.047 ms
Nov 24 01:19:13 netz Keepalived_vrrp_vr1[25941]: (LAN) Entering MASTER STATE
eth0@if3         UP             192.168.10.2/24 192.168.10.1/24 fe80::985f:2dff:fef1:a327/64

No loss: first BGP comes up, then the script reports success twice, then vr1 takes the VIP back with its higher priority, and vr2 hands it over cleanly. A ping has no connection state and survives the switch; a running download through vr2 would break again. If you want to avoid that, set nopreempt (Day 14) and switch back by hand at a quiet time.

Save your progress? Sign up for free

Exercise: Speed drills

  1. Which vRouter is MASTER? Target time: 20 s
  2. Which leases have the two DHCP servers handed out? Target time: 20 s
  3. With which public address does a new request from c1 currently go out? Prove it with conntrack. Target time: 45 s

Solution

# on netz
for v in vr1 vr2; do sudo ip -n $v -br addr show eth0 | grep -q 192.168.10.1/ && echo "MASTER: $v"; done
cat /var/lib/misc/dnsmasq-vr1.leases /var/lib/misc/dnsmasq-vr2.leases
sudo ip netns exec c1 curl -s -o /dev/null http://web.labor:8080/
sudo ip netns exec vr1 conntrack -L -p tcp --dport 8080 2>/dev/null | grep -o 'dst=198.51.100.[0-9]*' | head -1

Expected (timestamps and identifiers will differ on your side):

MASTER: vr1
1795526288 02:dc:a3:b0:7c:1f 192.168.10.118 * ff:a3:b0:7c:1f:00:01:00:01:32:97:6d:42:26:90:a6:e1:73:dc
1795526293 02:dc:a3:b0:7c:1f 192.168.10.168 * ff:a3:b0:7c:1f:00:01:00:01:32:97:6d:42:26:90:a6:e1:73:dc
dst=198.51.100.2

If you ran dhcpcd several times, c1 appears at both servers, depending on who was faster last. The first number is the expiry time as Unix time. In the reply direction of the conntrack entry, the destination is the address it was translated to.

Save your progress? Sign up for free

Challenge

A colleague has “aligned” the dnsmasq configuration of vr2 “with the one from Day 6”:

# on netz
sudo kill $(cat /run/dnsmasq-vr2.pid)
sudo sed -i 's/^bind-dynamic$/bind-interfaces/' /etc/dnsmasq-vr2.conf
sleep 1
sudo ip netns exec vr2 dnsmasq --conf-file=/etc/dnsmasq-vr2.conf
sudo ip netns exec c1 dig +short web.labor

Expected: 203.0.113.80

Everything works. Check whether it stays that way when things go wrong: let vr1’s connection fail, describe exactly what users experience, and fix the cause.

Hint: When dnsmasq binds its addresses is what bind-interfaces and bind-dynamic decide.

Solution

# on netz
sudo ip -n isp link set eth1 down
sleep 4
sudo ip -n vr2 -br addr show eth0
sudo ip netns exec c1 dig +tries=1 +time=2 web.labor 2>&1 | grep -E 'status|timed out|refused' | head -2
sudo ip netns exec c1 curl -sS -m 3 http://web.labor:8080/ 2>&1
sudo ip netns exec c1 curl -s -m 3 http://203.0.113.80:8080/
sudo ip netns exec vr2 ss -ulnp | grep ':53 ' | grep -v '\['

Expected:

eth0@if4         UP             192.168.10.3/24 192.168.10.1/24 fe80::ccd8:a6ff:fecb:6cc8/64
;; communications error to 192.168.10.1#53: connection refused
curl: (28) Resolving timed out after 3000 milliseconds
Hallo aus dem Internet
UNCONN 0      0                          192.168.10.3:53        0.0.0.0:*    users:(("dnsmasq",pid=27701,fd=6))

For users it looks like “the internet is broken”: every web page reports a DNS error. Anyone who addresses an IP address directly gets through; routing and NAT via vr2 work. The cause: with bind-interfaces, dnsmasq binds at startup all addresses that eth0 has at that moment. The VIP only arrived with the failover, dnsmasq on vr2 listens only on .3, and the kernel answers requests to .1 with “connection refused.” As long as vr1 was MASTER, nobody noticed.

# on netz
sudo kill $(cat /run/dnsmasq-vr2.pid)
sudo sed -i 's/^bind-interfaces$/bind-dynamic/' /etc/dnsmasq-vr2.conf
sleep 1
sudo ip netns exec vr2 dnsmasq --conf-file=/etc/dnsmasq-vr2.conf
sleep 1
sudo ip netns exec vr2 ss -ulnp | grep ':53 ' | grep -v '\['
sudo ip netns exec c1 dig +short web.labor
sudo ip -n isp link set eth1 up
sleep 15

Expected:

UNCONN 0      0                          192.168.10.3:53        0.0.0.0:*    users:(("dnsmasq",pid=27919,fd=8))
UNCONN 0      0                          192.168.10.1:53        0.0.0.0:*    users:(("dnsmasq",pid=27919,fd=6))
203.0.113.80

The lesson applies far beyond dnsmasq: Every service that is supposed to listen on a VIP must be able to bind addresses that appear after it starts. For other services that means 0.0.0.0 instead of a fixed address, or net.ipv4.ip_nonlocal_bind=1, which allows binding to an address that is not currently present. And every failover test must check the services on the BACKUP as well, not just the failover itself.

Save your progress? Sign up for free

Checkpoint

# on netz
for v in vr1 vr2; do echo "$v $(sudo vtysh -N $v -c 'show bgp ipv4 unicast summary' | grep -c '^198.51.100.[15] .* 1 ')"; done
grep -c '^bind-dynamic' /etc/dnsmasq-vr1.conf /etc/dnsmasq-vr2.conf

Expected:

vr1 1
vr2 1
/etc/dnsmasq-vr1.conf:1
/etc/dnsmasq-vr2.conf:1
# on netz
sudo ip netns exec c1 curl -s http://web.labor:8080/
pgrep -c keepalived
pgrep -c dnsmasq

Expected:

Hallo aus dem Internet
4
2

Clean-up

# on netz
sudo kill $(sudo cat /run/keepalived/vr1/keepalived.pid) $(sudo cat /run/keepalived/vr2/keepalived.pid)
sudo kill $(cat /run/dnsmasq-vr1.pid) $(cat /run/dnsmasq-vr2.pid)
for p in $(pgrep -f '^python3 -m http.server 8080'); do sudo kill $p; done
sudo systemctl stop frr@vr1 frr@vr2 frr@isp
sudo rm -rf /etc/frr/vr1 /etc/frr/vr2 /etc/frr/isp
sleep 2
for n in sw vr1 vr2 c1 isp; do sudo ip netns del $n; done
sudo rm -f /etc/keepalived/vr1.conf /etc/keepalived/vr2.conf /etc/dnsmasq-vr1.conf /etc/dnsmasq-vr2.conf
sudo rm -f /var/lib/misc/dnsmasq-vr1.leases /var/lib/misc/dnsmasq-vr2.leases /var/log/dnsmasq-vr1.log /var/log/dnsmasq-vr2.log /var/lib/dhcpcd/eth0.lease
sudo rm -r /etc/netns
rm -rf ~/vrouter /tmp/p.txt /tmp/c.txt
ip netns list | wc -l
pgrep -a 'keepalived|dnsmasq|zebra|bgpd' || echo "keine Labor-Prozesse"

Expected:

0
keine Labor-Prozesse

Practice note

  • A vRouter is a chain, and every link needs its own monitoring. Here the VIP depends on the BGP default route, not just on the link. In production, checks for DNS and the firewall are added.
  • Test the services on the BACKUP. The challenge is not a contrived case: a service that listens only on the addresses present at startup gets noticed exactly once, at the first real failure.
  • NAT and failover do not get along well. Existing connections break as long as the translation is tied to a device-bound address. conntrackd and a shared public address solve this but cost complexity. For offices, the insight that reloading the page helps is often enough.
  • Split-scope DHCP is the simplest redundancy; dnsmasq has no lease synchronization. In larger networks, Kea with its high-availability mode takes over this job.

Contents

More