Skip to content

VPN HA failover with keepalived

How to build active/standby VPN gateways with keepalived — VRRP fundamentals, state synchronization for OpenVPN and IPsec, and the failover behaviour we test before production.

A single VPN gateway is fine until it is not. When the audit calls out “single point of failure” and the contract specifies an uptime SLA, you need a second box that takes over when the first goes down. keepalived is the tool we reach for: it implements VRRP, monitors the active node, and floats a virtual IP to the standby when the active stops responding. This article covers the active/standby pattern for OpenVPN and strongSwan, the state-sync question (and its limits), and what we test before declaring HA done.

How to verify

Before configuring HA, confirm the two boxes have identical OS and VPN versions, can reach each other on a dedicated heartbeat link (or at least multicast 224.0.0.18), and the floating IP is reachable from a test client.

ip -br addr show
ping -c 3 <peer-ip>
dpkg -l | grep -E 'keepalived|openvpn|strongswan'
sysctl net.ipv4.ip_nonlocal_bind
ss -ltnp | grep -E ':(1194|500|4500)\b'

If net.ipv4.ip_nonlocal_bind is 0, the standby cannot bind to the floating IP before it has it. This is a near-universal pre-requisite for VRRP setups and people miss it every time.

What’s happening

VRRP (Virtual Router Redundancy Protocol) lets a group of routers share a virtual IP. One router is the “master” — it ARP-replies for the VIP and forwards traffic to it. The others are “backup.” They send heartbeats on a multicast address every second. When the master stops sending heartbeats (default 3 seconds), the highest-priority backup promotes itself and ARP-announces ownership of the VIP. Clients with cached ARP entries flush them within seconds and resume traffic to the new master.

keepalived wraps VRRP with health checks (“is the OpenVPN process listening?”) and tracking groups (“if my downstream interface is down, lower my priority”). The result is a state machine where the VIP follows the healthy node.

The hard part with VPNs specifically: session state. When the active box dies, every active VPN session terminates. The standby takes the VIP, clients re-establish — but the TLS / IKE handshake has to happen again. Some engines (OpenVPN with the patched dco module, strongSwan with ha plugin) can replicate session state between peers; in practice, the simpler pattern is to accept a 30-second client reconnect window and design for it.

The procedure

  1. Install keepalived on both nodes.

    apt install -y keepalived
    sysctl -w net.ipv4.ip_nonlocal_bind=1
    sysctl -w net.ipv6.ip_nonlocal_bind=1
    echo 'net.ipv4.ip_nonlocal_bind=1' >> /etc/sysctl.conf
  2. Write /etc/keepalived/keepalived.conf on the primary node.

    global_defs {
        router_id vpn-primary
        script_user nobody
        enable_script_security
    }
    
    vrrp_script chk_openvpn {
        script "/usr/bin/pgrep -f 'openvpn.*server.conf'"
        interval 2
        weight  -20
        fall 2
        rise 2
    }
    
    vrrp_instance VI_VPN {
        state MASTER
        interface eth0
        virtual_router_id 51
        priority 110
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass <16-char-shared-secret>
        }
        virtual_ipaddress {
            203.0.113.42/24 dev eth0
        }
        track_script {
            chk_openvpn
        }
        notify_master "/usr/local/bin/vpn-promote.sh"
        notify_backup "/usr/local/bin/vpn-demote.sh"
        notify_fault  "/usr/local/bin/vpn-fault.sh"
    }
  3. Mirror the config on the secondary with state BACKUP, priority 100, and the same virtual_router_id and auth_pass.

  4. Write the promote/demote scripts. What runs on transition:

    # /usr/local/bin/vpn-promote.sh
    #!/bin/bash
    logger -t vpn-ha "promoting to master"
    systemctl start openvpn-server@server
    # /usr/local/bin/vpn-demote.sh
    #!/bin/bash
    logger -t vpn-ha "demoting to backup"
    systemctl stop openvpn-server@server

    The pattern: only the master runs the OpenVPN process. When VRRP failover happens, the new master starts OpenVPN; the old master (if still alive) stops it.

  5. For strongSwan, the same shape applies but you also need to copy the secrets and PKI to the standby and use the ha plugin if you want session-state sync:

    # /etc/strongswan.d/charon/ha.conf
    ha {
        load = yes
        local = 10.0.0.1
        remote = 10.0.0.2
        secret = <shared-secret>
        segment_count = 1
        resync = yes
    }

    The HA plugin is finicky; in regulated environments we usually skip it and accept the reconnect.

  6. Start keepalived and watch the state.

    systemctl enable --now keepalived
    journalctl -u keepalived -f
    ip addr show eth0 | grep 203.0.113.42

    The VIP should appear on the primary. Stop OpenVPN on the primary and watch the VIP migrate.

  7. Test failover under client load. Connect a real client, start a long-running session (e.g. ssh through the VPN, then iperf3 -t 60`), then kill the primary keepalived. Measure the reconnect time. Document it.

Operational notes

  • VRRP advert interval defaults to 1 second; failover takes ~3 seconds. In environments where this is unacceptable, drop to 200ms intervals — but watch CPU on busy gateways.
  • The auth_pass is plaintext on the wire. VRRP authentication is weak by design; pair with strict L2 segmentation so unauthorized hosts cannot inject VRRP advertisements.
  • The “split-brain” scenario — both nodes think they are master — happens when the heartbeat link fails. Always run keepalived heartbeat on a dedicated link, not on the same physical interface as the VIP.
  • For client reconnect to feel automatic, set keepalive 10 60 in OpenVPN’s server.conf and ensure the client config has a low connect-retry. With VRRP failover in 3 seconds and a 10-second keepalive, total client outage is under 15 seconds.
  • For floating-IP environments (cloud providers, hosted VPS), VRRP may not work because the L2 broadcast is filtered. AWS uses an EIP-reassociation script; Azure uses an IP forwarding flag; on Hetzner Cloud you use their floating-IP API. The keepalived notify_master hook is where you call the provider’s API.

In the engagements we run, HA VPN is a clustered environment responsibility — two gateways, automatic failover, tested quarterly under load. The keepalived config is the easy part; the discipline of testing failover before you need it is what separates “we have HA” from “we have HA that worked the one time we needed it.”