- 将二进制可执行文件和配置文件拷贝到每台测试机器上后
- IP blocks for this host.
- Aggregation of routes on this host; export the block, nothing beneath it.
- 允许进行BGP路由宣告的ippool
- 与10.30.81.130建立IBGP关系
- 与10.30.81.126建立IBGP关系
- IP blocks for this host.
- Aggregation of routes on this host; export the block, nothing beneath it.
- IP blocks for this host.
- Aggregation of routes on this host; export the block, nothing beneath it.
- 满足netns下数据包封包
- 创建回netns的路由
- 分别从netns下ping本机网卡和对端主机网卡
Larioy
每一个没有朋友圈动态的日子都在踏实努力着
confd与bird自动配置bgp
calico bird bgp
calico bgp
发布日期: 2021-09-02文章字数: 2.3k阅读时长: 11 分阅读次数: 227
在对calico做调查时了解到其使用confd与bird来自动化的配置和维护bgp网络, 看完官方文档和部署学习calico的bgp模式后记录。
架构搭建
此次搭建的是同一自治域的三台bgp speaker做全连接,两两之间建立IBGP关系, 架构为同网段的三台主机和另一个台10.30.81.120不运行bird不建立bgp关系的主机
10.30.81.126: 其分配的子网段为192.168.60.0/24
10.30.81.128: 其分配的子网段为192.168.10.0/24和192.168.100.0/24, 其中192.168.10.0/24设置为黑洞路由, 其下所有子网路由或主机路由不会进行bgp路由宣告;192.168.100.0/24及其下的子网路由主机路由均会被进行bgp路由宣告
10.30.81.130: 其分配的子网段为192.168.60.0/24
bird的安装和配置
bird官网,bird作为linux主机上的bgp代理,需要在每一台主机上都安装好。向calico靠齐,直接从运行的calico-node中拷贝相关二进制执行文件和配置文件出来进行修改即可。
bash
mkdir bird; cd bird
kubectl get pod -A | grep calico-node
kubectl cp -n kube-system podName:/bin .
kubectl cp -n kube-system podName:/etc/calico .
将二进制可执行文件和配置文件拷贝到每台测试机器上后
cp bin/bird bin/birdcl /usr/bin
cp -r calico /etc
calico中关于bgp配置的相关文件及其作用划分的很清晰啦
bash
[root@k8s-node1 ~]# tree /etc/calico/confd/config/
/etc/calico/confd/config/
├── bird6_aggr.cfg
├── bird6.cfg
├── bird6_ipam.cfg # 和每个ippool设置的cidr一致, 允许将从其他节点学到的子网进行宣告, 所以所有处于使用状态的ippool的cidr对应网段都要accept
├── bird_aggr.cfg # 用来配置节点分配cidr的, 节点分配到的cidr对应网段设置为黑洞路由
├── bird.cfg # bird的主配置文件, 针对每种协议设置
└── bird_ipam.cfg
下面以一台主机的配置文件来解释, 三台主机的配置见下文
10.30.81.128上bird配置
bird_aggr.cfg配置文件, 设置本机获得的子网路由为黑洞路由,并允许将该子网段宣告到BGP网络中
bash
protocol static {
IP blocks for this host.
route 192.168.30.0/24 blackhole;
}
Aggregation of routes on this host; export the block, nothing beneath it.
function calico_aggr ()
{
# Block 10.244.235.192/26 is confirmed
if ( net = 192.168.30.0/24 ) then { accept; }
if ( net ~ 192.168.30.0/24 ) then { reject; }
}
bird_ipam.cfg配置文件,配置允许进行BGP路由宣告的最大网段, 在calico中可以理解为所有生效中的ippool的cidr并集, 结合bird_ipam.toml文件有助于理解。比如下面设置了两个ippool,10.10.0.0/16和192.168.0.0/16。
bash
允许进行BGP路由宣告的ippool
filter calico_export_to_bgp_peers {
apply_communities();
calico_aggr();
if ( net ~ 10.10.0.0/16 ) then {
accept;
}
if ( net ~ 192.168.0.0/16 ) then {
accept;
}
reject;
}
filter calico_kernel_programming {
if ( net ~ 10.10.0.0/16 ) then {
krt_tunnel = “”;
accept;
}
accept;
}
bird.cdf配置文件, 加载需要通告路由的配置bird_ipam.cfg和bird_aggr.cfg进行路由通告。 可以增加”debug all;”输出bgp建立连接与路由通告的详细信息。此为简单的bgp连接, 未设置router reflector与联盟。
bash
function applycommunities ()
{
}
include “bird_aggr.cfg”;
include “bird_ipam.cfg”;
router id 10.30.81.128;
protocol kernel {
learn;
persist;
scan time 2;
import all;
export filter calico_kernel_programming;
graceful restart;
merge paths on;
}
protocol device {
debug { states };
scan time 2; # Scan interfaces every 2 seconds
}
protocol direct {
debug { states };
interface -“cali“, -“kube-ipvs_”, “*”;
}
template bgp bgp_template {
debug { states };
description “Connection to BGP peer”;
local as 64513;
multihop;
gateway recursive;
import all;
debug all; # 开启调试
export filter calico_export_to_bgp_peers;
add paths on;
connect delay time 2;
connect retry time 50;
error wait time 5,30;
}
与10.30.81.130建立IBGP关系
protocol bgp Mesh_10_30_81_130 from bgp_template {
neighbor 10.30.81.130 as 64513;
source address 10.30.81.128;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
与10.30.81.126建立IBGP关系
protocol bgp Mesh_10_30_81_126 from bgp_template {
neighbor 10.30.81.126 as 64513;
source address 10.30.81.128;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
10.30.81.130上bird配置
bash
:’
bird_aggr.cfg文件
‘
protocol static {
IP blocks for this host.
route 192.168.10.0/24 blackhole;
}
Aggregation of routes on this host; export the block, nothing beneath it.
function calico_aggr ()
{
if ( net = 192.168.10.0/24 ) then { accept; }
if ( net ~ 192.168.10.0/24 ) then { reject; }
}
:’
bird_ipam.cfg配置文件
‘
filter calico_export_to_bgp_peers {
apply_communities();
calico_aggr();
if ( net ~ 10.10.0.0/16 ) then {
accept;
}
if ( net ~ 192.168.0.0/16 ) then {
accept;
}
reject;
}
filter calico_kernel_programming {
if ( net ~ 10.10.0.0/16 ) then {
krt_tunnel = “”;
accept;
}
accept;
}
:’
bird.cfg配置文件, 只列出与10.30.81.128中不同的部分
‘
router id 10.30.81.130;
protocol bgp Mesh_10_30_81_128 from bgp_template {
neighbor 10.30.81.128 as 64513;
source address 10.30.81.130;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
protocol bgp Mesh_10_30_81_126 from bgp_template {
neighbor 10.30.81.126 as 64513;
source address 10.30.81.130;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
10.30.81.126上bird配置
bash
:’
bird_aggr.cfg文件
‘
protocol static {
IP blocks for this host.
route 192.168.60.0/24 blackhole;
}
Aggregation of routes on this host; export the block, nothing beneath it.
function calico_aggr ()
{
if ( net = 192.168.60.0/24 ) then { accept; }
if ( net ~ 192.168.60.0/24 ) then { reject; }
}
:’
bird_ipam.cfg配置文件
‘
filter calico_export_to_bgp_peers {
apply_communities();
calico_aggr();
if ( net ~ 10.10.0.0/16 ) then {
accept;
}
if ( net ~ 192.168.0.0/16 ) then {
accept;
}
reject;
}
filter calico_kernel_programming {
if ( net ~ 10.10.0.0/16 ) then {
krt_tunnel = “”;
accept;
}
accept;
}
:’
bird.cfg配置文件, 只列出与10.30.81.128中不同的部分
‘
router id 10.30.81.130;
protocol bgp Mesh_10_30_81_128 from bgp_template {
neighbor 10.30.81.128 as 64513;
source address 10.30.81.126;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
protocol bgp Mesh_10_30_81_130 from bgp_template {
neighbor 10.30.81.130 as 64513;
source address 10.30.81.126;
#passive on; # Mesh is unidirectional, peer will connect to us.
}
节点网络配置
测试只需在两台主机创建veth-pair即可, 在10.30.81.128和10.30.81.130上创建并配置ip和路由, 相关配置如下。
bash
:’
10.30.81.128上配置
‘
ip netns add ns0
ip link add veth0 type veth peer name veth1
ip link set veth0 netns ns0
echo 1 > /proc/sys/net/ipv4/conf/veth1/proxy_arp
ip link set veth1 a cc:cc:cc:cc:cc:cc
ip netns exec ns0 ip a a 192.168.10.1/32 dev veth0
ip netns exec ns0 ip link set veth0 up
ip link set veth1 up
ip neigh show
ip netns exec ns0 ip neigh add 169.254.1.1 dev veth0 lladdr cc:cc:cc:cc:cc:cc
ip netns exec ns0 route -n
ip netns exec ns0 route add -net 0.0.0.0/0 dev veth0
满足netns下数据包封包
ip netns exec ns0 route add -net 0.0.0.0/0 gw 169.254.1.1 dev veth0
创建回netns的路由
route add 192.168.10.1/32 dev veth1
ip netns exec ns0 route -n
分别从netns下ping本机网卡和对端主机网卡
ip netns exec ns0 ping 10.30.81.120
route add 192.168.10.1/32 dev veth1
ip netns exec ns0 ping 10.30.81.120
ip netns exec ns0 ping 10.30.81.121
:’
10.30.81.130上配置
‘
ip netns add ns0
ip link add veth0 type veth peer name veth1
ip link set veth0 netns ns0
echo 1 > /proc/sys/net/ipv4/conf/veth1/proxy_arp
ip link set veth1 a aa:bb:bb:bb:bb:bb
ip netns exec ns0 ip a a 192.168.30.1/32 dev veth0
ip netns exec ns0 ip link set veth0 up
ip link set veth1 up
ip neigh show
ip netns exec ns0 ip neigh add 169.254.1.1 dev veth0 lladdr aa:bb:bb:bb:bb:bb
ip netns exec ns0 route -n
ip netns exec ns0 route add -net 0.0.0.0/0 dev veth0
ip netns exec ns0 route add -net 0.0.0.0/0 gw 169.254.1.1 dev veth0
route add 192.168.30.1/32 dev veth1
ip netns exec ns0 route -n
最后测试结果如下, 在10.30.81.126分别ping 192.168.10.1和192.168.30.1均有回复
ibgp路由宣告测试
ibgp路由宣告测试
待丰富项
可以参考官网和cilium部分篇章看看配置
建立ebgp连接的配置
使用router reflector后的配置
联盟架构下的配置
confd自动更新bird配置
分析confd的模板等配置的目的是为了更好的理解bird的配置的产生过程。confd是使用go语言编写的配置更新工具, 可以监控多种数据源中数据的变化然后触发模板渲染和服务更新, 支持的数据源有etcd、zookeeper等。 calico对confd进行了改造, 使其适应于根据datastore_type类型生成监控资源的客户端, 拓展text/template模板渲染的函数等。 我们使用confd一般都会指定一个配置文件目录, calico中直接默认写死去了。 主要结构体如下
go
// confd.toml文件
type Config struct {
ConfDir string
ConfigDir string
KeepStageFile bool
Noop bool
Prefix string
StoreClient backends.StoreClient
SyncOnly bool
TemplateDir string
}
// 对应每个模板文件, 模板渲染的逻辑主要都在这个结构体的方法里面
type TemplateResource struct {
CheckCmd string toml:"check_cmd"
Dest string // 模板渲染出来的文件保存位置
FileMode os.FileMode
Gid int
Keys []string // toml文件中的keys
Mode string
Prefix string // toml文件中的prefix字段
ReloadCmd string toml:"reload_cmd"
// 服务重加载命令
Src string // toml文件中的src, 用于暴露模板名字
StageFile *os.File
Uid int
ExpandedKeys []string // toml中prefix与key列表每一项的拼接
// calico自定义的模板函数, 最终在src/github.com/kelseyhightower/confd/pkg/resource/template/resource.go
// 的createStageFile函数创建template实例时注册进去
funcMap map[string]interface{}
keepStageFile bool
noop bool
store memkv.Store // 存储最新监控值的地方, 在执行模板渲染时所有的数据源就是从这里面取值的
storeClient backends.StoreClient // calico的数据源, 可以是apiserver或者是etcd, 因此使用接口类型做抽象, 根据datastore_type生成对应的client
syncOnly bool
shellCmd string // 根据操作系统平台填充/bin/sh或powershell
}
confd中是根据key监控资源变化的, 对应到apiserver类型的datastore_type, 需要中间做一层映射, 具体的映射需要看代码。
datastore_type为k8s时, 监控的资源也是通过控制平面传到confd中的, 看另外几篇分析文件
文章作者: larioy
文章链接: https://larioy.gst.monster/2021/09/02/k8s-ji-chong-cni-fang-an-jie-xi/calico/confd-yu-bird-zi-dong-pei-zhi-bgp/
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 larioy !
calico bird bgp
评论
昵称/QQ号(必填)
邮箱(必填)
网址(https://)
留下你的足迹..
来发评论吧~
Powered By Valine
v1.4.14
上一篇
ensp模拟calico跨网段bgp网络ensp模拟calico跨网段bgp网络
2021-09-05 cni calico bgp
cni calico bgp ensp
下一篇
calico架构与网络模型深入分析calico架构与网络模型深入分析
2021-08-26 cni calico bgp
calico bgp ipip vxlan
目录
架构搭建
bird的安装和配置
10.30.81.128上bird配置
10.30.81.130上bird配置
10.30.81.126上bird配置
节点网络配置
待丰富项
confd自动更新bird配置
Copyright © 2018-2021 Larioy | 基于Hexo的Matery主题构建
复制成功,请遵循本文的转载规则
查看