First Hop Redundancy Protocol (FHRP) 第一跳冗馀协定 – HSRP VRRP GLBP

前言

所谓 First Hop 就是由网络里的 Host 到 Gateway 这一跳,由於 Gateway 一旦出现问题而未能提供服务,该网络里的所有 Host 就无办法与其他网络沟通,对企业或组织影响极大 (尤其在老板正在闲逛网页的时候 😆 )。因此必需为 Gateway 提供 Redundancy (冗馀),最常用的协定分别是 Hot Standby Redundancy Protocol (HSRP)丶Virtual Router Redundancy Protocol (VRRP) 及 Gateway Load Balancing Protocol (GLBP)。笔者假设读者已对 Routing 及 ARP 有相当的认识。

First Hop Redundancy Protocol (FHRP)

协定诞生的原因必然是为了解决某些问题,我们先看看在没有 FHRP 的时候会有什麽困难发生。以下实验 R1丶R2 及 R4 已用 Routing Protocol 打通,R5 当成 Host,它把 Default Gateway 指向 192.168.125.1,於是能 Ping 通 4.4.4.4。

First Hop Routing Protocol

问题是当 R1 挂掉或需要维护时,我们便要把 R5 的 Default Gateway 改成 192.168.125.2 以维持服务,一台 Host 当然可以,如纲络内有数百台 Host 则成了大问题,而且人手更改不合逻辑。FHRP 的发明是为 R1 的 e1/0 和 R2 的 e1/0 产生出一组 Virtual MAC Address 以及一组 Virtual IP Address (VIP),Host 只要把 Gateway 指向 VIP,R1 的 e1/0 和 R2 的 e1/0 自行协定由谁来工作,即接收 Host 传来的 Traffic,接收的一方又唤作 Active,另一个 Interface 则是後备,即 Standby。当 Active 挂掉,Standby 便会补上变成 Active。FHRP 只是一个统称,以下将会介绍 3 款 FHRP,分别是 HSRP丶VRRP 及 GLBP。

First Hop Routing Protocol

Hot Standby Redundancy Protocol (HSRP)

Hot Standby Redundancy Protocol (HSRP) 是 Cisco 自家产品,只有 Cisco 设备可用,要设定 HSRP 极其简单,首先设定 Group ID 和 VIP。参与 HSRP 的 Interface 之 Group ID 必需相同,VIP 需与 Interface 的 IP 处於同一个 Subnet 之中。指令是 standby <group id> ip <vip>

R1(config)#interface ethernet 1/0
R1(config-if)#standby 1 ip 192.168.125.3
R2(config)#interface ethernet 1/0
R2(config-if)#standby 1 ip 192.168.125.3

show standby 可查看 HSRP 状态。

R1#show standby 
Ethernet1/0 - Group 1
  State is Active
    2 state changes, last state change 00:10:30
  Virtual IP address is 192.168.125.3
  Active virtual MAC address is 0000.0c07.ac01
    Local virtual MAC address is 0000.0c07.ac01 (v1 default)
  Hello time 3 sec, hold time 10 sec
    Next hello sent in 2.240 secs
  Preemption disabled
  Active router is local
  Standby router is 192.168.125.2, priority 100 (expires in 11.872 sec)
  Priority 100 (default 100)
  Group name is "hsrp-Et1/0-1" (default)

State 是 HSRP 现时的角色,在 R1 看到 Active 代表现时此 Interface 是负责工作的 Active Interface,如在 R2 查看则会看到 State 为 Standby。除了 Virtual IP 外,我们还看到相对的 Virtual MAC Address。Virtual MAC Address 是自动产生的,可用以下指令更改,必需於 Group 内所有 Interface 都下指令才能生效,但其实我们很少会更改,由它自行产生便可。

R1(config-if)#standby 1 mac-address 0000.0100.1a2b

预设 Hello Time 为 3 秒,即 Router 预设每 3 秒联络一下 Group 里面其他 Interface,如 Standby Interface 过了 Hold Time (预设 10 秒) 也无法收到 Active Interface 报平安,则判断对方挂掉,自己升格成为 Active。若嫌反应时间太慢,可用指令更改,但必需把 Group 里所有 Interface 的 Timer 都更改,新设定才能生效。

R1(config-if)#standby 1 timers 2 6
R1(config-if)#do show standby
Ethernet1/0 - Group 1
  State is Active
    5 state changes, last state change 00:06:53
  Virtual IP address is 192.168.125.3
  Active virtual MAC address is 0000.0c07.ac01
    Local virtual MAC address is 0000.0c07.ac01 (v1 default)
  Hello time 2 sec, hold time 6 sec
    Next hello sent in 1.488 secs
  Preemption disabled
  Active router is local
  Standby router is 192.168.125.2, priority 100 (expires in 11.872 sec)
  Priority 100 (default 100)
  Group name is "hsrp-Et1/0-1" (default)

