使用CentOS7.4搭建bgp网络实验Quagga

Quagga是一个开源路由软件套件。在这个教程中,我将会重点讲讲如何把一个Linux系统变成一个BGP路由器,还是使用Quagga,演示如何建立BGP与其它BGP路由器对等。

在我们进入细节之前,一些BGP的背景知识还是必要的。边界网关协议(即BGP)是互联网的域间路由协议的实际标准。在BGP术语中,全球互联网是由成千上万相关联的自治系统(AS)组成,其中每一个AS代表每一个特定运营商提供的一个网络管理域(据说,美国前总统乔治.布什都有自己的 AS 编号)。

为了使其网络在全球范围内路由可达,每一个AS需要知道如何在英特网中到达其它的AS。这时候就需要BGP出来扮演这个角色了。BGP是一个AS去与相邻的AS交换路由信息的语言。这些路由信息通常被称为BGP线路或者BGP前缀。包括AS号(ASN;全球唯一号码)以及相关的IP地址块。一旦所有的BGP线路被当地的BGP路由表学习和记录,每一个AS将会知道如何到达互联网的任何公网IP。

在不同域(AS)之间路由的能力是BGP被称为外部网关协议(EGP)或者域间协议的主要原因。就如一些路由协议,例如OSPF、IS-IS、RIP和EIGRP都是内部网关协议(IGPs)或者域内路由协议,用于处理一个域内的路由。

我们假设运营商A想要建立一个BGP来与运营商B对等交换路由。它们的AS号和IP地址空间的细节如下所示:

  • 运营商 A: ASN (100), IP地址空间 (100.100.1.0/24), 分配给BGP路由器ens33网卡的IP地址(10.45.11.118)
  • 运营商 B: ASN (200), IP地址空间 (200.200.1.0/24), 分配给BGP路由器ens33网卡的IP地址(10.45.11.119)

路由器A和路由器B使用10.45.10.0/23子网来连接到对方。从理论上来说,任何子网从运营商那里都是可达的、可互连的。在真实场景中,建议使用掩码为30位的公网IP地址空间来实现运营商A和运营商B之间的连通。

1、在centos7中安装Quagga

yum install quagga

请确保配置了本地yum源,在centos的安装镜像文件中,自带了该软件包。

2、放开selinux限制

如果你正在使用的是CentOS7系统,你需要应用一下策略来设置SELinux。否则,SElinux将会阻止Zebra守护进程写入它的配置目录。如果你正在使用的是CentOS6,你可以跳过这一步。

setsebool -P zebra_write_config 1 

或者可以直接关闭selinux:

[root@gfs02 ~]# vi /etc/selinux/config

修改配置项为如下值:

SELINUX=disabled

重启下主机。

Quagga软件套件包含几个守护进程,这些进程可以协同工作。关于BGP路由,我们将把重点放在建立以下2个守护进程。

  • Zebra:一个核心守护进程用于内核接口和静态路由.
  • BGPd:一个BGP守护进程.

3、zebra配置启动

cp /usr/share/doc/quagga-0.99.22.4/zebra.conf.sample /etc/quagga/zebra.conf

启动进程

systemctl start zebra

systemctl enable zebra

4、配置quagga日志

Quagga提供了一个叫做vtysh特有的命令行工具,你可以输入与路由器厂商(例如Cisco和Juniper)兼容和支持的命令。我们将使用vtysh shell来配置BGP路由在教程的其余部分。

启动vtysh shell 命令,输入:

[root@gfs01 network-scripts]# vtysh
提示将被改成该主机名,这表明你是在vtysh shell中。

gfs01#

现在我们将使用以下命令来为Zebra配置日志文件:

gfs01# configure terminal
gfs01(config)# log file /var/log/quagga/quagga.log
gfs01(config)# exit

永久保存Zebra配置:

gfs01# write

在路由器B操作同样的步骤。

5、配置对等的ip地址

下一步,我们将在可用的接口上配置对等的IP地址。

#显示接口信息

