kubernetes网络(二)之bird实现节点间BGP互联的实验

打印 上一主题 下一主题

主题 1036|帖子 1036|积分 3108

择要

上一篇文章中我们学习了calico的原理,kubernetes中的node节点,利用 calico 的 bird 程序相互学习路由,为了加深对 bird 程序的认识,本文我们将利用bird举行实验,实验中实现了BGP FULL MESH模式让宿主相互学习到对方的容器网段,从而到达容器网段能相互通信的目标。
bird 实验

bird简介



  • BIRD 实际上是 BIRD Internet Routing Daemon 的缩写,是一款可运行在 Linux 和其他类 Unix 系统上的路由软件,它实现了多种路由协议,比如 BGP、OSPF、RIP 等。
  • BIRD 会在内存中维护许多路由表,路由表根据不同的协议,通过与各种“其他事物”互换路由信息,来更新路由规则。这里说的“其他事物”可能是其他的路由表,也可能是外部的路由器,还可以是内核的某些 API。
  • 根本概念
路由表

路由表(Routing tables)是 BIRD 的核心,一个路由表是内存中一组路由规则的聚集,BIRD 根据网络类型的不同会有多种路由表。默认情况下,BIRD 有master默认的路由表。除此外,你也可以创建其他的路由表。
协议与通道

协议(Protocols)将路由表和“其他事物”毗连起来。“其他事物”可以是一个 Socket 对象,毗连了外部的路由器,比方 BGP 路由协议;也可以是修改 FIB 的内核 API,比方kernel协议;也可以是空,比如静态路由static协议。一个协议可以实例化为多个对象,比方创建多个 BGP 协议的实例,以表现多个 BGP 邻居。协议也会提供一些路由属性,根据协议的不同路由属性也不同,比如利用 BGP 协议时,会有bgp_path属性。
本文的目标不是先容bird的底子概念,重点在于bgp的实操,如果需要详细相识干系概念可以查阅文档:https://soha.moe/post/bird-bgp-kickstart.html
目标



  • 学习bird程序的安装方法
  • 学习bird.conf配置文件的语法
  • 学习利用bird实现三台宿主bgp full mesh 模式
  • 三台宿主的各个容器网段能相互通信
实验环境

系统版本bird版本宿主IP容器网段ubuntu16.04BIRD 1.5.010.226.11.27192.168.227.0/24ubuntu16.04BIRD 1.5.010.226.11.22192.168.222.0/24ubuntu16.04BIRD 1.5.010.226.11.21192.168.221.0/24 实验拓扑

三台宿主在同一网段内,每个宿主各自下挂一个容器网段,下面实现三台服务器两两相互建立bgp peer,相互学习路由,终极目标是实现各个容器网段能相互通信。
创建模仿的容器网段


我们知道容器是通过linux 的 namespace 机制实现的一个隔离空间, 这里我们同样借助 namespace 机制 实现一个隔离空间,并为隔离出的命名空间配置网络,模仿一个宿主"挂了"一个容器网段。


  • 宿主10.226.11.22上创建模仿容器网段
  1. # 创建命名空间 netns1
  2. ip netns add netns1
  3. # 创建 veth peer
  4. ip link add veth1 type veth peer name veth2
  5. # 将 veth1 放入 netns1
  6. ip link set veth1 netns netns1
  7. # 查看 veth
  8. ip link show | grep veth
  9. # 为 netns1 中的
  10. ip netns exec netns1 ifconfig veth1 192.168.222.102/24 up
  11. # 为 netns1 指定默认网关,网关是宿主1的网络协议栈
  12. ip netns exec netns1 route add -net 0.0.0.0/0 gw 192.168.222.101
  13. # 为宿主中的 veth2 配置IP地址,这样就实现了宿主与netns1 通过 veth2 与 veth1 的互联
  14. ifconfig veth2 192.168.222.101/24 up