在 Group 里面的 Interface 中 Priority 最高者会成为 Active,预设值为 100,现在尝试在 R2 把 Priority 调高:

R2(config)#interface ethernet 1/0
R2(config-if)#standby 1 priority 150
R2(config-if)#do show standby        
Ethernet1/0 - Group 1
  State is Standby
    9 state changes, last state change 00:00:39
  Virtual IP address is 192.168.125.3
  Active virtual MAC address is 0000.0c07.ac01
    Local virtual MAC address is 0000.0c07.ac01 (v1 default)
  Hello time 2 sec, hold time 6 sec
    Next hello sent in 0.336 secs
  Preemption disabled
  Active router is 192.168.125.1, priority 100 (expires in 11.760 sec)
  Standby router is local
  Priority 150 (configured 150)
  Group name is "hsrp-Et1/0-1" (default)

奇怪的是,把 R2 e1/0 的 Priority 调高至 150 後,并没有使 R2 e1/0 即时成为 Active,反而仍处於 Standby 状态。原来为了保持稳定性,当 Priority 被修改,又或者 Priority 较高的 Interface 出现,HSRP 不会立即把其升格成 Active。必需等到现时的 Active 挂掉,Standby 收不到 Active 的 Hello Message,才能变成 Active。

但有些情况我们却希望能自主选择 Active 多於保持此种稳定性,例如 R2 的性能较高,我们希望它总是成为 Active,这时便要使用 Preempt 指令。Preempt 的意思是当该 Interface 存在於 Group 时,如果 Priority 较高则必然即时升格为 Active,无需等待。

R2(config)#int ethernet 1/0
R2(config-if)#standby 1 preempt 
*Jul 28 17:32:03.571: %HSRP-5-STATECHANGE: Ethernet1/0 Grp 1 state Standby -> Active
R2(config-if)#do show standby
Ethernet1/0 - Group 1
  State is Active
    10 state changes, last state change 00:00:40
  Virtual IP address is 192.168.125.3
  Active virtual MAC address is 0000.0c07.ac01
    Local virtual MAC address is 0000.0c07.ac01 (v1 default)
  Hello time 3 sec, hold time 10 sec
    Next hello sent in 1.904 secs
  Preemption enabled
  Active router is local
  Standby router is 192.168.125.1, priority 100 (expires in 9.696 sec)
  Priority 150 (configured 150)
  Group name is "hsrp-Et1/0-1" (default)

然後有一个有趣的问题,再看看我们的 Topology,假设 R1 e1/0 是 Active,如果图中 R1 与 R4 中间的网络断掉会出现什麽情况?因为这连线不会触发 Active Standby 角色转换,所以 Host 仍以 R1 作为 Gateway,所以讯息到了 R1 就会被 Drop 掉了。解决方法为 Track,假设 R1 e1/0 的 Priority 为 100,R2 e1/0 为 90,以下指令的意思是当 e1/1 Down 时,就把 Priority 扣减 20,如此一来,Priority 就会被扣减至 80,配合 R2 设定了 Preempt,就会立刻触发 R2 成为 Active。

R1(config-if)#standby 1 track ethernet 1/1 20

最後,基於保安理由,可为 HSRP 设定密码验证,指令如下:

R1(config-if)#standby 1 authentication ? 
  WORD  Plain text authentication string
  md5   Use MD5 authentication
  text  Plain text authentication

R1(config-if)#standby 1 authentication md5 ?   
  key-chain   Set key chain
  key-string  Set key string

R1(config-if)#standby 1 authentication md5 key-string ccie

Virtual Router Redundancy Protocol (VRRP)

如果 Group 里面存在有非 Cisco 品牌的 Router,则需使用 Virtual Router Redundancy Protocol (VRRP),其指令 99% 与 HSRP 相似,只不过把指令 standby 改为 vrrp,以下为基本设定:

interface Ethernet1/0
 ip address 192.168.125.1 255.255.255.0
 vrrp 1 ip 192.168.125.3
 vrrp 1 priority 150
 vrrp 1 authentication text ccie
R1#show vrrp
Ethernet1/0 - Group 1  
  State is Master
  Virtual IP address is 192.168.125.3
  Virtual MAC address is 0000.5e00.0101
  Advertisement interval is 1.000 sec
  Preemption enabled
  Priority is 150 
  Authentication text "ccie"
  Master Router is 192.168.125.1 (local), priority is 150 
  Master Advertisement interval is 1.000 sec
  Master Down interval is 3.414 sec

