想玩 BGP 路由器麼?用 CentOS 做一個

 

之前的教程中,我對如何簡單地使用Quagga把CentOS系統變成一個不折不扣地OSPF路由器做了一些介紹。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.0.0/22), 分配給BGP路由器eth1網卡的IP地址(100.100.1.1)

  • 運營商 B: ASN (200), IP地址空間 (200.200.0.0/22), 分配給BGP路由器eth1網卡的IP地址(200.200.1.1)

路由器A和路由器B使用100.100.0.0/30子網來連接到對方。從理論上來說,任何子網從運營商那裏都是可達的、可互連的。在真實場景中,建議使用掩碼爲30位的公網IP地址空間來實現運營商A和運營商B之間的連通。

在 CentOS中安裝Quagga

如果Quagga還沒安裝好,我們可以使用yum來安裝Quagga。


 
  1. # yum install quagga

如果你正在使用的是CentOS7系統,你需要應用一下策略來設置SELinux。否則,SElinux將會阻止Zebra守護進程寫入它的配置目錄。如果你正在使用的是CentOS6,你可以跳過這一步。


 
  1. # setsebool -P zebra_write_config 1

Quagga軟件套件包含幾個守護進程,這些進程可以協同工作。關於BGP路由,我們將把重點放在建立以下2個守護進程。

  • Zebra:一個核心守護進程用於內核接口和靜態路由.
  • BGPd:一個BGP守護進程.

配置日誌記錄

在Quagga被安裝後,下一步就是配置Zebra來管理BGP路由器的網絡接口。我們通過創建一個Zebra配置文件和啓用日誌記錄來開始第一步。


 
  1. # cp /usr/share/doc/quagga-XXXXX/zebra.conf.sample /etc/quagga/zebra.conf

在CentOS6系統中:


 
  1. # service zebra start
  2. # chkconfig zebra on

在CentOS7系統中:


 
  1. # systemctl start zebra
  2. # systemctl enable zebra

Quagga提供了一個叫做vtysh特有的命令行工具,你可以輸入與路由器廠商(例如Cisco和Juniper)兼容和支持的命令。我們將使用vtysh shell來配置BGP路由在教程的其餘部分。

啓動vtysh shell 命令,輸入:


 
  1. # vtysh

提示將被改成該主機名,這表明你是在vtysh shell中。


 
  1. Router-A#

現在我們將使用以下命令來爲Zebra配置日誌文件:


 
  1. Router-A# configure terminal
  2. Router-A(config)# log file /var/log/quagga/quagga.log
  3. Router-A(config)# exit

永久保存Zebra配置:


 
  1. Router-A# write

在路由器B操作同樣的步驟。

配置對等的IP地址

下一步,我們將在可用的接口上配置對等的IP地址。


 
  1. Router-A# show interface #顯示接口信息


 
  1. Interface eth0 is up, line protocol detection is disabled
  2. . . . . .
  3. Interface eth1 is up, line protocol detection is disabled
  4. . . . . .

配置eth0接口的參數:


 
  1. site-A-RTR# configure terminal
  2. site-A-RTR(config)# interface eth0
  3. site-A-RTR(config-if)# ip address 100.100.0.1/30
  4. site-A-RTR(config-if)# description "to Router-B"
  5. site-A-RTR(config-if)# no shutdown
  6. site-A-RTR(config-if)# exit

繼續配置eth1接口的參數:


 
  1. site-A-RTR(config)# interface eth1
  2. site-A-RTR(config-if)# ip address 100.100.1.1/24
  3. site-A-RTR(config-if)# description "test ip from provider A network"
  4. site-A-RTR(config-if)# no shutdown
  5. site-A-RTR(config-if)# exit

現在確認配置:


 
  1. Router-A# show interface


 
  1. Interface eth0 is up, line protocol detection is disabled
  2. Description: "to Router-B"
  3. inet 100.100.0.1/30 broadcast 100.100.0.3
  4. Interface eth1 is up, line protocol detection is disabled
  5. Description: "test ip from provider A network"
  6. inet 100.100.1.1/24 broadcast 100.100.1.255


 
  1. Router-A# show interface description #顯示接口描述


 
  1. Interface Status Protocol Description
  2. eth0 up unknown "to Router-B"
  3. eth1 up unknown "test ip from provider A network"

如果一切看起來正常,別忘記保存配置。


 
  1. Router-A# write

同樣地,在路由器B重複一次配置。

在我們繼續下一步之前,確認下彼此的IP是可以ping通的。


 
  1. Router-A# ping 100.100.0.2


 
  1. PING 100.100.0.2 (100.100.0.2) 56(84) bytes of data.
  2. 64 bytes from 100.100.0.2: icmp_seq=1 ttl=64 time=0.616 ms

下一步,我們將繼續配置BGP對等和前綴設置。

配置BGP對等

Quagga守護進程負責BGP的服務叫bgpd。首先我們來準備它的配置文件。


 
  1. # cp /usr/share/doc/quagga-XXXXXXX/bgpd.conf.sample /etc/quagga/bgpd.conf

在CentOS6系統中:


 
  1. # service bgpd start
  2. # chkconfig bgpd on

在CentOS7中:


 
  1. # systemctl start bgpd
  2. # systemctl enable bgpd

現在,讓我們來進入Quagga 的shell。


 
  1. # vtysh

