如何配置 Cilium 和 BGP 協同工作?
官方提供了多篇文檔說明如何配置 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
上圖中,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 如下圖:
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
:
參考以下配置 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 接地的路由信息如下:
上圖中,我們發現 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:
我們登錄 router 節點通過 vtysh 查看 Cilium 是否 bgpd 服務建立了連接,並且查看其通告的路由信息如下:
需要注意,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 時的路徑選擇:
Cilium BGP Control Plane
BGP Controller 控制器是 Cilium 高版本推出的針對內置 BGP Speaker 更加細粒度的控制功能,其功能是上述 ConfigMap 的擴展。
感興趣的讀者可以參考以下文檔:
-
Cilium BGP Control Plane[7]
-
LoadBalancer IP Address Management (LB IPAM)[8]
參考
-
BGP in the Data Center[9]
-
讀書筆記 [10]
-
Using BIRD to run BGP[11]
-
L4LB for Kubernetes: Theory and Practice with Cilium+BGP+ECMP[12]
引用鏈接
[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/
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/FlOb0E_oToQY9Z2u0g9QyQ