VRRP 的 State 不是 Active 和 Standby,而是 Master 和 Backup,同样是 Priority 较高者成为 Master。VRRP 用来报平安的不是 Hello Message 而是 Master Advertisement,Master 预设每 1 秒 (Master Advertisement Interval) 送出 Master Advertisement,Master Advertisement Interval 可以更改。如过了 Master Down Interval 後,Backup 没有收到来自 Master 报平安,则升格成为 Master。Master Down Interval 不能设定,会自行计算,公式如下:

3 x [Advertisement Interval] + (256 – Priority) / 256

可用以下指令更改 Master Advertisment Interval:

R1(config-if)#vrrp 1 timers advertise 2

其他 Router 则要用以下指令把自己的 Timer 跟 Master 同步。

R2(config-if)#vrrp 1 timers learn

Track 在 VRRP 会多一点点设定,首先建立一个 Track Object,例如建立 Track 10 监察 e1/1 的 Line-protocol,如 Line-protocol Down 则 Track 10 成立。

R1(config)#track 10 interface ethernet 1/1 line-protocol

然後,当 Track 10 成立,则把 Priority 扣减 50,配合 R2 设了 Preempt,就会立刻触发 R2 成为 Master。

R1(config-if)#vrrp 1 track 10 decrement 50

Gateway Load Balancing Protocol (GLBP)

HSRP 和 VRRP 都有一个问题,就是 Group 中只能有一个 Interface 成为 Active (或 Master),其他的 Interface 只能当後备,无法做到 Load Balancing 或 Load Sharing,Cisco 专有的 Gateway Load Balancing Protocol (GLBP) 可以解决此问题。GLBP 可容许多於一个 Interface 成为 Active Virtual Forwarder (AVF),可以同时接收 Host 的 Traffic,能做到这件事是因为每次有 Host 透过 ARP 询问 Gateway IP 的 MAC Address 时,GLBP 就回覆不同的 MAC Address,於是相同的 Gateway IP 在不同的 Host 中有着不同的 MAC Address 纪录,从而达成 Load Balancing。

GLBP 有两个角色,AVG 和 AVF:

Active Virtual Gateway (AVG)

Active Virtual Gateway 的作用就是负责回覆 Host 的 ARP 查询,它会收集所有 AVF 的资料,包括 IP Address 和 MAC Address 等,一个 GLBP Group 中只有一只 AVG,就是 Priority 最高的 Interface,其馀成为 Standby,设定指令跟 HSRP 相若。Priority丶Preempt丶Authentication 及 Timer 等设定不在此重覆说明。

R1(config)#int ethernet 1/0
R1(config-if)#glbp 1 ip 192.168.125.3
R1(config-if)#glbp 1 priority 150
R1(config-if)#glbp 1 preempt
R1(config-if)#glbp 1 timers 2 6
R1(config-if)#glbp 1 authentication md5 key-string ccie
R2(config)#int ethernet 1/0
R2(config-if)#glbp 1 ip 192.168.125.3
R2(config-if)#glbp 1 authentication md5 key-string ccie

Active Virtual Forwarder (AVF)

预设所有在 GLBP Group 的 Router 都会成为 Active Virtual Forwarder,AVG 会收集所有 AVF 的资料,并跟据 Load Balancing 设定来回应 ARP 查询。

首先用 show glbp 来看 GLBP 状态,这个会比 HSRP 和 VRRP 较为复杂,大家可以把结果分成两部份来看,第一部份是关於 AVG 的资讯,而第二部份则是 AVF。

R1#show glbp 
Ethernet1/0 - Group 1
  State is Active
    4 state changes, last state change 01:35:59
  Virtual IP address is 192.168.125.3
  Hello time 2 sec, hold time 6 sec
    Next hello sent in 0.480 secs
  Redirect time 600 sec, forwarder time-out 14400 sec
  Authentication MD5, key-string
  Preemption enabled, min delay 0 sec
  Active is local
  Standby is 192.168.125.2, priority 100 (expires in 4.800 sec)
  Priority 150 (configured)
  Weighting 100 (default 100), thresholds: lower 1, upper 100
  Load balancing: round-robin
  Group members:
    ca01.29e5.001c (192.168.125.1) local
    ca02.29f4.001c (192.168.125.2) authenticated

<--接下表-->

以上是 AVG 的部份,可见本机是 Active AVG,负责回应 ARP 查询。比较令人关注的应该是关於 Weighting 和 Load Balancing 的部份。GLBP 预设的 Load Balancing 办法是 Round-robin,即每次有 Host 提出 VIP 192.168.125.3 的 ARP 查询时,AVG 就会把所有 AVF Group Members 的 MAC Address 轮流用作回覆,假设 Group 内有两只 AVF,Round-robin 令大约一半 Host 纪录了 MAC Address ca01.29e5.001c (即 R1 自己),而另一半 Host 则纪录了 ca02.29f4.001c (即 R2),於是网络里的 Host 便可平均地使用 R1 和 R2 作为 Gateway 了。当然,如有 AVF 突然挂掉,AVG 就会把其 MAC Address 从 Group Members 移除,不会再向 Host 发放该 MAC Address。至於 Weighting 则会在稍後介绍另一个 Load Balancing 方法 Weighted 时再作说明。

<--接上表-->

  There are 2 forwarders (1 active)
  Forwarder 1
    State is Listen
      2 state changes, last state change 00:22:54
    MAC address is 0007.b400.0101 (learnt)
    Owner ID is ca02.29f4.001c
    Redirection enabled, 598.816 sec remaining (maximum 600 sec)
    Time to live: 14398.816 sec (maximum 14400 sec)
    Preemption enabled, min delay 30 sec
    Active is 192.168.125.2 (primary), weighting 100 (expires in 5.888 sec)
  Forwarder 2
    State is Active
      1 state change, last state change 01:56:33
    MAC address is 0007.b400.0102 (default)
    Owner ID is ca01.29e5.001c
    Redirection enabled
    Preemption enabled, min delay 30 sec
    Active is local, weighting 100

然後,我们又看到这个 Group 里有两组 Forwarder (即是刚才说过的两组 MAC Address),第一组是 ca02.29f4.001c (R2),R1 对於这组 Forwarder 的状态是 Listen,而对方 (R2) 是 Active,如果对方挂掉则 R1 会由 Listen 转为 Active,帮忙处理传到 ca02.29f4.001c 的讯息。刚才提过,当 R2 挂掉,AVG 就不会再用它的 MAC Address 来回应 ARP,所以为什麽需要有 Active 和 Listen 的机制呢?道理很简单,由於 MAC 纪录会存在於 Host 一段时间 (大约 10 分钟),在这段时间里,必需要有人接手 R2 的工作才可令该些 Host 不会因为找不到 Gateway 而断线。另一组 Forwarder 2 则相反,由 R1 做 Active,R2 是 Listen。

除了 Round-robin 外,其他的 Load Balancing 方法可用以下指令设置:

R1(config-if)#glbp 1 load-balancing ?
  host-dependent  Load balance equally, source MAC determines forwarder choice
  round-robin     Load balance equally using each forwarder in turn
  weighted        Load balance in proportion to forwarder weighting

R1(config-if)#glbp 1 load-balancing weighted 

如使用 Weighted,则可设定 Weighting 值去调校各 AVF 的 Traffic 比例。例如把 R1 e1/0 的 Weighting 值改成 200,而 R2 E1/0 则维持原设定值 100,则 AVG 会把 R1 和 R2 的 MAC Address 按 2:1 的比例来回应 Host 的 ARP 查询,於是 Host 也会按 2:1 的比例把 Traffic 送至 R1 和 R2。

R1(config-if)#glbp 1 weighting 200
R1(config-if)#end
R1#show glbp

<--Output Omitted-->

  Forwarder 1
    State is Listen
      2 state changes, last state change 00:44:18
    MAC address is 0007.b400.0101 (learnt)
    Owner ID is ca02.29f4.001c
    Redirection enabled, 598.688 sec remaining (maximum 600 sec)
    Time to live: 14398.688 sec (maximum 14400 sec)
    Preemption enabled, min delay 30 sec
    Active is 192.168.125.2 (primary), weighting 100 (expires in 5.632 sec)
  Forwarder 2
    State is Active
      1 state change, last state change 01:01:40
    MAC address is 0007.b400.0102 (default)
    Owner ID is ca01.29e5.001c
    Redirection enabled
    Preemption enabled, min delay 30 sec
    Active is local, weighting 200

另外有个进阶的玩法是可以由系统主动调校 Weighting 值,籍此控制 Router 的 Traffic 处理量。假设已建立 Track 10 在 SLA 不达标时 (例如回应时间变慢) 被触发,关於 SLA 设定请参考以下文章:SLA,如想 Track 10 被触发时把 Weighting 减少 100,使用以下指令。

R1(config-if)#glbp 1 weighting track 10 decrement 100

我们甚至可以在 Weighting 过低时把 AVF 从 Group 中移除,这就是 Threshold 设定。当 Weighting 值低於 Lower Threshold 时,该 AVF 会从 Group 中移除,直至 Weighting 值高於 Upper Threshold 时,AVF 才再次被加入 Group,指令如下:

R1(config-if)#glbp 1 weighting 200 lower 150 upper 200

相關主題

发表回复

2021-07-22

Posted In: 网络服务 Services

Leave a Comment