如何快速让 Cilium 和 BGP 协同工作

公众号关注 「奇妙的 Linux 世界」

设为「星标」,每天带你玩转 Linux !

9dde4791ac8f175063baa964d0f3cc57.png

背景

官方提供了多篇文档说明如何配置 Cilium 和 BGP 协同工作,本文主要对以下部分功能进行验证:

  • Using BIRD to run BGP[1]

  • Using kube-router to run BGP[2]

  • BGP[3]

  • Cilium BGP Control Plane[4]

为了模拟支持 BGP 的网络环境,文中所有节点均是通过 vagrant 创建的 VM, 网络拓扑如下图。

注意:实际配置时使用 vagrant 创建的 VM 模拟网络环境并不便利。可以参考以下文章,使用 ContainerLab 和 Kind 进行验证。参考:https://mp.weixin.qq.com/s/k25e7gTIIJLnL_FLlgdHUw

181f6828f8ec80aa96d8939da740fed2.png

上图中,Router 节点包含多张网卡并将作为其他两台主机的网关,对应的系统配置如下:

net.ipv4.ip_forward = 1
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv6.conf.all.forwarding = 1

node1、node2 节点均只包含一张网卡,其默认路由均指向 router 节点(node1 指向 10.0.1.2,node2 指向 10.0.2.2)。

node1、node2 上将部署 Kubernetes 和 Cilium。

基于 Bird 部署容器网络

Cilium 为 PodCIDR 提供了 Encapsulation 和 Native-Routing 两种组网方式。

Native-Routing 方案中,Cilium 会将 PodCIDR 中的跨节点流量委托给 Linux 内核的路由子系统,此时 Linux 内核需要知道如何路由 PodCIDR 中的特定地址。

当用户的所有 Node 处于同一个 L2 网络时,我们可以设置参数 autoDirectNodeRoutes=true ,此时整个 PodCIDR 路由信息被插入到每个节点的内核路由表中,用户无需其他额外工作即部署完成。

上述测试环境中,Node1 和 Node2 分别处于 10.0.1.0/24 和 10.0.2.0/24 ,并不满足设置 autoDirectNodeRoutes 的条件,因此我们需要借助设置 BGP 服务完成 PodCIDR 组网。

参考 Using BIRD to run BGP[5] 文档中的描述,并结合测试环境的网络拓扑,我们设定测试节点的 ASN 如下图:

2991ac0e3c15f31b03a51c9ca7ffd1e6.png

1. FRR 设置

