武汉勇远科技
勇远科技微信二维码
微信咨询
扫一扫获取方案
24小时服务热线
177 6259 3139

基于 WireGuard 与 FRR 的企业级 SD-WAN 跨地域组网实战

1. 背景与企业痛点

随着企业跨地域扩张,传统的 MPLS 专线成本极其高昂(数千元/月/兆),而传统的 IPsec VPN 配置复杂、握手慢、NAT 穿透困难。
当前最前沿的 SD-WAN(软件定义广域网)架构,核心正是利用廉价的宽带互联网,通过现代化的加密隧道协议,结合动态路由算法,实现去中心化的全网互联。

本文将演示如何利用 Linux 内核级协议 WireGuard,结合开源路由套件 FRRouting (FRR),在总公司与分公司之间构建一条带 OSPF 动态路由的 SD-WAN 链路。

2. 架构设计与环境准备

  • HQ(总部):
  • WAN IP: 119.29.x.x (固定公网 IP)
  • LAN 网段: 10.10.0.0/16
  • WireGuard IP: 172.16.0.1/30
  • Branch(分公司):
  • WAN IP: 动态公网 IP (或处于 NAT 之后)
  • LAN 网段: 10.20.0.0/16
  • WireGuard IP: 172.16.0.2/30

我们在两端的 Linux 边缘网关上分别安装 wireguard-toolsfrr

3. WireGuard 加密隧道配置

WireGuard 相比 IPsec 具有极高的性能,因为它直接运行在内核态,且代码量极小。

3.1 生成密钥对

在 HQ 和 Branch 上分别执行:

# 生成私钥和公钥
wg genkey | tee privatekey | wg pubkey > publickey

3.2 配置 HQ 网关 (wg0.conf)

在 HQ 网关的 /etc/wireguard/wg0.conf 中填入:

[Interface]
Address = 172.16.0.1/30
ListenPort = 51820
PrivateKey = <HQ_PRIVATE_KEY>
# 必须开启内核转发
PreUp = sysctl -w net.ipv4.ip_forward=1

[Peer]
# 填入分公司的公网 Key
PublicKey = <BRANCH_PUBLIC_KEY>
# 允许 OSPF 组播地址及所有网段通过隧道
AllowedIPs = 0.0.0.0/0
# 保持 NAT 穿透心跳
PersistentKeepalive = 25

3.3 配置 Branch 网关 (wg0.conf)

[Interface]
Address = 172.16.0.2/30
PrivateKey = <BRANCH_PRIVATE_KEY>
PreUp = sysctl -w net.ipv4.ip_forward=1

[Peer]
PublicKey = <HQ_PUBLIC_KEY>
Endpoint = 119.29.x.x:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

启动隧道:systemctl enable --now wg-quick@wg0。此时两端 172.16.0.1172.16.0.2 应能互相 ping 通。

4. 动态路由 FRR 实战配置

在纯静态路由下,每增加一个分公司就要手动添加几十条路由表。我们引入 FRR 跑 OSPF 协议,实现路由自动学习。

4.1 安装与开启 OSPF 守护进程

apt install frr
# 开启 ospfd
sed -i 's/ospfd=no/ospfd=yes/g' /etc/frr/daemons
systemctl restart frr

4.2 FRR 配置 (HQ 端)

进入 vtysh 配置 OSPF:

HQ-Gateway# configure terminal
HQ-Gateway(config)# interface wg0
HQ-Gateway(config-if)# ip ospf network point-to-point  ! 指定网络类型为P2P,加快收敛
HQ-Gateway(config-if)# exit
HQ-Gateway(config)# router ospf
HQ-Gateway(config-router)# ospf router-id 172.16.0.1
HQ-Gateway(config-router)# network 172.16.0.0/30 area 0    ! 宣告 wg0 隧道网段
HQ-Gateway(config-router)# network 10.10.0.0/16 area 0     ! 宣告总部本地网段
HQ-Gateway(config-router)# exit
HQ-Gateway(config)# write

4.3 FRR 配置 (Branch 端)

Branch-Gateway# configure terminal
Branch-Gateway(config)# interface wg0
Branch-Gateway(config-if)# ip ospf network point-to-point
Branch-Gateway(config-if)# exit
Branch-Gateway(config)# router ospf
Branch-Gateway(config-router)# ospf router-id 172.16.0.2
Branch-Gateway(config-router)# network 172.16.0.0/30 area 0
Branch-Gateway(config-router)# network 10.20.0.0/16 area 0
Branch-Gateway(config-router)# exit
Branch-Gateway(config)# write

5. 验证与排障

在任意一端输入 show ip route ospf

HQ-Gateway# show ip route ospf
O   10.20.0.0/16 [110/20] via 172.16.0.2, wg0, weight 1, 00:02:15

看到标志为 O 的路由表,说明 HQ 已经自动学习到了分公司的 10.20.x.x 网段。

高阶防火墙配置 (Iptables)

必须在边缘网关允许 WireGuard 接口的流量放行,否则内网机器无法互通:

iptables -A FORWARD -i wg0 -j ACCEPT
iptables -A FORWARD -o wg0 -j ACCEPT

6. 总结

利用 WireGuard + FRR,我们仅用几十行配置文件,就搭建起了一个高性能、支持 NAT 穿透且具备动态路由自愈能力的 SD-WAN 骨干网。无论未来增加多少个分公司结点,只需宣告本地网段,核心路由表即可自动全网同步,彻底释放了网络工程师的生产力。