复制代码


  • 宿主10.226.11.21上创建模仿容器网段
  1. ip netns add netns1
  2. ip link add veth1 type veth peer name veth2
  3. ip link set veth1 netns netns1
  4. ip link show | grep veth
  5. ip netns exec netns1 ifconfig veth1 192.168.221.102/24 up
  6. ip netns exec netns1 route add -net 0.0.0.0/0 gw 192.168.221.101
  7. ifconfig veth2 192.168.221.101/24 up
复制代码


  • 宿主10.226.11.27上创建模仿容器网段
  1. ip netns add netns1
  2. ip link add veth1 type veth peer name veth2
  3. ip link set veth1 netns netns1
  4. ip link show | grep veth
  5. ip netns exec netns1 ifconfig veth1 192.168.227.102/24 up
  6. ip netns exec netns1 route add -net 0.0.0.0/0 gw 192.168.227.101
  7. ifconfig veth2 192.168.227.101/24 up
复制代码
bird的安装



  • bird程序的安装
  1. apt-get update
  2. apt-get install bird
  3. /etc/init.d/bird start
复制代码


  • 检查是否安装成功
  1. /etc/init.d/bird status
  2. birdc show status
复制代码
输出如下表现表现安装正常
  1. root@10_226_11_21:/etc/bird# birdc show status
  2. BIRD 1.5.0 ready.
  3. BIRD 1.5.0
  4. Router ID is 10.226.11.21
  5. Current server time is 2024-09-23 15:03:28
  6. Last reboot on 2024-09-22 20:22:36
  7. Last reconfiguration on 2024-09-23 13:01:00
  8. Daemon is up and running
复制代码
bgp full-mesh模式的实现

宿主10.226.11.27上的/etc/bird/bird.conf配置

  1. # This is a minimal configuration file, which allows the bird daemon to start
  2. # but will not cause anything else to happen.
  3. #
  4. # Please refer to the documentation in the bird-doc package or BIRD User's
  5. # Guide on http://bird.network.cz/ for more information on configuring BIRD and
  6. # adding routing protocols.
  7. #log syslog all;
  8. log "/var/log/bird.log" all;
  9. # Change this into your BIRD router ID. It's a world-wide unique identification
  10. # of your router, usually one of router's IPv4 addresses.
  11. router id 10.226.11.27;
  12. filter export_filter_v4 {
  13.         if net ~ [ 192.168.227.0/24 ] then accept; # 如果为默认路由则拒绝
  14.         reject;
  15.         #accept; # 接收所有其他路由
  16. }
  17. # The Kernel protocol is not a real routing protocol. Instead of communicating
  18. # with other routers in the network, it performs synchronization of BIRD's
  19. # routing tables with the OS kernel.
  20. protocol kernel {
  21.         debug { states };
  22.         scan time 10;
  23.         learn;
  24.         persist;
  25.         import none;  # kernel to bird map
  26.         export all;   # Actually insert routes into the kernel routing table
  27. }
  28. # The Device protocol is not a real routing protocol. It doesn't generate any
  29. # routes and it only serves as a module for getting information about network
  30. # interfaces from the kernel.
  31. protocol direct {
  32.         interface "veth2";
  33. }
  34. protocol device {
  35. }
  36. # 与10.226.11.21建立bgp peer的配置
  37. protocol bgp peer_10_226_11_21 {
  38.         debug { states };
  39.         local as 64512;
  40.         neighbor 10.226.11.21 as 64512;
  41.         source address 10.226.11.27;
  42.         #multihop;
  43.         password "passwd";
  44.         direct;
  45.         export filter export_filter_v4; # 使用过滤表 export_filter_v4 控制哪些路由可以发布给bgp peer
  46.         import all; # 从 direct, device, static, kernel 等所有protocol的路由都导入bird 的 bgp 路由
  47. }
  48. # 与10.226.11.22建立bgp peer的配置
  49. protocol bgp peer_10_226_11_22 {
  50.         debug { states };
  51.         # 配置 BGP 的 graceful restart
  52.             # 如果对端因为网络抖动或暂时崩溃而暂时下线,会导致所有传入路由瞬间消失
  53.             # 为了避免这种情况下数据转发中断,才有 graceful restart
  54.             # 建议打开
  55.         graceful restart on;
  56.         # 指定自己的 ASN 为 65550
  57.         local as 64512;
  58.         # 指定对端的 ASN 为 64512,IP 为 10.226.11.22
  59.             # 如果 ASN 和 local as 相同,那么 BIRD 会自动认为这是一个 iBGP,否则是 eBGP
  60.             # i 表示 internal(内部),e 表示 external(外部)
  61.         neighbor 10.226.11.22 as 64512;
  62.         # source: 定义本地地址作为BGP会话的源地址。Default:邻居所连接接口的本端地址。
  63.         source address 10.226.11.27;
  64.         #multihop;
  65.         # password: 如果和对端约定了密码,在这里配置约定好的密码,否则不用写
  66.         password "passwd";
  67.         # direct: eBGP 默认启用可以不写
  68.         # direct: iBGP 如果是直接连接的可以写这个来避免 multihop 被指定
  69.         # 指定邻居为直连。邻居的IP地址必须在直接可达的IP范围内(即与路由器的接口有关联),
  70.         # 否则BGP会话不会启动,而是等待这样的接口出现。另一种选择是多跳选项。默认值:使能eBGP。
  71.         direct;
  72.         #export all; # 将所有 bird 路由表中的路由都通过bgp发布给peer
  73.         #export: 控制哪些路由可以发布给bgp peer
  74.         export filter {
  75.                 # net 匹配的则被filter放行
  76.                 if net ~ [ 192.168.227.0/24 ] then accept;
  77.                 # 其他的路由全部被filter挡住,从而不会被发布给bgp peer
  78.                 reject;
  79.         };
  80.         import all;
  81. }
