Calico 网络支持 WireGuard 加密

Calico 支持对 IPv4 和 IPv6 流量进行 WireGuard 加密,可以通过 FelixConfiguration 资源中的参数独立启用。

安装状态

默认安装

操作系统内核版本
Linux5.6 及以上版本默认安装
Ubuntu 20.045.4.0-135-generic
Kylin Linux Advanced Server V10 - SP34.19.90-52.22.v2207.ky10.x86_64

非默认安装

操作系统内核版本
openEuler4.18.0-147.5.2.13.h996.eulerosv2r10.x86_64
CentOS 73.10.0-1160.el7.x86_64
Redhat 8.74.18.0-425.3.1.el8.x86_64
Kylin Linux Advanced Server V10 - SP24.19.90-24.4.v2101.ky10.x86_64
Kylin Linux Advanced Server V10 - SP14.19.90-23.8.v2101.ky10.x86_64
Kylin Linux Advanced Server V104.19.90-11.ky10.x86_64

术语

术语说明
wireguardEnabled启用 IPv4 流量在 IPv4 下层网络上进行加密。
wireguardEnabledV6启用 IPv6 流量在 IPv6 下层网络上进行加密。

注意事项

  1. 使用 Calico 网络插件时,请确保 natOutgoing 参数设置为 true,以支持 WireGuard 加密。默认情况下,创建集群时该参数已正确配置,无需额外配置。

  2. WireGuard 支持对 IPv4 和 IPv6 流量进行加密;如果需要对两种类型的流量进行加密,则必须分别进行配置。有关详细的参数配置,请参阅 Felix 配置文档,配置 wireguardEnabledwireguardEnabledV6 两个参数。

  3. 如果 WireGuard 未默认安装,请参阅 WireGuard 安装指南 进行手动安装,尽管在某些情况下可能会出现 WireGuard 模块的手动安装失败。

  4. 跨节点的容器间流量将被加密,包括从一个主机到另一个主机的网络流量;但是,在同一节点的 Pods 之间及 Pod 与其宿主节点之间的通信将不会被加密。

先决条件

  • 必须事先在集群中的所有节点上安装 WireGuard。有关详情,请参阅 WireGuard 安装文档。未安装 WireGuard 的节点不支持加密。

步骤

  1. 启用或禁用 IPv4 和 IPv6 加密。

    注意:以下命令必须在节点所在的 Master 节点的 CLI 工具中执行。

    • 仅启用 IPv4 加密

      kubectl patch felixconfiguration default --type='merge' -p '{"spec":{"wireguardEnabled":true}}'
    • 仅启用 IPv6 加密

      kubectl patch felixconfiguration default --type='merge' -p '{"spec":{"wireguardEnabledV6":true}}'
    • 启用 IPv4 和 IPv6 加密

      kubectl patch felixconfiguration default --type='merge' -p '{"spec":{"wireguardEnabled":true,"wireguardEnabledV6":true}}'
    • 同时禁用 IPv4 和 IPv6 加密

      • 方法 1:在 CLI 工具中执行命令以禁用加密。

        kubectl patch felixconfiguration default --type='merge' -p '{"spec":{"wireguardEnabled":false,"wireguardEnabledV6":false}}'
      • 方法 2:修改 felixconfiguration 配置文件以禁用加密。

        1. 执行以下命令以打开 felixconfiguration 配置文件。

          kubectl get felixconfiguration -o yaml default
        2. wireguardEnabledwireguardEnabledV6 参数设置为 false 以禁用 WireGuard 加密。

          apiVersion: crd.projectcalico.org/v1
          kind: FelixConfiguration
          metadata:
            annotations:
              projectcalico.org/metadata: '{"uid":"f5facabd-8304-46d6-81c1-f1816235b487","creationTimestamp":"2024-08-06T03:46:51Z"}'
            generation: 2
            name: default
            resourceVersion: "890216"
          spec:
            bpfLogLevel: ""
            floatingIPs: Disabled
            logSeverityScreen: Info
            reportingInterval: 0s
            wireguardEnabled: false        # 改为 true 以启用 IPv4 加密
            wireguardEnabledV6: false     # 改为 true 以启用 IPv6 加密
          
  2. 完成 Calico WireGuard 加密配置后,执行以下命令确认 WireGuard 加密状态。如果 IPv4 和 IPv6 加密均已启用,Status 字段下存在 wireguardPublicKeywireguardPublicKeyV6 表示成功激活;如果 IPv4 和 IPv6 加密均已禁用,则这些字段不会包含 wireguardPublicKeywireguardPublicKeyV6,表示成功停用。

    calicoctl get node <NODE-NAME> -o yaml # 将 <NODE-NAME> 替换为节点名称。

    输出:

    Status: wireguardPublicKey: L/MUP9+Yxx/xxxxxxxxxxxx/xxxxxxxxxx =