在 router 上部署软件路由器 FRR (参考:https://rpm.frrouting.org/), 如下:

FRRVER="frr-stable"
curl -O https://rpm.frrouting.org/repo/$FRRVER-repo-1-0.el7.noarch.rpm
sudo yum install ./$FRRVER*
sudo yum install frr frr-pythontools

修改 /etc/frr/daemons 文件,打开 bgpd 功能(设置配置文件中 bgpd=yes)。编辑 /etc/frr/frr.conf 文件,写入以下 BGP 相关的配置:

frr version 8.4.1
frr defaults traditional
hostname router                        # 主机名
log syslog informational
!
router bgp 65100                       # router 节点的本地 ASN
 bgp router-id 192.168.121.16          # router-id
 no bgp ebgp-requires-policy
 neighbor 10.0.1.10 remote-as 65010    # 配置 Node1 作为 router 的邻居,ASN 为 65010
 neighbor 10.0.2.10 remote-as 65020    # 配置 Node2 作为 router 的邻居,ASN 为 65020
exit
!

完成上述配置后,启动 frr 服务 systemctl restart frr !

2. 部署 Cilium

登录 Node1 或 Node2 部署 Cilium,配置如下:

k8sServiceHost: "10.0.1.10"
k8sServicePort: 6443
kubeProxyReplacement: strict
devices: eth1
ipam:
  operator:
    clusterPoolIPv4PodCIDR: "172.31.254.0/23"
    clusterPoolIPv4PodCIDRList: []
    clusterPoolIPv4MaskSize: 26
loadBalancer:
  mode: dsr
tunnel: disabled 
autoDirectNodeRoutes: false
bpf:
  masquerade: true
ipv4NativeRoutingCIDR: "172.31.254.0/23"
socketLB:
  enabled: true
nodePort:
  enabled: true
externalIPs:
  enabled: true
hostPort:
  enabled: true

Cilium 容器就绪后,Kubernetes 集群中可以正常创建容器并分配容器IP,但是跨节点容器无法正常通信。

3. 部署 Bird

Cilium 官方文档中,给出了 Bird2 的配置示例。我们可以直接通过 yum -y install bird2 安装。

查看各个节点分配的 PodCIDR 网段,执行kubectl -n kube-system exec -it ds/cilium -- cilium node list

dd292942c909a6c2bc3cd9f17fe86390.png

参考以下配置 bird2 服务,配置文件 /etc/bird.conf

router id 10.0.1.10;
protocol device {
    scan time 10;           # Scan interfaces every 10 seconds
}
# Disable automatically generating direct routes to all network interfaces.
protocol direct {
    disabled;               # Disable by default
}
# Forbid synchronizing BIRD routing tables with the OS kernel.
protocol kernel {
    ipv4 {                # Connect protocol to IPv4 table by channel
        import none;      # Import to table, default is import all
        export none;      # Export to protocol. default is export none
    };
}
# Static IPv4 routes.
protocol static {
    ipv4;
    route 172.31.254.0/26 via "cilium_host";    # 将 PodCICR 通告到上游,PS:这里是 Node1 分配到的 PodCIDR
}
# BGP peers
protocol bgp uplink0 {
    description "BGP uplink 0";
    local 10.0.1.10 as 65010;                   # 设置当前节点的 ASN ,PS:这里示例的是 Node1
    neighbor 10.0.1.2 as 65100;                 # 设置节点的 Neighbor, 这里是 router 节点
    ipv4 {
        import filter {reject;};
        export filter {accept;};
    };
}

在 Node1、Node2 按照上述方式配置完成 Bird2 后启动服务。执行以下命令检查 BGP 连接是否正常:

# 在 router 执行以下命令

# 查看 bgp peer 连接
vtysh -c "show bgp summary"
# 查看注册到 router 的路由信息
vtysh -c "show bgp ipv4 all"

完成上述流程后,使得 Node1 和 Node2 上的容器网络打通,并且任意以 router 节点作为默认网关的服务器都可以直连 PodIP。

查看 router 接地的路由信息如下:

a405c59123b5c7dcde392696eb392039.png

上图中,我们发现 router 节点被注入了 PodCIDR 。

本文测试环境的网络拓扑非常简单,实际上直接通过命令行直接在 router 节点上插入路由信息可以达到同样效果。在实际生产中,我们可以通过 BGP 动态发现简化配置流程。

内置 BGP

Cilium 1.10 之后的版本内置了 BGP Speaker  的功能,用户无需在节点上部署 Bird2 也可以向外广播节点的 PodCIDR 信息,并且 1.12 版本中 Cilium 参考 Metallb 实现支持基于 BGP + ECMP 的 LoadBalancer 功能。

参考文档[6]中的描述,启用内置的 BGP 能力需要额外创建以下 ConfigMap,Cilium-Agent 和 Cilium-Operator 启动时均会挂载该配置。

apiVersion: v1
kind: ConfigMap
metadata:
  name: bgp-config
  namespace: kube-system
data:
  config.yaml: |
    peers:
      - peer-address: 192.168.121.16
        peer-asn: 65100
        my-asn: 65000
    address-pools:
      - name: default
        protocol: bgp
        addresses:
          - 192.0.2.0/24

上述配置中,Cilium 将使用 192.168.121.16 连接 router 节点的 bgpd 服务(PS:BGP 建立连接是基于 TCP 的),并且 Node1 和 Node2 将使用相同的 ASN 65000。

address-pools 指定的是 LoadBalanacer 的 IP 地址池,当用户创建 LoadBalancer 类型的 Service 时,Cilium 将自动从该地址池中分配 ip 地址,并自动进行 BGP 宣告。

安装上述 Configmap 后,我们需要为 Cilium 为添加如下配置:

bgp:
  enabled: true
  announce:
    loadbalancerIP: true
    podCIDR: true
loadBalancer:
  mode: snat      # 此处使用 dsr 模式时,存在问题

创建 service 如下:

apiVersion: v1
kind: Service
metadata:
  name: whoami-lb
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  selector:
    app: whoami

cilium 自动分配 192.0.2.0 作为 service 的 EXTERNAL-IP:

b535bc326ddc7202ebd9b806679337af.png

我们登录 router 节点通过 vtysh 查看 Cilium 是否 bgpd 服务建立了连接,并且查看其通告的路由信息如下:

0e642a2ac21aeff7377312ccb661bcd0.png

需要注意,router 节点上我们需要添加 ECMP 的相关配置,并且依然静态指定 Node1 和 Node2 作为 neighbor 如下:

frr version 8.4.1
frr defaults traditional
hostname router
log syslog informational
!
router bgp 65100
 bgp bestpath as-path multipath-relax
 bgp bestpath bandwidth skip-missing
 bgp router-id 192.168.121.16
 no bgp ebgp-requires-policy
 neighbor 10.0.1.10 remote-as 65000
 neighbor 10.0.2.10 remote-as 65000
exit
!

执行 vtysh -c "show bgp ipv4 unicast 192.0.2.0/32" 我们可以查看当前,FRR 执行 ECMP 时的路径选择:

08ab40ec9b4a20d2dbfac654ef541651.png

Cilium BGP Control Plane

BGP Controller 控制器是 Cilium 高版本推出的针对内置 BGP Speaker 更加细粒度的控制功能,其功能是上述 ConfigMap 的扩展。

感兴趣的读者可以参考以下文档:

  • Cilium BGP Control Plane[7]

  • LoadBalancer IP Address Management (LB IPAM)[8]

参考

引用链接

[1]

Using BIRD to run BGP: https://docs.cilium.io/en/stable/gettingstarted/bird/

[2]

Using kube-router to run BGP: https://docs.cilium.io/en/stable/gettingstarted/kube-router/

[3]

BGP: https://docs.cilium.io/en/stable/gettingstarted/bgp/

[4]

Cilium BGP Control Plane: https://docs.cilium.io/en/stable/gettingstarted/bgp-control-plane/

[5]

Using BIRD to run BGP: https://docs.cilium.io/en/stable/gettingstarted/bird/

[6]

参考文档: https://docs.cilium.io/en/stable/gettingstarted/bgp/

[7]

Cilium BGP Control Plane: https://docs.cilium.io/en/latest/network/bgp-control-plane/#bgp-control-plane

[8]

LoadBalancer IP Address Management (LB IPAM): https://docs.cilium.io/en/latest/network/lb-ipam/

[9]

BGP in the Data Center: chrome-extension://ikhdkkncnoglghljlkmcimlnlhkeamad/pdf-viewer/web/viewer.html?file=https%3A%2F%2Fdocs.jetstream-cloud.org%2Fattachments%2Fbgp-in-the-data-center.pdf

[10]

读书笔记: https://arthurchiao.art/blog/bgp-in-data-center-zh/

[11]

Using BIRD to run BGP: https://docs.cilium.io/en/stable/gettingstarted/bird/

[12]

L4LB for Kubernetes: Theory and Practice with Cilium+BGP+ECMP: https://arthurchiao.art/blog/k8s-l4lb/

本文转载自:「Yoaz 的博客」,原文:https://url.hi-linux.com/12Rw2,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。

724dd4b5c5a2358c69d23186be2216d8.gif

最近,我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神,有兴趣的同学可以加入和我们一起交流技术,在 「奇妙的 Linux 世界」 公众号直接回复 「加群」 邀请你入群。

664f280f969a6b01d6b7b98ba9774172.png

你可能还喜欢

点击下方图片即可阅读

bc5084fd089b1ac308039e6effea5622.png

Linux 内核观测技术 eBPF 中文入门指南

d608464cd0c633c81d1cefb24fd5196a.png
点击上方图片,『美团|饿了么』外卖红包天天免费领

64a1cfacedb460d35411eccfbc7182f6.png

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值