复制代码
由于配置文件较长,我们展开详细讲解。
Bird.conf配置详细说明
关于bird.conf配置文件的详细说明可以参考文档 https://soha.moe/post/bird-bgp-kickstart.html
  1. log "/var/log/bird.log" all;
复制代码
log的配置,表现bird日志单独记录的文件/var/log/bird.log; 如果你希望log记录到系统默认日志,可以利用
   #log syslog all;
  1. router id 10.226.11.27;
复制代码
定义bgp节点的 router-id , bgp 协议中要求每个节点必须定义一个router-id,而且是全局唯一不能重复。
Protocol 表现
对 kernel 路由表的控制
  1. protocol kernel {
  2.         # 开启debug模式,日志输出更详细
  3.         debug { states };
  4.         # 参与安全重启恢复。如果启用了该选项,并且激活了优雅重启恢复,那么内核协议将推迟路由表的同步,直到恢复结束。注意,内核路由导入到BIRD不受影响。
  5.         graceful restart on;
  6.         # 每 10 秒扫描一次kernel路由表。这个路由表就是用户在宿主上执行ip route show 看到的表
  7.         scan time 10;
  8.         # 允许学习由其他路由守护进程或系统管理员添加到内核路由表中的路由。这只能在支持路由作者识别的系统上实现。
  9.         # 缺省情况下,不引入由kernel(标记为“proto kernel”)创建的路由。使用learn all选项来导入这些路由。
  10.         learn;
  11.         # 告诉BIRD在退出时将所有路由留在路由表中(而不是清理它们)
  12.         persist;
  13.         # import 控制哪些路由被导入到 bird
  14.         import none;  # kernel to bird map
  15.         # export 控制哪些路由从 bird 插入到 kernel 路由表
  16.         export all;   # Actually insert routes into the kernel routing table
  17. }
复制代码
对直连路由的控制
  1. protocol direct {
  2.         # interface 指定哪个接口的路由将被导入到bird路由表
  3.         interface "veth2";
  4. }
