A redundant Linux gateway with keepalived and VRRP

Share a virtual gateway address between two routers and track upstream health.

Clients use one virtual gateway address. The preferred router holds it while healthy. The standby takes over when VRRP detects that the preferred router is unavailable or its tracked service changes the election.

Preferred node

# /etc/keepalived/keepalived.conf
vrrp_script chk_route {
    script "/usr/bin/ip route get 192.0.2.1"
    interval 2
    fall 3
    rise 2
}

vrrp_instance LAN {
    state BACKUP            # both BACKUP with nopreempt avoids unnecessary takeovers
    nopreempt
    interface eth1
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 8chars!!
    }
    virtual_ipaddress {
        10.20.0.1/24 dev eth1
    }
    track_script {
        chk_route
    }
}

Use the same settings on the standby with priority 100.

Checks before deployment

  • Choose a virtual_router_id that is unique on the Layer 2 segment.
  • Allow VRRP protocol 112 and its multicast traffic between the two nodes if using the multicast configuration.
  • Existing NAT connections usually need to reconnect after failover. If that matters, plan for state synchronization.
Test the service path

Check whether the router can reach its upstream, not just whether the VRRP process is alive.

Verification

ip -br addr show eth1           # the active router should hold the VIP
journalctl -u keepalived -f     # watch MASTER/BACKUP transitions
systemctl stop keepalived       # on the active router, check that the VIP moves