首页 / 最佳实践 / 网络 / Calico 网络支持 WireGuard 加密

Calico 网络支持 WireGuard 加密

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

安装情况说明

默认安装

操作系统 内核版本
Linux 默认情况下 5.6 及以上版本均默认安装
Ubuntu 20.04 5.4.0-135-generic
Kylin Linux Advanced Server V10 - SP3 4.19.90-52.22.v2207.ky10.x86_64

默认未安装

操作系统 内核版本
openEuler 4.18.0-147.5.2.13.h996.eulerosv2r10.x86_64
CentOS 7 3.10.0-1160.el7.x86_64
Redhat 8.7 4.18.0-425.3.1.el8.x86_64
Kylin Linux Advanced Server V10 - SP2 4.19.90-24.4.v2101.ky10.x86_64
Kylin Linux Advanced Server V10 - SP1 4.19.90-23.8.v2101.ky10.x86_64
Kylin Linux Advanced Server V10 4.19.90-11.ky10.x86_64

名词解释

名词 解释
wireguardEnabled 启用通过 IPv4 Underlay 网络对 IPv4 流量进行加密。
wireguardEnabledV6 启用通过 IPv6 Underlay 网络对 IPv6 流量进行加密。

注意事项

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

  2. WireGuard 支持 IPv4 和 IPv6 的流量加密,若需对 IPv4 及 IPv6 两种流量加密,则需分别进行配置。具体参数配置说明请参考 Felix 配置说明 ,需配置 wireguardEnabledwireguardEnabledV6 两个参数。

  3. 若系统没有默认安装 WireGuard,则需参考 WireGuard 安装手册 进行手动安装,但可能会出现手动安装 WireGuard 模块失败的情况。

  4. 跨节点的容器网络流量将被加密,加密范围包括从一个主机到另一个主机之间的容器网络;同节点内 Pod 之间的通信以及 Pod 与所在节点之间的流量不会进行加密。

前提条件

操作步骤

  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 加密

      • 方式一:在 CLI 工具中执行命令关闭加密。

        kubectl patch felixconfiguration default --type='merge' -p '{"spec":{"wireguardEnabled":false,"wireguardEnabledV6":false}}'
      • 方式二:修改 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 网卡转发。