复制代码
控制对bgp peer 10.226.11.22 的路由发布
  1. protocol bgp peer_10_226_11_22 {
  2.         debug { states };
  3.         # 配置 BGP 的 graceful restart
  4.             # 如果对端因为网络抖动或暂时崩溃而暂时下线,会导致所有传入路由瞬间消失
  5.             # 为了避免这种情况下数据转发中断,才有 graceful restart
  6.             # 建议打开
  7.         graceful restart on;
  8.         # 指定自己的 ASN 为 65550
  9.         local as 64512;
  10.         # 指定对端的 ASN 为 64512,IP 为 10.226.11.22
  11.             # 如果 ASN 和 local as 相同,那么 BIRD 会自动认为这是一个 iBGP,否则是 eBGP
  12.             # i 表示 internal(内部),e 表示 external(外部)
  13.         neighbor 10.226.11.22 as 64512;
  14.         # source: 定义本地地址作为BGP会话的源地址。Default:邻居所连接接口的本端地址。
  15.         source address 10.226.11.27;
  16.         #multihop;
  17.         # password: 如果和对端约定了密码,在这里配置约定好的密码,否则不用写
  18.         password "passwd";
  19.         # direct: eBGP 默认启用可以不写
  20.         # direct: iBGP 如果是直接连接的可以写这个来避免 multihop 被指定
  21.         # 指定邻居为直连。邻居的IP地址必须在直接可达的IP范围内(即与路由器的接口有关联),
  22.         # 否则BGP会话不会启动,而是等待这样的接口出现。另一种选择是多跳选项。默认值:使能eBGP。
  23.         direct;
  24.         #export all; # 将所有 bird 路由表中的路由都通过bgp发布给peer
  25.         #export: 控制哪些路由可以发布给bgp peer
  26.         export filter {
  27.                 # net 匹配的则被filter放行
  28.                 if net ~ [ 192.168.227.0/24 ] then accept;
  29.                 # 其他的路由全部被filter挡住,从而不会被发布给bgp peer
  30.                 reject;
  31.         };
  32.         # import 控制对方bgp peer 发个我的路由,哪些会被我接收
  33.         import all;
  34. }
复制代码
控制对bgp peer 10.226.11.21 的路由发布
  1. protocol bgp peer_10_226_11_21 {
  2.         debug { states };
  3.         local as 64512;
  4.         neighbor 10.226.11.21 as 64512;
  5.         source address 10.226.11.27;
  6.         #multihop;
  7.         password "passwd";
  8.         direct;
  9.         export filter export_filter_v4; # 使用过滤表 export_filter_v4 控制哪些路由可以发布给bgp peer
  10.         import all; # 从 direct, device, static, kernel 等所有protocol的路由都导入bird 的 bgp 路由
  11. }
复制代码
示意图

宿主10.226.11.22上的/etc/bird/bird.conf配置

  1. # This is a minimal configuration file, which allows the bird daemon to start# but will not cause anything else to happen.## Please refer to the documentation in the bird-doc package or BIRD User's# Guide on http://bird.network.cz/ for more information on configuring BIRD and# adding routing protocols.#log syslog all;log "/var/log/bird.log" all;
  2. # Change this into your BIRD router ID. It's a world-wide unique identification# of your router, usually one of router's IPv4 addresses.router id 10.226.11.22;filter export_filter_v4 {        if net ~ [ 192.168.222.0/24 ] then accept; # 如果为默认路由则拒绝        reject;        #accept; # 接收全部其他路由};# The Kernel protocol is not a real routing protocol. Instead of communicating# with other routers in the network, it performs synchronization of BIRD's# routing tables with the OS kernel.protocol kernel {        debug { states };        scan time 10;        learn;        persist;        import none;  # kernel to bird map        export all;   # Actually insert routes into the kernel routing table}# The Device protocol is not a real routing protocol. It doesn't generate any# routes and it only serves as a module for getting information about network# interfaces from the kernel.protocol direct {        interface "veth2";}protocol device {}protocol bgp peer_10_226_11_21 {        debug { states };        local as 64512;        neighbor 10.226.11.21 as 64512;        source address 10.226.11.22;        #multihop;  # multihop 用于source address 不是直连的情况,比如利用loopback地点互联        direct;        password "passwd";        export all;        import all;}protocol bgp peer_10_226_11_27 {        debug { states };        local as 64512;        neighbor 10.226.11.27 as 64512;        source address 10.226.11.22;        direct;        password "passwd";        export all;        import all;}
