环境:centos 7.6.1810
版本:FRR 7.3
服务器: 1台
FRR的vrf功能支持同一个进程在不同ns内创建bgp对等体。首先需要配置zebra支持backend方式为ns
[root@k8s-master ~]# cat /usr/local/frr/etc/daemons |grep zebra
# The watchfrr and zebra daemons are always started.
zebra_options=" -A 127.0.0.1 -s 90000000 -n"
我们在一台服务器上创建ns1和ns2,并创建veth pair分别绑定到ns1和ns2,使得两者的网络直连,然后使用vrf功能在ns1和ns2上创建对等体关系,并分别向对方通告虚拟路由。
ip netns add ns1
ip netns add ns2
ip link add veth_ns2 type veth peer name veth_ns1
#去掉ARP限制
echo 1 > /proc/sys/net/ipv4/conf/veth_ns1/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns2/accept_local
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns2/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns1/rp_filter
ip link set veth_ns1 netns ns1
ip netns exec ns1 ip link set veth_ns1 up
ip netns exec ns1 ip addr add 10.10.1.2/24 dev veth_ns1
ip link set veth_ns2 netns ns2
ip netns exec ns2 ip link set veth_ns2 up
ip netns exec ns2 ip addr add 10.10.1.4/24 dev veth_ns2
测试网络连通性:
[root ~]# ip netns exec ns1 ping 10.10.1.4
PING 10.10.1.4 (10.10.1.4) 56(84) bytes of data.
64 bytes from 10.10.1.4: icmp_seq=1 ttl=64 time=0.275 ms
64 bytes from 10.10.1.4: icmp_seq=2 ttl=64 time=0.091 ms
64 bytes from 10.10.1.4: icmp_seq=3 ttl=64 time=0.057 ms
64 bytes from 10.10.1.4: icmp_seq=4 ttl=64 time=0.093 ms
[root ]# cat /usr/local/frr/etc/bgpd.conf
hostname bgpd
password zebra
router bgp 100 vrf ns1
bgp router-id 10.10.1.2
network 100.10.1.0/24
neighbor 10.10.1.4 remote-as 200
neighbor 10.10.1.4 ebgp-multihop
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns2
bgp router-id 10.10.1.4
neighbor 10.10.1.2 remote-as 100
neighbor 10.10.1.2 ebgp-multihop 255
network 200.10.1.0/24
address-family ipv4 unicast
exit-address-family
debug bgp neighbor-events
debug bgp updates
debug bgp keepalives
debug bgp zebra
log file /usr/local/frr/var/log/bgpd.log
vtysh查看当前状态show bgp vrf ns1 neighbors
[root ]# /usr/local/frr/bin/vtysh
Hello, this is FRRouting (version 7.3).
Copyright 1996-2005 Kunihiro Ishiguro, et al.
AS100# show bgp vrf ns1 neighbors
BGP neighbor is 10.10.1.4, remote AS 200, local AS 100, external link
Hostname: bgpd
BGP version 4, remote router ID 10.10.1.4, local router ID 10.10.1.2
BGP state = Established, up for 00:05:17
......
vtysh查看当前状态show bgp vrf ns1 ipv4,可以看到AS200通告的路由200.10.1.0/24;
同样,show bgp vrf ns2 ipv4,可以看到AS100通告的路由200.10.1.0/24;
上个实例已经把ns1和ns2组成EBGP对等体网络,在这基础上,我们把ns2、ns3和ns4组成一个IBGP对等体网络。其中,ns1属于AS100,ns2、ns3和ns4属于AS200。AS100向AS200通告路由4.0.1.0/24,这部分路由会从ns2通过IBGP对等体关系通告给ns3,但是由于水平分割原则,ns4将不会收到4.0.1.0/24的路由通告。
#还原环境
ip netns del ns1
ip netns del ns2
#连通ns1和ns2
ip netns add ns1
ip netns add ns2
ip link add veth_ns2 type veth peer name veth_ns1
#去掉ARP限制
echo 1 > /proc/sys/net/ipv4/conf/veth_ns1/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns2/accept_local
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns2/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns1/rp_filter
ip link set veth_ns1 netns ns1
ip netns exec ns1 ip link set veth_ns1 up
ip netns exec ns1 ip addr add 10.10.1.2/24 dev veth_ns1
ip link set veth_ns2 netns ns2
ip netns exec ns2 ip link set veth_ns2 up
ip netns exec ns2 ip addr add 10.10.1.4/24 dev veth_ns2
#连通ns2和ns3
ip netns add ns3
ip link add veth_ns3_2 type veth peer name veth_ns2_3
echo 1 > /proc/sys/net/ipv4/conf/veth_ns3_2/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns2_3/accept_local
echo 0 > /proc/sys/net/ipv4/conf/veth_ns3_2/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns2_3/rp_filter
ip link set veth_ns2_3 netns ns2
ip netns exec ns2 ip link set veth_ns2_3 up
ip netns exec ns2 ip addr add 20.10.1.4/24 dev veth_ns2_3
ip link set veth_ns3_2 netns ns3
ip netns exec ns3 ip link set veth_ns3_2 up
ip netns exec ns3 ip addr add 20.10.1.6/24 dev veth_ns3_2
#连通ns3和ns4
ip netns add ns4
ip link add veth_ns4_3 type veth peer name veth_ns3_4
echo 1 > /proc/sys/net/ipv4/conf/veth_ns4_3/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns3_4/accept_local
echo 0 > /proc/sys/net/ipv4/conf/veth_ns4_3/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns3_4/rp_filter
ip link set veth_ns3_4 netns ns3
ip netns exec ns3 ip link set veth_ns3_4 up
ip netns exec ns3 ip addr add 30.10.1.6/24 dev veth_ns3_4
ip link set veth_ns4_3 netns ns4
ip netns exec ns4 ip link set veth_ns4_3 up
ip netns exec ns4 ip addr add 30.10.1.8/24 dev veth_ns4_3
网络较为复杂,可以先用ping命令测试下网络是否可达,如果有问题要先检查网络设置是否生效
ip netns exec ns1 ping 10.10.1.4
ip netns exec ns2 ping 20.10.1.6
ip netns exec ns3 ping 30.10.1.8
[root ~]# cat /usr/local/frr/etc/bgpd.conf
hostname bgpd
password zebra
router bgp 100 vrf ns1
bgp router-id 10.10.1.2
neighbor 10.10.1.4 remote-as 200
neighbor 10.10.1.4
network 4.0.1.0/24
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns2
bgp router-id 10.10.1.4
neighbor 10.10.1.2 remote-as 100
neighbor 20.10.1.6 remote-as 200
neighbor 20.10.1.6 update-source 20.10.1.4
#将从EBGP收到的路由宣告给对等体时,将下一跳地址改为自身
neighbor 20.10.1.6 next-hop-self
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns3
bgp router-id 20.10.1.6
neighbor 20.10.1.4 remote-as 200
neighbor 30.10.1.8 remote-as 200
neighbor 30.10.1.8 update-source 30.10.1.6
#所有收到的路由宣告给对等体时,将下一跳地址改为自身
neighbor 30.10.1.8 next-hop-self all
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns4
bgp router-id 30.10.1.8
neighbor 30.10.1.6 remote-as 200
neighbor 30.10.1.6
address-family ipv4 unicast
exit-address-family
debug bgp neighbor-events
debug bgp updates
debug bgp keepalives
debug bgp zebra
log file /usr/local/frr/var/log/bgpd.log
配置完成后启动FRR
systemctl restart frr
老规矩通过/usr/local/frr/bin/vtysh查看邻居关系的当前状态。
当所有邻居关系都是建立成功后,通过命令查看路由的通告情况show bgp vrf NS ipv4
可以看到,ns2和ns3都收到了路由通告,只有ns4没有,符合IBGP水平分割原则。
那么,如何让ns4也能够收到路由通告?其中一种方法就是设置路由反射器,将ns3设置为RR,ns4设置为其client。
参考IBGP配置实例
网络设置不变,参考IBGP配置实例
BGP配置主要更改在配置ns3作为RR,并设置其client为ns4
[root@k8s-master ~]# cat /usr/local/frr/etc/bgpd.conf
hostname bgpd
password zebra
router bgp 100 vrf ns1
bgp router-id 10.10.1.2
neighbor 10.10.1.4 remote-as 200
neighbor 10.10.1.4
network 4.0.1.0/24
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns2
bgp router-id 10.10.1.4
neighbor 10.10.1.2 remote-as 100
neighbor 20.10.1.6 remote-as 200
neighbor 20.10.1.6 update-source 20.10.1.4
neighbor 20.10.1.6 next-hop-self
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns3
bgp router-id 20.10.1.6
bgp cluster-id 6.6.6.6
neighbor 20.10.1.4 remote-as 200
neighbor 30.10.1.8 remote-as 200
neighbor 30.10.1.8 update-source 30.10.1.6
neighbor 30.10.1.8 next-hop-self all
#指定路由反射器的client
neighbor 30.10.1.8 route-reflector-client
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns4
bgp router-id 30.10.1.8
neighbor 30.10.1.6 remote-as 200
neighbor 30.10.1.6
address-family ipv4 unicast
exit-address-family
debug bgp neighbor-events
debug bgp updates
debug bgp keepalives
debug bgp zebra
log file /usr/local/frr/var/log/bgpd.log
网络较为复杂,可以先用ping命令测试下网络是否可达,如果有问题要先检查网络设置是否生效
ip netns exec ns1 ping 10.10.1.4
ip netns exec ns2 ping 20.10.1.6
ip netns exec ns3 ping 30.10.1.8
配置完成后重启FRR
systemctl restart frr
通过/usr/local/frr/bin/vtysh查看路由信息。
AS100# show bgp vrf ns4 ipv4 4.0.1.0
BGP routing table entry for 4.0.1.0/24
Paths: (1 available, best #1, vrf ns4)
Not advertised to any peer
100
30.10.1.6 from 30.10.1.6 (10.10.1.4)
Origin IGP, metric 0, localpref 100, valid, internal, best (First path received)
Originator: 10.10.1.4, Cluster list: 6.6.6.6
Last update: Tue Jun 16 17:05:04 2020
可以看到,Originator和Cluster list这两个路径属性也正确传递。
在IBGP配置实例的基础上,新增ns5作为AS500与ns4建立EBGP对等体关系,同时会向ns4通告同一个路由4.0.1.0/24。那么ns2和ns4都会向ns3通告4.0.1.0/24的路由,看看ns3会优选出哪个路由。
可以看到默认情况下根据BGP路由优选原则,前面10条原则一致,会在比较到第11条(优选route-id较小)的时候,选择ns2通告的路由。
这里我们测试加入路径属性Local_Preference,我们会通过在ns2和ns4上使用route-map路由策略为路由设置不同的Local_Preference值,看看ns3是否会根据Local_Preference来优选。Local_Preference的详细解释可以参考BGP路径属性Local-Preference
#还原环境
ip netns del ns1
ip netns del ns2
ip netns del ns3
ip netns del ns4
ip netns del ns5
#连通ns1和ns2
ip netns add ns1
ip netns add ns2
ip link add veth_ns2 type veth peer name veth_ns1
#去掉ARP限制
echo 1 > /proc/sys/net/ipv4/conf/veth_ns1/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns2/accept_local
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns2/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns1/rp_filter
ip link set veth_ns1 netns ns1
ip netns exec ns1 ip link set veth_ns1 up
ip netns exec ns1 ip addr add 10.10.1.2/24 dev veth_ns1
ip link set veth_ns2 netns ns2
ip netns exec ns2 ip link set veth_ns2 up
ip netns exec ns2 ip addr add 10.10.1.4/24 dev veth_ns2
#连通ns2和ns3
ip netns add ns3
ip link add veth_ns3_2 type veth peer name veth_ns2_3
echo 1 > /proc/sys/net/ipv4/conf/veth_ns3_2/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns2_3/accept_local
echo 0 > /proc/sys/net/ipv4/conf/veth_ns3_2/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns2_3/rp_filter
ip link set veth_ns2_3 netns ns2
ip netns exec ns2 ip link set veth_ns2_3 up
ip netns exec ns2 ip addr add 20.10.1.4/24 dev veth_ns2_3
ip link set veth_ns3_2 netns ns3
ip netns exec ns3 ip link set veth_ns3_2 up
ip netns exec ns3 ip addr add 20.10.1.6/24 dev veth_ns3_2
#连通ns3和ns4
ip netns add ns4
ip link add veth_ns4_3 type veth peer name veth_ns3_4
echo 1 > /proc/sys/net/ipv4/conf/veth_ns4_3/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns3_4/accept_local
echo 0 > /proc/sys/net/ipv4/conf/veth_ns4_3/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns3_4/rp_filter
ip link set veth_ns3_4 netns ns3
ip netns exec ns3 ip link set veth_ns3_4 up
ip netns exec ns3 ip addr add 30.10.1.6/24 dev veth_ns3_4
ip link set veth_ns4_3 netns ns4
ip netns exec ns4 ip link set veth_ns4_3 up
ip netns exec ns4 ip addr add 30.10.1.8/24 dev veth_ns4_3
#连通ns4和ns5
ip netns add ns5
ip link add veth_ns4_5 type veth peer name veth_ns5_4
echo 1 > /proc/sys/net/ipv4/conf/veth_ns4_5/accept_local
echo 1 > /proc/sys/net/ipv4/conf/veth_ns5_4/accept_local
echo 0 > /proc/sys/net/ipv4/conf/veth_ns4_5/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/veth_ns5_4/rp_filter
ip link set veth_ns4_5 netns ns4
ip netns exec ns4 ip link set veth_ns4_5 up
ip netns exec ns4 ip addr add 40.10.1.8/24 dev veth_ns4_5
ip link set veth_ns5_4 netns ns5
ip netns exec ns5 ip link set veth_ns5_4 up
ip netns exec ns5 ip addr add 40.10.1.10/24 dev veth_ns5_4
网络较为复杂,可以先用ping命令测试下网络是否可达,如果有问题要先检查网络设置是否生效
ip netns exec ns1 ping 10.10.1.4
ip netns exec ns2 ping 20.10.1.6
ip netns exec ns3 ping 30.10.1.8
ip netns exec ns4 ping 40.10.1.10
[root@k8s-master ~]# cat /usr/local/frr/etc/bgpd.conf
hostname bgpd
password zebra
bgp as-path access-list permit-100 permit _100$
route-map ns2_setlp permit 10
match as-path permit-100
set local-preference 80
route-map ns2_setlp permit 20
ip prefix-list 1 permit 40.10.1.0/24
route-map ns4_setlp permit 10
match ip address prefix-list 1
set local-preference 100
route-map ns4_setlp permit 20
router bgp 100 vrf ns1
bgp router-id 10.10.1.2
neighbor 10.10.1.4 remote-as 200
neighbor 10.10.1.4
network 4.0.1.0/24
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns2
bgp router-id 10.10.1.4
neighbor 10.10.1.2 remote-as 100
neighbor 20.10.1.6 remote-as 200
neighbor 20.10.1.6 update-source 20.10.1.4
neighbor 20.10.1.6 next-hop-self
neighbor 20.10.1.6 route-map ns2_setlp out
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns3
bgp router-id 20.10.1.6
bgp cluster-id 6.6.6.6
neighbor 20.10.1.4 remote-as 200
neighbor 30.10.1.8 remote-as 200
neighbor 30.10.1.8 update-source 30.10.1.6
neighbor 30.10.1.8 next-hop-self all
address-family ipv4 unicast
exit-address-family
router bgp 200 vrf ns4
bgp router-id 30.10.1.8
neighbor 30.10.1.6 remote-as 200
neighbor 40.10.1.10 remote-as 500
neighbor 40.10.1.10 update-source 40.10.1.8
neighbor 30.10.1.6 next-hop-self
neighbor 30.10.1.6 route-map ns4_setlp out
address-family ipv4 unicast
exit-address-family
router bgp 500 vrf ns5
bgp router-id 40.10.1.10
neighbor 40.10.1.8 remote-as 200
network 4.0.1.0/24
address-family ipv4 unicast
exit-address-family
debug bgp neighbor-events
debug bgp updates
debug bgp keepalives
debug bgp zebra
log file /usr/local/frr/var/log/bgpd.log
neighbor 20.10.1.6 route-map ns2_setlp out
为ns2往ns3的out方向设置路由策略ns2_setlp,而ns2_setlp的路由策略是把AS100通告的路由设置local-preference等于80;
neighbor 30.10.1.6 route-map ns4_setlp out
为ns4往ns3的out方向设置路由策略ns4_setlp,而ns4_setlp的路由策略是把IP符合40.10.1.0/24的对等体通告的路由设置local-preference等于100。