gfs01# show interface
Interface ens33 is up, line protocol detection is disabled
  index 2 metric 1 mtu 1500 
  flags: <UP,BROADCAST,RUNNING,MULTICAST>
  HWaddr: 00:0c:29:f5:fe:fd
  inet 10.45.11.118/23 broadcast 10.45.11.255
  inet6 fe80::34a7:56c3:71b7:3a0/64
Interface lo is up, line protocol detection is disabled
  index 1 metric 1 mtu 65536 
  flags: <UP,LOOPBACK,RUNNING>
  inet 100.100.1.1/24 broadcast 100.100.1.255 lo:1
  inet 127.0.0.1/8
  inet6 ::1/128

配置ens33接口参数:

gfs01# configure terminal
gfs01(config)# interface ens33
gfs01(config-if)# ip address 10.45.11.118/23
gfs01(config-if)# description "to gfs02"
gfs01(config-if)# no shutdown
gfs01(config-if)# exit

配置lo:1接口参数:

gfs01# conf t 
gfs01(config)# interface lo:1
gfs01(config-if)# ip address 100.100.1.1/24
gfs01(config-if)# description "test ip from provider A network"
gfs01(config-if)# no shutdown
gfs01(config-if)# exit

确认配置:

gfs01# show interface
Interface ens33 is up, line protocol detection is disabled
  index 2 metric 1 mtu 1500 
  flags: <UP,BROADCAST,RUNNING,MULTICAST>
  HWaddr: 00:0c:29:f5:fe:fd
  inet 10.45.11.118/23 broadcast 10.45.11.255
  inet6 fe80::34a7:56c3:71b7:3a0/64
Interface lo is up, line protocol detection is disabled
  index 1 metric 1 mtu 65536 
  flags: <UP,LOOPBACK,RUNNING>
  inet 100.100.1.1/24 broadcast 100.100.1.255 lo:1
  inet 127.0.0.1/8
  inet6 ::1/128

如果一切看起来正常,别忘记保存配置。

gfs01# write

同样地,在路由器B重复一次配置。

注意:配置网卡接口ip这个地方我这边是在主机层面已经配置好了。

使用ens33接口作为两个centos虚机直连接口使用。使用lo:1接口作为两个 运营商后面的网络。

添加一个lo接口:

[root@gfs02 quagga-0.99.22.4]# cd /etc/sysconfig/network-scripts/

[root@gfs02 network-scripts]# cp ifcfg-lo ifcfg-lo:1

[root@gfs02 network-scripts]# vi ifcfg-lo:1
DEVICE=lo:1
IPADDR=200.200.1.1
NETMASK=255.255.255.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
ONBOOT=yes
NAME=loopback1

#重启网络

[root@gfs02 network-scripts]# systemctl restart network

#检查