复制代码
宿主10.226.11.21上的/etc/bird/bird.conf配置

  1. # This is a minimal configuration file, which allows the bird daemon to start# but will not cause anything else to happen.## Please refer to the documentation in the bird-doc package or BIRD User's# Guide on http://bird.network.cz/ for more information on configuring BIRD and# adding routing protocols.log "/var/log/bird.log" all;
  2. # Change this into your BIRD router ID. It's a world-wide unique identification# of your router, usually one of router's IPv4 addresses.router id 10.226.11.21;filter filter_export_v4 {        if net ~ [ 192.168.21.0/24 ] then accept;        if net ~ [ 192.168.221.0/24 ] then accept;        reject;}# The Kernel protocol is not a real routing protocol. Instead of communicating# with other routers in the network, it performs synchronization of BIRD's# routing tables with the OS kernel.protocol kernel {        learn;        persist;        scan time 10;        import none;        export all;   # Actually insert routes into the kernel routing table}# The Device protocol is not a real routing protocol. It doesn't generate any# routes and it only serves as a module for getting information about network# interfaces from the kernel.protocol device {}protocol direct {        interface "veth2";}protocol bgp peer_10_226_11_22 {        debug { states };        local as 64512;        neighbor 10.226.11.22 as 64512;        source address 10.226.11.21;        #multihop;        direct;        password "passwd";        export all;        import all;}protocol bgp peer_10_226_11_27 {        debug { states };        local as 64512;        neighbor 10.226.11.27 as 64512;        source address 10.226.11.21;        direct;        password "passwd";        export all;        import all;}
复制代码
查看状态

登录宿主10.226.11.27查看其干系网络的状态


  • 查看 bgp peer 邻居状态
Established表现与bgp peer邻接关系已经建立,而且已经相互完成了路由学习
  1. root@10_226_11_27:/work/code# birdc  show  protocol
  2. BIRD 1.5.0 ready.
  3. name     proto    table    state  since       info
  4. kernel1  Kernel   master   up     16:06:17
  5. direct1  Direct   master   up     16:06:17
  6. device1  Device   master   up     16:06:17
  7. peer_10_226_11_21 BGP      master   up     16:06:21    Established
  8. peer_10_226_11_22 BGP      master   up     16:06:22    Established
复制代码


  • bird 路由表
  1. root@10_226_11_27:/work/code# birdc show route
  2. BIRD 1.5.0 ready.
  3. # 从 protocol direct 导入的路由
  4. 192.168.227.0/24   dev veth2 [direct1 16:06:17] * (240)
  5. # 从 bgp peer_10_226_11_21 学习的路由
  6. 192.168.221.0/24   via 10.226.11.21 on eth0 [peer_10_226_11_21 16:06:21] * (100) [i]
  7. # 从 bgp peer_10_226_11_22 学习的路由
  8. 192.168.222.0/24   via 10.226.11.22 on eth0 [peer_10_226_11_22 16:18:14] * (100) [i]
复制代码


  • bird路由表的详细信息
  1. root@10_226_11_27:/work/code# birdc show route all
  2. BIRD 1.5.0 ready.
  3. 192.168.227.0/24   dev veth2 [direct1 16:06:17] * (240)
  4.         Type: device unicast univ
  5. 192.168.221.0/24   via 10.226.11.21 on eth0 [peer_10_226_11_21 16:06:21] * (100) [i]
  6.         Type: BGP unicast univ
  7.         BGP.origin: IGP
  8.         BGP.as_path:
  9.         BGP.next_hop: 10.226.11.21
  10.         BGP.local_pref: 100
  11. 192.168.222.0/24   via 10.226.11.22 on eth0 [peer_10_226_11_22 16:18:14] * (100) [i]
  12.         Type: BGP unicast univ
  13.         BGP.origin: IGP
  14.         BGP.as_path:
  15.         BGP.next_hop: 10.226.11.22
  16.         BGP.local_pref: 100
