背景 链接到标题

自建 NetBird 网络最初只有一个路由 peer(称节点 A),负责将办公子网 192.168.*.* 暴露给其他 NetBird 节点。单一故障点意味着节点 A 下线后,家庭等远程节点无法访问办公内网。

增加第二个路由 peer,实现主备冗余。

新增节点到 NetBird 链接到标题

办公室有另一台 Mac Mini(称为节点 B),加入 NetBird 网络:

netbird up --management-url https://netbird-api.域名 --setup-key <setup-key>

加入后获取 NetBird IP(100.x.x.x/16),所有 peer 自动连通。

开启 IP 转发 链接到标题

NetBird 路由 peer 需要转发子网流量。macOS 默认关闭 IP 转发:

sudo sysctl -w net.inet.ip.forwarding=1
echo "net.inet.ip.forwarding=1" | sudo tee -a /etc/sysctl.conf

即时生效并持久化。

创建备份路由 链接到标题

通过 NetBird Route API 添加第二条路由,关键参数是 metric

路由 路由 peer Metric 角色
192.168.*.*/24 节点 A 100 主路由
192.168.*.*/24 节点 B 200 备份路由

低 metric 优先,节点 A 正常时所有流量走主路由;节点 A 不可用时 NetBird 自动切换到节点 B,无需人工干预。

配置命令:

# 主路由(节点 A)
curl -X POST "https://netbird-api.域名/api/routes" \
  -H "Authorization: Token <PAT>" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Office LAN",
    "network_id": "office-lan",
    "enabled": true,
    "peer": "<peer_A_id>",
    "network": "192.168.0.0/24",
    "metric": 100,
    "masquerade": true,
    "groups": ["<all_group_id>"]
  }'

# 备份路由(节点 B)
curl -X POST "https://netbird-api.域名/api/routes" \
  -H "Authorization: Token <PAT>" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Office LAN (backup)",
    "network_id": "office-lan",
    "enabled": true,
    "peer": "<peer_B_id>",
    "network": "192.168.0.0/24",
    "metric": 200,
    "masquerade": true,
    "groups": ["<all_group_id>"]
  }'

验证 链接到标题

临时将备份路由的 metric 改为 10(低于主路由的 100),使流量走节点 B:

curl -X PUT "https://netbird-api.域名/api/routes/<route_id>" \
  -H "Authorization: Token <PAT>" \
  -H "Content-Type: application/json" \
  -d '{"metric": 10, ...}'

从家里节点 SSH 到办公室内网机器,确认连接正常:

$ ssh <internal-host> "hostname && uptime -p"
internal-host
up 3 weeks, 3 days, 18 hours

验证通过后恢复备份路由 metric 为 200,主路由重新生效。

最终拓扑 链接到标题

flowchart LR A["路由 peer A
主路由
metric=100"] -->|192.168.*.*/24| B["办公子网"] C["路由 peer B
备份路由
metric=200"] -->|192.168.*.*/24| B D["远程节点
家庭"] --> A D -.->|A 故障时| C style A fill:#e3f2fd,stroke:#1565c0 style C fill:#fff3e0,stroke:#e65100 style B fill:#c8e6c9,stroke:#2e7d32 style D fill:#f3e5f5,stroke:#7b1fa2

总结 链接到标题

  • NetBird Route API 支持同一子网配置多个路由 peer,通过 metric 控制优先级
  • 主路由故障时自动切换,无需人工介入
  • macOS 作路由 peer 需手动开启 net.inet.ip.forwarding
  • 两个路由 peer 在同一办公网络内,无法应对整个办公网络断电的场景,但覆盖了单节点宕机的绝大部分情况