High Availability — Active-Passive Pair

AiFw supports a two-node active-passive cluster: one master forwarding production traffic, one backup with replicated pf state, ready to take over within seconds of any failure on the master.

Validation status: the failover behaviors and timing numbers on this page are design targets. The runtime fixes in #712 (demotion, preemption, boot ordering, replication) are covered by unit and API tests; the automated two-node failover suite is tracked in #534. Validate failover in your own environment before relying on it.

What survives a master reboot

Component Survives Notes
TCP sessions through the firewall yes pfsync replicates state to the backup; state-policy floating lets replicated states match traffic on the new master’s interface
WireGuard tunnels yes* Tunnels replicate to the standby but run only on the CARP MASTER; the new master brings them up within ~2 s of taking over. Existing peers reconnect within ~5 s provided remote peers have PersistentKeepalive ≤ 5 (the default is off — peers without keepalive only reconnect on next outbound traffic)
IPsec (IKEv2) tunnels re-established Tunnels replicate; the BACKUP renders them passive (start_action = none) so it never initiates with the master’s identities, and re-renders them active when it becomes MASTER. SAs are not synced — IKE re-negotiates after failover
DHCP leases (rDHCP) yes rDHCP HA handles its own state replication; AiFw’s dhcp_link flag keeps the peer list in sync
ACME certificates yes renewal happens master-only; on success the cert+key are pushed to peers via POST /api/v1/cluster/cert-push
In-flight DNS lookups no small visible glitch during the failover window — most resolvers retry transparently
IDS in-memory ring buffer no rule overrides and suppressions are replicated; runtime alert ring buffer is not

Prerequisites

Security considerations

The replication channel between cluster nodes is treated as TRUSTED. Specifically:

If your network does not satisfy “trusted pfsync segment,” do not enable HA replication.

Setup

  1. Install AiFw on both nodes via the ISO build.
  2. On node A, in the first-boot wizard:
    • Answer Yes to “Configure HA pair?”
    • Choose Primary.
    • Select the pfsync interface, peer IP, password, and per-LAN/WAN VIPs.
  3. On node B: run the same wizard, choose Secondary, enter the same VHIDs and password.
  4. After both nodes are up: open Cluster / HA → HA Dashboard on node A. Expect MASTER on every VHID, the peer answering and reporting BACKUP, pfsync bulk sync complete, no demotion held and no warnings. Node health comes from an authenticated probe of the peer’s API every 5 s.
  5. Verify with aifw cluster verify on each node, then run scripts/ha-verify.sh node-a node-b over SSH for a pair-wide check.

Clusters built from the UI or API (instead of the wizard) get the same boot handling: the first CARP VIP or pfsync config written sets aifw_cluster_enabled, aifw_cluster_role and the boot/shutdown demotion switches in rc.conf.

Peer API port

Every cluster call to a peer (snapshot replication, health probes, cert push) goes to https://<peer address>:<api_port>. The port defaults to 8080 and is stored per node (api_port on POST/PUT /api/v1/cluster/nodes, the API port field on the Cluster → Nodes form, aifw cluster nodes add --api-port) — set it when a peer’s API listens elsewhere.

WireGuard and IPsec follow the CARP role

Both nodes hold the same WireGuard private keys and IPsec identities, so in a cluster only the CARP MASTER runs WireGuard and initiates IPsec. The API re-checks the kernel every 2 seconds (and immediately on a role-change event): on MASTER it brings the enabled WireGuard tunnels up and renders IPsec tunnels with their configured start_action; on BACKUP it tears WireGuard down and renders IPsec passive (start_action = none). The older wg_deconfigure_on_backup setting is kept for compatibility but no longer changes anything.

How CARP is driven

All CARP and pfsync kernel changes go through one root helper, /usr/local/libexec/aifw-sudo-carp; the API and daemon run unprivileged and call it via sudo.

Boot sequence

  1. rc.d/aifw_carp_demote runs before any interface is configured: enables preemption and holds a boot demotion (plus maintenance if persistent maintenance mode is on).
  2. pfsync0 comes up from rc.conf, then pf.
  3. aifw-daemon loads the aifw-ha pf anchor (CARP and pfsync pass rules), configures pfsync and the VHIDs, and releases the boot hold once pfsync’s bulk update has finished (syncok: 1 in ifconfig pfsync0) and, if the peer is MASTER, this node has the peer’s replicated config — minimum 10 s, maximum 120 s.
  4. If aifw-daemon never releases it (disabled, crashing), a failsafe started by step 1 releases the hold after 5 minutes.

A rebooted primary therefore comes back as BACKUP, receives the state table and any config edits made on the peer while it was down, and only then preempts. Running service aifw_carp_demote start again later is a no-op.

Latency profiles

The pfsync.latency_profile setting sets the secondary’s advskew. A BACKUP declares the master dead after 3 × advbase + advskew/256 seconds without an advertisement, and CARP’s advbase cannot go below 1 s, so hard-failure takeover is about 3 seconds with every profile:

Profile advbase secondary advskew Hard-failure takeover
Conservative (default) 1 100 ~3.4 s
Tight 1 20 ~3.1 s
Aggressive 1 10 ~3.0 s

The primary uses advskew 0 regardless of profile. Timing follows the node’s configured role, so a primary that is temporarily BACKUP keeps advskew 0 and takes the VIPs back once healthy.

Planned moves — demote, maintenance mode, shutdown, a failing health check — don’t wait for the timeout: the demoted node advertises a worse skew and the peer preempts within about one advertisement (~1 s).

aifw cluster pfsync set --latency-profile tight

Or via the API (PUT /api/v1/cluster/pfsync).

Minimizing the unplanned-failure gap

On shutdown or reboot, aifw_demote_on_shutdown holds a shutdown demotion and waits 2 s before the data plane goes down, so the peer takes over first; a reboot of the master typically misses zero to two packets. Restarting the AiFw services (including aifw update install --restart) does not demote: pf, pfsync and CARP live in the kernel and keep forwarding throughout.

For unplanned failures (power loss, kernel panic, NIC death) the gap is CARP’s ~3 s detection:

Without a UPS, a hard power loss results in:

Split-brain handling

If the links between the nodes fail but both stay up, both may become MASTER (a “split brain”).

Operations

Planned maintenance / rolling upgrade

Always upgrade the standby first.

# On the standby
aifw update install --restart

Restarting the AiFw services doesn’t touch CARP or pf, so traffic keeps flowing on the node being upgraded. After the standby is healthy on the new version, move the VIPs and upgrade the other node:

aifw cluster maintenance on    # on the master: VIPs move to the peer, stays BACKUP across reboots
aifw update install --restart  # upgrade (reboot if the update needs it)
aifw cluster maintenance off   # take the VIPs back (the primary preempts)

Confirm version drift is gone via aifw cluster nodes list (or the dashboard’s per-node panel — the software_version field shows the running version of each node).

Manual promote / demote

aifw cluster demote            # hand the VIPs to the peer until promote or reboot
aifw cluster promote           # clear manual demotion and maintenance mode
aifw cluster maintenance on    # stay BACKUP, across reboots, until turned off
aifw cluster maintenance off

promote clears this node’s operator holds; it does not force MASTER over a peer with a better advskew. To move the VIPs to the secondary, demote (or put into maintenance) the primary. The same actions are on the HA Dashboard.

Decommission a node

aifw cluster nodes remove <node-id>

Obtain the <node-id> from aifw cluster nodes list. To run the remaining node standalone, also remove its CARP VIPs and pfsync config; that clears aifw_cluster_enabled and the boot/shutdown demotion switches.

Force a config sync

aifw cluster sync     # this node pulls the current snapshot from the primary

The dashboard’s Force sync from peer button does the same thing. It pulls from the peer that currently reports MASTER (or the only peer). Use this when the standby’s cluster_snapshot_state.last_applied_hash doesn’t match the master’s live config hash and the next replicator tick is too far away.

Replication always flows from the current MASTER to the other node — including from a promoted secondary back to the primary while the primary is BACKUP — so changes made during a failover survive the primary’s return. Each node therefore needs a peer key for the other (Generate Peer Key on both, register each on the opposite node); the dashboard warns when one is missing.

Replication carries cluster-wide config only. Each node keeps its own node table (peer keys, TLS pins, API ports), pfsync settings, DHCP-HA peer settings, hostname and /etc/resolv.conf; services (rDHCP, DNS resolver, IPsec, …) are only restarted on the standby when their section actually changed. A backup taken on one node and restored on the other keeps the target’s cluster identity.

Show cluster status

aifw cluster status
aifw cluster status --json

Verifying

# On either node — exits 0 healthy, exits 1 with reason on failure
aifw cluster verify

# Machine-readable output (used by the harness)
aifw cluster verify --json | python3 -m json.tool

Run as root on a node, aifw cluster authenticates with the on-box CLI key (created on first use). From elsewhere, aifw login https://<node>:8080 first, or set AIFW_TOKEN to an API key with ha:manage. --all-contexts cluster verify checks every node you have a context for.

The verify command checks:

  1. pf.conf.aifw sets state-policy floating.
  2. CARP VHIDs exist in the kernel, none in INIT, and they agree (no split).
  3. net.inet.carp.preempt is 1 and no demotion is held.
  4. pfsync0 is up, has a syncdev, and its bulk update completed.
  5. The peer is answering its API and there is no split brain.
  6. On the secondary: a config snapshot from the primary has been applied.

aifw cluster health run runs every enabled health check once and prints the results (the dashboard’s Run now button does the same).

Pair-wide check via SSH

sh scripts/ha-verify.sh node-a.example.com node-b.example.com

The harness asserts:

Exit codes:

Code Meaning
0 Pair healthy
1 A node was unreachable or aifw cluster verify returned a non-zero exit
2 At least one node failed its local checks (ok=false)
3 Expected exactly 1 MASTER, got a different count (0 or 2)

Out of scope (this release)

See also

Last updated: