FHRP - 網關冗餘協議

通常情況下,在終端設備進入網絡前,都會有一個 Router 充當網絡,作爲第一跳的網絡地址。但假設路由器發生故障,此時終端設備就無法再接入互聯網。

爲了防止這樣的問題,一般會再加入一臺路由器充當備份。但此時就出現一個問題,網關地址配這兩個路由器中的那個?

於是 FHRP 協議出現,可以將多個路由器虛擬成一個路由器提供服務,當提供服務的路由器發生故障時,會自動切換到另一臺上。

FHRP 是這類路由協議的統稱,常見路由協議如下:

  • HSRP 協議,Cisco 私有協議。
  • VRRP 協議,公有協議。

HSRP

HRSP 熱備份冗餘協議,是 CISCO 私有協議,用於解決網關的備份問題。

能夠在不改變組網的情況下,將多臺路由器虛擬成一個虛擬路由器。

First Hop Redundancy Protocols (FHRP) – A Primer – AnandSoft Blog

HRSP 角色

HRSP 組:由多個 HRSP 路由器組成,屬於同一個 HSRP 組,組內的路由器會交換信息,每個組由唯一 ID 指定。經過選舉會推選出 Active 路由器和 Standby 路由器。

HSRP 虛擬路由:對每個 HSRP 組,抽象出的虛擬路由器。作爲我拿過來。具有虛擬 IP 和 MAC。虛擬出來的 IP 作爲網關地址。

Active 路由器:HSRP 中實際轉發數據包的路由器,每個 HRSP 組中,僅有一個 active 路由器,並綁定虛擬 IP,用於響應請求。

Stand by 路由器:在 HSRP 組中,處於監聽狀態的路由器,當 Active 路由器出現問題時,接替工作。

HSRP 選舉過程

  1. 處於同一 HRSP 組的路由器會向組播地址 224.0.0.2 發送 Hello 報文,使用 UDP 1985 端口。
  2. 然後⽐較 Hello 報⽂中的優先級,擁有最⾼優先級(默認爲100,範圍爲1~255)的設備將成爲活動路由器。
  3. 最後優先級⼀樣則⽐較接⼝ IP 地址,選⼤的。

HSRP狀態

  • Initial:初始化狀態,當接⼝UP的時候或某些配置變更時

  • Listen:路由器已獲知虛擬IP,開始偵聽其他同組HSRP路由器的Hello消息

  • Speak:發送週期的Hello同時參與Active/Standby路由器的選舉

  • Standby:成爲Standby路由器,同時週期性的發送Hello,持續偵聽Active路由器的Hello消息以便在其失效後接替其位置

  • Active:成爲Active路由器,響應PC對於虛擬IP的ARP請求,同時週期性發送Hello消息以宣告⾃⼰的存活狀態

HSRP 版本

HSRPV1 HSRPV2
IPv4 HSRPv4/v6
Group numbers 0 - 255 Group numbers 0 -4095
Virtual MAC: 0000:0C07:ACxx (xx 來源 HSRP 組) Virtual MAC: 0000:0C9F:Fxxx (xxx 來源 HSRP 組)
廣播地址:224.0.0.2 廣播地址:224.0.0.102

HSRP 特性

  • 在 Active 路由器中負責分發虛擬 IP 和 MAC
  • Active 路由器會每三秒給 Standby 路由器發送 Hello 報文,如果 10s 未發送,Standby 會成爲 Master 宣告 Active 路由器死亡。
  • 支持開啓搶佔功能,默認未開啓,開啓後,可配置立即搶佔和延時搶佔。
  • 支持追蹤功能,用於檢測當某個接口或路由條目丟失時,降低其 HSRP 優先級。
  • 每個接口最多支持 255 個 HSRP 組,可在一定意義上實現負載。
  • HSRP 支持明文和 MD5 認證。

HSRP 實驗1

將 R1 和 R2 配置 HRSP 協議,down 掉 R1,網絡依然正常。

# R1
inter Eth0/0
standby 1 ip 192.168.1.254
standby 1 priority 120
show standby

# R2
inter Eth0/0
standby 1 192.168.1.254
standby 1 priority 100

HSRP 實驗2

準備工作 1 - 4:

# PC 5: IP 網關
VPC5> ip 10.1.1.1/24 10.1.1.251

# PC 6:
VPC6> ip 20.1.1.1/24 20.1.1.252

# SW3:
vlan 10,20
!
hostname SW3
!
interface Ethernet0/0
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/1
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/2
 switchport access vlan 10
 switchport mode access
!
interface Ethernet0/3
 switchport access vlan 20
 switchport mode access
!

# SW1
vlan 10,20
ip routing
!
hostname SW1
!
interface Ethernet0/0
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/1
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/2
 no switchport
 ip address 100.1.1.1 255.255.255.0
!
interface Vlan10
 ip address 10.1.1.251 255.255.255.0
!
interface Vlan20
 ip address 20.1.1.251 255.255.255.0
!
router eigrp 1
 network 100.1.1.0 0.0.0.255
 network 10.1.1.251 0.0.0.255
 network 20.1.1.251 0.0.0.255