第一步,我們要確認當前沒有已經配置的BGP會話。在一些版本,我們可能會發現一個AS號爲7675的BGP會話。由於我們不需要這個會話,所以把它移除。


 
  1. Router-A# show running-config


 
  1. ... ... ...
  2. router bgp 7675
  3. bgp router-id 200.200.1.1
  4. ... ... ...

我們將移除一些預先配置好的BGP會話,並建立我們所需的會話取而代之。


 
  1. Router-A# configure terminal
  2. Router-A(config)# no router bgp 7675
  3. Router-A(config)# router bgp 100
  4. Router-A(config)# no auto-summary
  5. Router-A(config)# no synchronizaiton
  6. Router-A(config-router)# neighbor 100.100.0.2 remote-as 200
  7. Router-A(config-router)# neighbor 100.100.0.2 description "provider B"
  8. Router-A(config-router)# exit
  9. Router-A(config)# exit
  10. Router-A# write

路由器B將用同樣的方式來進行配置,以下配置提供作爲參考。


 
  1. Router-B# configure terminal
  2. Router-B(config)# no router bgp 7675
  3. Router-B(config)# router bgp 200
  4. Router-B(config)# no auto-summary
  5. Router-B(config)# no synchronizaiton
  6. Router-B(config-router)# neighbor 100.100.0.1 remote-as 100
  7. Router-B(config-router)# neighbor 100.100.0.1 description "provider A"
  8. Router-B(config-router)# exit
  9. Router-B(config)# exit
  10. Router-B# write

當相關的路由器都被配置好,兩臺路由器之間的對等將被建立。現在讓我們通過運行下面的命令來確認:


 
  1. Router-A# show ip bgp summary

從輸出中,我們可以看到"State/PfxRcd"部分。如果對等關閉,輸出將會顯示"Idle"或者"Active'。請記住,單詞'Active'這個詞在路由器中總是不好的意思。它意味着路由器正在積極地尋找鄰居、前綴或者路由。當對等是up狀態,"State/PfxRcd"下的輸出狀態將會從特殊鄰居接收到前綴號。

在這個例子的輸出中,BGP對等只是在AS100和AS200之間呈up狀態。因此沒有前綴被更改,所以最右邊列的數值是0。

配置前綴通告

正如一開始提到,AS 100將以100.100.0.0/22作爲通告,在我們的例子中AS 200將同樣以200.200.0.0/22作爲通告。這些前綴需要被添加到BGP配置如下。

在路由器-A中:


 
  1. Router-A# configure terminal
  2. Router-A(config)# router bgp 100
  3. Router-A(config)# network 100.100.0.0/22
  4. Router-A(config)# exit
  5. Router-A# write

在路由器-B中:


 
  1. Router-B# configure terminal
  2. Router-B(config)# router bgp 200
  3. Router-B(config)# network 200.200.0.0/22
  4. Router-B(config)# exit
  5. Router-B# write

在這一點上,兩個路由器會根據需要開始通告前綴。

測試前綴通告

首先,讓我們來確認前綴的數量是否被改變了。


 
  1. Router-A# show ip bgp summary

爲了查看所接收的更多前綴細節,我們可以使用以下命令,這個命令用於顯示鄰居100.100.0.2所接收到的前綴總數。


 
  1. Router-A# show ip bgp neighbors 100.100.0.2 advertised-routes

查看哪一個前綴是我們從鄰居接收到的:


 
  1. Router-A# show ip bgp neighbors 100.100.0.2 routes

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


 
  1. Router-A# show ip bgp

以上的命令都可以被用於檢查哪個路由器通過BGP在路由器表中被學習到。


 
  1. Router-A# show ip route


 
  1. 代碼: K - 內核路由, C - 已鏈接 , S - 靜態 , R - 路由信息協議 , O - 開放式最短路徑優先協議,
  2.  
  3. I - 中間系統到中間系統的路由選擇協議, B - 邊界網關協議, > - 選擇路由, * - FIB 路由
  4.  
  5. C>* 100.100.0.0/30 is directly connected, eth0
  6. C>* 100.100.1.0/24 is directly connected, eth1
  7. B>* 200.200.0.0/22 [20/0] via 100.100.0.2, eth0, 00:06:45


 
  1. Router-A# show ip route bgp


 
  1. B>* 200.200.0.0/22 [20/0] via 100.100.0.2, eth0, 00:08:13

BGP學習到的路由也將會在Linux路由表中出現。


 
  1. [root@Router-A~]# ip route


 
  1. 100.100.0.0/30 dev eth0 proto kernel scope link src 100.100.0.1
  2. 100.100.1.0/24 dev eth1 proto kernel scope link src 100.100.1.1
  3. 200.200.0.0/22 via 100.100.0.2 dev eth0 proto zebra

最後,我們將使用ping命令來測試連通。結果將成功ping通。


 
  1. [root@Router-A~]# ping 200.200.1.1 -c 2

總而言之,本教程將重點放在如何在CentOS系統中運行一個基本的BGP路由器。這個教程讓你開始學習BGP的配置,一些更高級的設置例如設置過濾器、BGP屬性調整、本地優先級和預先路徑準備等,我將會在後續的教程中覆蓋這些主題。

希望這篇教程能給大家一些幫助。


via: http://xmodulo.com/centos-bgp-router-quagga.html

作者:Sarmed Rahman 譯者:disylee 校對:wxy

本文由 LCTT 原創翻譯,Linux中國 榮譽推出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章