结果验证

本文使用 IPv4 流量验证作为示例;IPv6 流量验证与 IPv4 类似,此处不再重复。

IPv4 流量验证

  1. 配置 WireGuard 加密后,检查路由信息,节点之间的流量优先使用 wireguard.cali 接口进行消息转发。

    root@test:~# ip rule   # 查看当前路由规则
         0: from all lookup local
         99:  not from all fwmark 0x100000/0x100000 lookup 1    # 对于所有未标记为 0x100000 的数据包,使用路由表 1 进行路由查找
         32766:  from all lookup main
         32767 :  from all lookup default
     
    root@test:~# ip route show table 1    # 显示表 1 的路由条目。
        10.3.138.0 dev wireguard.cali scope link
        10.3.138.0/26 dev wireguard.cali scope link
        throw 10.3.231.192
        10.3.236.128 dev wireguard.cali scope link     # 到达 IP 地址 10.3.236.128 的流量将通过 wireguard.cali 接口发送
        10.3.236.128/26 dev wireguard.cali scope link
        throw 10.10.10.124/30
        10.10.10.200/30 dev wireguard.cali scope link
        throw 10.10.20.124/30
        10.10.20.200/30 dev wireguard.cali scope link
        throw
        10.13.138.0 dev wireguard.cali scope link
        10.13.138.0/26 dev wireguard.cali scope link
        throw 10.13.231.192/26
        10.13.236.128 dev wireguard.cali scope link
        10.13.236.128/26 dev wireguard.cali scope link
    
    root@test:~# ip r get 10.10.10.202    # 当前节点到目标 IP 地址 10.10.10.202 的路由路径
        10.10.10.202 dev wireguard.cali table 1 src 10.10.10.127 uid 0  cache   # 当从当前节点访问目标 IP 地址 10.10.10.202 时,数据包将通过 wireguard.cali 接口发送,使用路由表 1,源地址将设置为 10.10.10.127
    
    root@test:~# ip route    # 显示主路由表
        default via 192.168.128.1 dev eth0 proto static
        10.3.138.0/26 via 10.3.138.0 dev vxlan.
        blackhole 10.3.231.193
        10.3.231.194
        10.3.231.195
        10.3.231.196
        10.3.231.197
        3.231.192/26 proto 80
        dev cali8dcd31cIdOO scope link
        dev cali3012b5b29b scope link
        dev calibeefea2ff87 scope link
        dev cali2b27d5e4053 scope link
        dev cali1a35dbdd639 scope link
        calico on link
    
  2. 在节点上捕获数据包以观察跨节点流量。

    root@test:~# ip a s wireguard.cali    # 查看 wireguard.cali 网络接口的详细信息
        30: wireguard.cali: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1440 qdisc noqueue state UNKNOWN group default qlen 1000
        link/none
        inet 10.10.10.127/32 scope global wireguard.cali   # wireguard.cali 接口分配的 IP 地址为 10.10.10.127
        valid_lft forever preferred_lft forever
    
    root@test:~# tcpdump -i wireguard.cali -nnve icmp   # 捕获并显示通过 wireguard.cali 的 ICMP 数据包
        tcpdump: listening on wireguard.cali, link-type RAW (Raw IP), capture size 262144 bytes
        08:58:36.987559 ip: (tos 0x0, ttl 63, id 29731, offset 0, flags [DF], proto ICMP (1), length 84)
        10.10.10.125 > 10.10.10.202: ICMP echo request, id 1110, seq 0, length 64
        08:58:36.988683 ip: (tos 0x0, ttl 63, id 1800, offset 0, flags [none], proto ICMP (1), length 84)
        10.10.10.202 > 10.10.10.125: ICMP echo reply, id 1110, seq 0, length 64
        2 packets captured
        2 packets received by filter
        0 packets dropped by kernel
    
  3. 测试表明 IPv4 类型流量通过 wireguard.cali 接口转发。