!

# SW2
vlan 10,20
ip routing
!
hostname SW2
!
interface Ethernet0/0
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/1
 switchport trunk encapsulation dot1q
 switchport mode trunk
!
interface Ethernet0/2
 no switchport
 ip address 200.1.1.1 255.255.255.0
!
interface Vlan10
 ip address 10.1.1.252 255.255.255.0
!
interface Vlan20
 ip address 20.1.1.252 255.255.255.0
!
!
router eigrp 1 
 network 200.1.1.0  0.0.0.255
 network 10.1.1.252 0.0.0.255
 network 20.1.1.252 0.0.0.255
!

# Router
!
hostname R1
!
interface Loopback0
 ip address 1.1.1.1 255.255.255.0
!
interface Ethernet0/0
 ip address 100.1.1.2 255.255.255.0
 duplex auto
!
interface Ethernet0/1
 ip address 200.1.1.2 255.255.255.0
 duplex auto
!
router eigrp 1
 network 1.1.1.1 0.0.0.0
 network 100.1.1.0 0.0.0.255
 network 200.1.1.0
!

配置成功後,PC 5 可以 ping 通 PC6.

並且可以在 SW1 或 2 看到如下的路由信息:

SW2#show ip route
Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP
       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
       E1 - OSPF external type 1, E2 - OSPF external type 2
       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
       ia - IS-IS inter area, * - candidate default, U - per-user static route
       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP
       a - application route
       + - replicated route, % - next hop override, p - overrides from PfR

Gateway of last resort is not set

      1.0.0.0/24 is subnetted, 1 subnets
D        1.1.1.0 [90/409600] via 200.1.1.2, 00:31:52, Ethernet0/2
      10.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C        10.1.1.0/24 is directly connected, Vlan10
L        10.1.1.252/32 is directly connected, Vlan10
      20.0.0.0/8 is variably subnetted, 2 subnets, 2 masks
C        20.1.1.0/24 is directly connected, Vlan20
L        20.1.1.252/32 is directly connected, Vlan20
      100.0.0.0/24 is subnetted, 1 subnets
D        100.1.1.0 [90/307200] via 200.1.1.2, 00:31:52, Ethernet0/2
      200.1.1.0/24 is variably subnetted, 2 subnets, 2 masks
C        200.1.1.0/24 is directly connected, Ethernet0/2
L        200.1.1.1/32 is directly connected, Ethernet0/2

配置 SW1 爲 VLAN 10 的根橋 ID. 配置 SW2 爲 VLAN 20 的根橋 ID.

# SW1
SW1(config)#spanning-tree vlan 10 priority 0
SW1(config)#spanning-tree vlan 20 priority 4096

# SW2
SW2(config)#spanning-tree vlan 10 priority 4096
SW2(config)#spanning-tree vlan 20 priority 0

現在用 PC5 Ping 1.1.1.1:

trace to 1.1.1.1, 8 hops max, press Ctrl+C to stop
 1   10.1.1.251   0.893 ms  0.852 ms  2.083 ms
 2   *100.1.1.2   0.654 ms (ICMP type:3, code:3, Destination port unreachable)  *

用 PC6 Ping 1.1.1.1:

VPC6> trace 1.1.1.1
trace to 1.1.1.1, 8 hops max, press Ctrl+C to stop
 1   20.1.1.252   0.402 ms  0.327 ms  0.378 ms
 2   *200.1.1.2   0.754 ms (ICMP type:3, code:3, Destination port unreachable)  *

可以發現 VLAN 10 走 SW1 ,VLAN 20 走 SW2.

配置 HSRP :

# 配置 Vlan 10 HSRP:
SW1(config)#int vlan 10
SW1(config)#standby 1 ip 10.1.1.254
SW1(config)#standby 1 priority 120

SW2(config)#int vlan 10
SW2(config)#standby 1 ip 10.1.1.254
SW2(config)#standby 1 priority 100

# 配置 Vlan 20 HSRP:
SW1(config)#int vlan 20
SW1(config)#standby 2 ip 20.1.1.254
SW1(config)#standby 2 priority 100

SW2(config)#int vlan 20
SW2(config)#standby 2 ip 20.1.1.254
SW2(config)#standby 2 priority 120

此時將 PC5 和 PC 6 的網關改爲:10.1.1.254 和 20.1.1.254. 然後 shutdown SW1 vlan 10,可以發現依然可以訪問 1.1.1.1 只不過現在走 SW2. 這條備份鏈路。

接着將 Vlan1 10 接口打開, 但發現無法將 VLAN 10 的 Active 恢復 SW1,這時需要打開搶佔功能:

SW1(config)#interface Vlan 10
SW1(config-if)#standby 1 preempt

打開認證功能:

SW2(config)#int vlan 10
SW2(config-if)#standby 1 authentication md5 key-string cisco

SW1(config)#int vlan 10
SW1(config-if)#standby 1 authentication md5 key-string cisco

打開鏈路跟蹤功能, 當 SW1 或 SW2 上行鏈路出現問題,立即進行切換:

SW1(config)# track 1 inerface e0/2 line-protocol
SW1(config)#int vlan 10
# 調用跟蹤鏈路 track1
SW1(config-if)#standby 2 track 1 decrement 30
# 搶佔等待時間 10
SW2(config-if)#standby 2 preempt delay minimum 10

SW2(config)# track 1 inerface e0/2 line-protocol
SW2(config)#int vlan 20
SW2(config-if)#standby 2 track 1 decrement 30
SW2(config-if)#standby 2 preempt delay minimum 10

VRRP

VRRP 本身是公有協議,和 HSRP 非常類似,用於解決網關備份的問題。

VRRP 角色

VRRP 組:由多個 VRRP 路由器組成,屬於同一個 VRRP 組,組內的路由器會交換信息,每個組由唯一 ID 指定。經過選舉會推選出 Active 路由器和 Standby 路由器。

VRRP 虛擬路由:對每個 HSRP 組,抽象出的虛擬路由器。作爲我拿過來。具有虛擬 IP 和 MAC。虛擬出來的 IP 作爲網關地址。

Master 路由器:HSRP 中實際轉發數據包的路由器,每個 HRSP 組中,僅有一個 Master 路由器,並綁定虛擬 IP,用於響應請求。

backup 路由器:在 HSRP 組中,處於監聽狀態的路由器,當 Master 路由器出現問題時,接替工作。

VRRP 特性

  • 在 VRRP 組中的路由器,通過 Advertise 報文交互。
    • 報文間隔時間爲 1 s,失效時間爲 3s + (256 - 優先級) / 256
  • VRRP 使用 224.0.0.18 爲組播地址,協議號爲 112
  • 每個接口最多支持 256 個組,可以實現像 HRSP 一樣的負載分攤。
  • VRRP 和 HSRP 不同的是,VRRP 虛擬出的來 IP 可以和同一網絡下的實際物理 IP 相同。稱爲 IP 擁有者,優先級爲最大 255.
    • 優先級範圍:0 - 255,可配置範圍爲 1 - 254, 255 是 IP 擁有者,0 是在關閉 VRRP 進程觸發
  • 虛擬 MAC 地址爲:000.5e00.01xx (xx 爲 VRRP 組ID)
  • VRRP 默認開啓搶佔功能

VRRP 狀態

  • Init 狀態:初始化狀態
    • 如果虛擬 IP 和物理 IP 一樣,表示爲 IP 擁有者,立即變成 Master 狀態。
    • 其他情況,都會轉化爲 Backup 狀態。
  • Backup 狀態:備份狀態
    • 等待時滯時間後,先發送 Hello 報文的成爲 master.
  • Master 狀態:實際轉發狀態

GLBP

GLBP 思科私有協議,和 VRRP 不同,組內可有多臺 Active,能實現真正意義的上的負載分擔。能提供一個虛擬 IP 和多個 MAC 地址並分配給 PC。

當 PC 在解析網關時,由於一個 IP 對應多個 MAC 地址,可將流量分配到不同的 MAC 地址所對應的設備上,實現負載分攤。

img

GLBP 角色

  • AVG(Active Virtual Gateway):在每個組中,分配 MAC 地址給成員
    • Active AVG 默認不能自動強佔,只能手動強佔。
  • AVF(Active Virtual Forwarder):組內實際轉發的成員
    • 每組最多 4 個,每個 AVF 都有一個虛擬的 MAC 地址(0007-b400-XXYY,XX 表示組號,YY代表轉發者編號)
    • 如果組內成員超過 4 個,最先成爲 AVF 成爲 Primary 成員,其餘爲 Secondary 成員(不參與轉發,處於等待狀態)。
    • 如果 Primary AVF 中的權重值小於,Secondary AVF 成員中的權重值,會被替換。
  • BVG(Backup Virtual Gateway):充當分配 MAC 地址的備份,防止單點故障

選舉過程

通過 Hello 報文交互,數值越大,優先級越高。如果優先級一樣,選 IP 地址大的。

GLBP 特性

負載分攤模式:

  • 加權負載均衡:取決於 AVF 網關的權重值
  • 主機負載均衡:確保主機一直使用同一個虛擬 MAC 地址,根據主機實現負載均衡
  • 循環負載均衡:默認模式,按照順序分配

GLBP 配置

# R1
inter Eth0/0
glbp 1 ip 192.168.1.254
show glbp

GLBP 的問題

需要注意的是 GLBP 不能用在二層鏈路上,因爲二層鏈路上存在生成樹協議,存在阻塞端口,進一步導致次優路徑。

總結

本篇共提到了三個協議,用於解決網關單點故障的問題。

從目前使用的情況看,VRRP 由於是公有協議使用的更多些。HSRP 和 GLBP 由於是思科的私有協議,僅能跑在思科設備上。

從配置的角度看,VRRP 和 HSRP 非常類似,其中概念也較爲相似。僅在命名和選舉的計算方式稍有不同。

從流量的負載角度看,GLBP 更加優秀一些,相比 VRRP 和 HSRP,實現的是真正意義上的負載分攤。但由於其存在多個 MAC 的原因,不能適用於充當二層網關的設備。

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