[root@gfs02 network-scripts]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.45.11.119  netmask 255.255.254.0  broadcast 10.45.11.255
        inet6 fe80::11aa:9ac6:ff72:adb0  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:a1:11:5e  txqueuelen 1000  (Ethernet)
        RX packets 746441  bytes 404706532 (385.9 MiB)
        RX errors 0  dropped 34  overruns 0  frame 0
        TX packets 87385  bytes 7583699 (7.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 95719  bytes 7866600 (7.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 95719  bytes 7866600 (7.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:1: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 200.200.1.1  netmask 255.255.255.0
        loop  txqueuelen 1000  (Local Loopback)
 

在我们继续下一步之前,确认下彼此的IP是可以ping通的。

gfs01# ping 10.45.11.119
PING 10.45.11.119 (10.45.11.119) 56(84) bytes of data.
64 bytes from 10.45.11.119: icmp_seq=1 ttl=64 time=0.420 ms
64 bytes from 10.45.11.119: icmp_seq=2 ttl=64 time=0.274 ms

下一步,我们将继续配置BGP对等和前缀设置。

6、配置bgp对等

Quagga守护进程负责BGP的服务叫bgpd。首先我们来准备它的配置文件。

cp /usr/share/doc/quagga-0.99.22.4/bgpd.conf.sample /etc/quagga/bgpd.conf

启动bgpd进程:

[root@gfs02 quagga-0.99.22.4]# systemctl start bgpd
[root@gfs02 quagga-0.99.22.4]# systemctl enable bgpd

确保没有报错。可以通过下面命令检查状态。

systemctl status bgpd

现在,让我们来进入Quagga 的shell。

[root@gfs02 quagga-0.99.22.4]# vtysh

第一步,我们要确认当前没有已经配置的BGP会话。在一些版本,我们可能会发现一个AS号为7675的BGP会话。由于我们不需要这个会话,所以把它移除。

gfs02# show running-config
Building configuration...

Current configuration:
!
hostname gfs02
log file /var/log/quagga/quagga.log
hostname bgpd
log stdout
!
password zebra
!
interface ens33
 ipv6 nd suppress-ra
!
interface lo
!
router bgp 7675
 bgp router-id 200.200.1.1

!
line vty
!
end

我们将移除一些预先配置好的BGP会话,并建立我们所需的会话取而代之。

gfs02# conf t 
gfs02(config)# no router bgp 7675
gfs02(config)# router bgp 200
gfs02(config-router)# no auto-summary  
gfs02(config-router)# no synchronization  
gfs02(config-router)# neighbor  10.45.11.118 remote-as  100
gfs02(config-router)# neighbor  10.45.11.118 description  "provider A"
gfs02(config-router)# exit
gfs02(config)# exit
gfs02# write

路由器B将用同样的方式来进行配置,以下配置提供作为参考。

gfs01# configure terminal
gfs01(config)# no router bgp 7675
gfs01(config)# router bgp 100
gfs01(config-router)# no auto-summary
gfs01(config-router)# no synchronization
gfs01(config-router)# neighbor  10.45.11.119 remote-as 200
gfs01(config-router)# neighbor  10.45.11.119 description "provider B"
gfs01(config-router)# exit
gfs01(config)# exit
gfs01# write

当相关的路由器都被配置好,两台路由器之间的对等将被建立。现在让我们通过运行下面的命令来确认:

gfs02# show ip bgp summary
BGP router identifier 200.200.1.1, local AS number 200
RIB entries 0, using 0 bytes of memory
Peers 1, using 4560 bytes of memory

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
10.45.11.118    4   100       2       3        0    0    0 00:00:44        0

Total number of neighbors 1

从输出中,我们可以看到"State/PfxRcd"部分。如果对等关闭,输出将会显示"Idle"或者"Active'。请记住,单词'Active'这个词在路由器中总是不好的意思。它意味着路由器正在积极地寻找邻居、前缀或者路由。当对等是up状态,"State/PfxRcd"下的输出状态将会从特殊邻居接收到前缀号。

在这个例子的输出中,BGP对等只是在AS100和AS200之间呈up状态。因此没有前缀被更改,所以最右边列的数值是0。

7、配置前缀通告

正如一开始提到,AS 100将以100.100.1.0/24作为通告,在我们的例子中AS 200将同样以200.200.1.0/24作为通告。这些前缀需要被添加到BGP配置如下。

在路由器-A中:

gfs01# conf t 
gfs01(config)# router bgp 100
gfs01(config-router)# network 100.100.1.0/24 
gfs01(config-router)# exit
gfs01(config)# exit
gfs01# write

在路由器-B中:

gfs02# conf t 
gfs02(config)# router bgp 200
gfs02(config-router)# network 200.200.1.0/24
gfs02(config-router)# exit
gfs02(config)# exit
gfs02# write

在这一点上,两个路由器会根据需要开始通告前缀。

8、测试前缀通告

首先,让我们来确认前缀的数量是否被改变了。

gfs02# show ip bgp summary
BGP router identifier 200.200.1.1, local AS number 200
RIB entries 3, using 336 bytes of memory
Peers 1, using 4560 bytes of memory

Neighbor        V    AS MsgRcvd MsgSent   TblVer  InQ OutQ Up/Down  State/PfxRcd
10.45.11.118    4   100      12      13        0    0    0 00:09:32        1

Total number of neighbors 1

为了查看所接收的更多前缀细节,我们可以使用以下命令,这个命令用于显示邻居

10.45.11.118所接收到的前缀总数。

gfs02# show ip bgp neighbors 10.45.11.118 advertised-routes  
BGP table version is 0, local router ID is 200.200.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, R Removed
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 200.200.1.0      10.45.11.119             0         32768 i

Total number of prefixes 1

查看哪一个前缀是我们从邻居接收到的:

gfs02# show ip bgp neighbors 10.45.11.118 routes
BGP table version is 0, local router ID is 200.200.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, R Removed
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.100.1.0/24   10.45.11.118             0             0 100 i

Total number of prefixes 1

我们也可以查看所有的BGP路由器:
 

gfs02# show ip bgp
BGP table version is 0, local router ID is 200.200.1.1
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, R Removed
Origin codes: i - IGP, e - EGP, ? - incomplete

   Network          Next Hop            Metric LocPrf Weight Path
*> 100.100.1.0/24   10.45.11.118             0             0 100 i
*> 200.200.1.0      0.0.0.0                  0         32768 i

Total number of prefixes 2

以上的命令都可以被用于检查哪个路由器通过BGP在路由器表中被学习到。

gfs02# show ip route
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, A - Babel,
       > - selected route, * - FIB route

K>* 0.0.0.0/0 via 10.45.11.254, ens33
C>* 10.45.10.0/23 is directly connected, ens33
B>* 100.100.1.0/24 [20/0] via 10.45.11.118, ens33, 00:04:50
C>* 127.0.0.0/8 is directly connected, lo
C>* 200.200.1.0/24 is directly connected, lo
gfs02# show ip route bgp
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, A - Babel,
       > - selected route, * - FIB route

B>* 100.100.1.0/24 [20/0] via 10.45.11.118, ens33, 00:06:08

gfs02# show ip route bgp
Codes: K - kernel route, C - connected, S - static, R - RIP,
       O - OSPF, I - IS-IS, B - BGP, A - Babel,
       > - selected route, * - FIB route

B>* 100.100.1.0/24 [20/0] via 10.45.11.118, ens33, 03:38:21
gfs02# 

BGP学习到的路由也将会在Linux路由表中出现。这个也是我们模拟在linux跑起来bgp后,无需添加静态路由的情况下,位于gfs01上的lo:1接口100.100.1.1/24和位于gfs02上的lo:1接口200.200.1.1/24直接能ping通的前提。

[root@gfs02 ~]# ip route
default via 10.45.11.254 dev ens33 proto static metric 100 
10.45.10.0/23 dev ens33 proto kernel scope link src 10.45.11.119 metric 100 
100.100.1.0/24 via 10.45.11.118 dev ens33 proto zebra 
[root@gfs02 ~]# 

[root@gfs02 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.45.11.254    0.0.0.0         UG    100    0        0 ens33
10.45.10.0      0.0.0.0         255.255.254.0   U     100    0        0 ens33
100.100.1.0     10.45.11.118    255.255.255.0   UG    0      0        0 ens33

最后,我们将使用ping命令来测试连通。结果将成功ping通。
[root@gfs02 ~]# ping 100.100.1.1
PING 100.100.1.1 (100.100.1.1) 56(84) bytes of data.
64 bytes from 100.100.1.1: icmp_seq=1 ttl=64 time=0.394 ms
64 bytes from 100.100.1.1: icmp_seq=2 ttl=64 time=0.378 ms
64 bytes from 100.100.1.1: icmp_seq=3 ttl=64 time=0.538 ms
64 bytes from 100.100.1.1: icmp_seq=4 ttl=64 time=0.302 ms
^C
--- 100.100.1.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 0.302/0.403/0.538/0.085 ms

总而言之,本教程将重点放在如何在CentOS系统中运行一个基本的BGP路由器。这个教程让你开始学习BGP的配置,一些更高级的设置例如设置过滤器、BGP属性调整、本地优先级和预先路径准备等,我将会在后续的教程中覆盖这些主题。

希望这篇教程能给大家一些帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值