复制代码


  • kernel 路由表
  1. root@10_226_11_27:/work/code# ip route show
  2. default via 10.226.8.1 dev eth0
  3. # 宿主 eth0接口 的直连路由
  4. 10.226.8.0/22 dev eth0  proto kernel  scope link  src 10.226.11.27
  5. # 从bird 学习来的路由
  6. 192.168.221.0/24 via 10.226.11.21 dev eth0  proto bird
  7. 192.168.222.0/24 via 10.226.11.22 dev eth0  proto bird
  8. # 宿主 veth2接口 直接的路由(模拟的容器网段)
  9. 192.168.227.0/24 dev veth2  proto kernel  scope link  src 192.168.227.101
复制代码
网络测试验证



  • 从容器192.168.227.101 ping 容器192.168.222.101
  1. root@10_226_11_27:/work/code# ip netns exec netns1 ping 192.168.222.101 -c 2
  2. PING 192.168.222.101 (192.168.222.101) 56(84) bytes of data.
  3. 64 bytes from 192.168.222.101: icmp_seq=1 ttl=63 time=0.925 ms
  4. 64 bytes from 192.168.222.101: icmp_seq=2 ttl=63 time=0.252 ms
  5. --- 192.168.222.101 ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
  7. rtt min/avg/max/mdev = 0.252/0.588/0.925/0.337 ms
复制代码


  • 从容器192.168.227.101 ping 容器192.168.221.101
  1. root@10_226_11_27:/work/code# ip netns exec netns1 ping 192.168.221.101 -c 2
  2. PING 192.168.221.101 (192.168.221.101) 56(84) bytes of data.
  3. 64 bytes from 192.168.221.101: icmp_seq=1 ttl=63 time=0.221 ms
  4. 64 bytes from 192.168.221.101: icmp_seq=2 ttl=63 time=0.251 ms
  5. --- 192.168.221.101 ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 1011ms
  7. rtt min/avg/max/mdev = 0.221/0.236/0.251/0.015 ms
复制代码


  • 宿主10.226.11.27 ping 192.168.221.101
  1. root@10_226_11_27:/work/code# ping 192.168.221.101 -c 2
  2. PING 192.168.221.101 (192.168.221.101) 56(84) bytes of data.
  3. 64 bytes from 192.168.221.101: icmp_seq=1 ttl=64 time=0.886 ms
  4. 64 bytes from 192.168.221.101: icmp_seq=2 ttl=64 time=0.268 ms
  5. --- 192.168.221.101 ping statistics ---
  6. 2 packets transmitted, 2 received, 0% packet loss, time 1015ms
  7. rtt min/avg/max/mdev = 0.268/0.577/0.886/0.309 ms
  8. root@10_226_11_27:/work/code# traceroute -d 192.168.221.101
  9. traceroute to 192.168.221.101 (192.168.221.101), 30 hops max, 60 byte packets
  10. 1  192.168.221.101 (192.168.221.101)  0.261 ms  0.231 ms  0.225 ms
复制代码


  • 容器192.168.227.102的路由
  1. root@10_226_11_27:/work/code# ip netns exec netns1 ip route
  2. default via 192.168.227.101 dev veth1
  3. 192.168.227.0/24 dev veth1  proto kernel  scope link  src 192.168.227.102
  4. root@10_226_11_27:/work/code# ip netns exec netns1 ip route get 192.168.221.101
  5. 192.168.221.101 via 192.168.227.101 dev veth1  src 192.168.227.102
  6.     cache
复制代码
实验结论



  • 实现了三台宿主的bird的bgp full mesh 模式
  • 三台宿主通过bgp相互完成了路由学习
  • 通过实验我们对calico中的bird程序有了更深入的认知
参考文档

bird官网
BGP_example_1
Intro-to-BGP-with-BIRD
bird-bgp-kickstart
https://wiki.skywolf.cloud/quickstart/player.html

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表