VRRP工作原理

简介:
VRRP:(Virtual Router Redundancy Protocol)虚拟路由器冗余协议
主用路由器master:负责转发发给虚拟路由器的数据包,并响应ARP请求的路由器。若一台拥有与虚拟路由器有相同IP地址的VRRP路由器(IP拥有者)活动,则此VRRP路由器为主用路由器。
备用路由器backup:在VRRP中,其他参与此虚拟路由器的均为备用路由器。它将在主用路由器不能工作时接替其工作。
 
工作机制:
VRRP协议通过发送协议报文来进行主、备关系的选举;协议报文主要是用来传递VRRP路由器优先级、主用路由器状态(含有VRID、VIP)等信息,通过组播通讯(224.0.0.18);每个虚拟路由器组有一个 VRID, 它定义了此组的所有成员;为减少网络流量,只有主用路由器可以定期发布VRRP广告消息,若备用路由器在一段时间内未收到来自主用路由器的广告,便自动转换到主用状态,并将自己广告出去。主用路由器会根据vrid生成虚拟mac,00005e0001xx,作为vip的mac地址,并通过发送arp报文通告给网络中的设备;若主用路由器不能工作,则优先级最高的备用路由器将接替主用路由器工作(很短的延时),作为有效的虚拟路由器转发数据包;VRRP仅在本网段有效,其他网段的通信与其无关。
 
 案例一
 
实验设备:华为(两台路由器、两台交换机、两台物理主机)
实验拓扑图:
 
路由器r1配置:
 
  
  1. r1]inter e1.10  
  2. [r1-Ethernet1.10]vlan-type dot1q vid 10            //封装dot1q协议  
  3. [r1-Ethernet1.10]ip add 192.168.10.1 255.255.255.0 //配置子接口地址  
  4. [r1-Ethernet1.10]inter e1.20                        
  5. [r1-Ethernet1.20]vlan-type dot1q vid 20            
  6. [r1-Ethernet1.20]ip add 192.168.20.1 255.255.255.0  
  7. [r1-Ethernet1.20]quit  
  8. [r1]vrrp ping-enable                            //启动ping vrrp (配置前要先启动)  
  9. [r1]inter e1.10             
  10. [r1-Ethernet1.10]vrrp vrid 10 virtual-ip 192.168.10.254 //配置虚拟ip地址  
  11. [r1-Ethernet1.10]vrrp vrid 10 priority 130             //设置优先级  
  12. [r1-Ethernet1.10]inter e1.20                             
  13. [r1-Ethernet1.20]vrrp vrid 20 virtual-ip 192.168.20.254  
  14. [r1-Ethernet1.20]vrrp vrid 20 priority 120  
 
查看配置结果:
 
  
  1. [r1]dis cu  
  2.  interface Ethernet1.10  
  3.     vlan-type dot1q vid 10                  
  4.     ip address 192.168.10.1 255.255.255.0  
  5.     vrrp vrid 10 virtual-ip 192.168.10.254  
  6.     vrrp vrid 10 priority 130  
  7.  !  
  8.  interface Ethernet1.20  
  9.     vlan-type dot1q vid 20   
  10.     ip address 192.168.20.1 255.255.255.0  
  11.     vrrp vrid 20 virtual-ip 192.168.20.254  
  12.     vrrp vrid 20 priority 120  
查看vrrp信息:
 
  
  1. [r1]dis vrrp  
  2.    Ethernet1.20 | Virtual Router 20  
  3.        state : Master   
  4.  Virtual IP : 192.168.20.254  
  5.     Priority : 120  
  6.      Preempt : YES   Delay Time : 0  
  7.        Timer : 1  
  8.    Auth Type : NO  
  9.    
  10.    Ethernet1.10 | Virtual Router 10  
  11.        state : Master   
  12.  Virtual IP : 192.168.10.254  
  13.     Priority : 130  
  14.      Preempt : YES   Delay Time : 0  
  15.        Timer : 1  
  16.    Auth Type : NO  
 路由器r2配置:
 
  
  1. [r2]inter e1.10  
  2. [r2-Ethernet1.10]vlan-type dot1q vid 10  
  3. [r2-Ethernet1.10]ip add 192.168.10.2 255.255.255.0  
  4. [r2-Ethernet1.10]inter e1.20                        
  5. [r2-Ethernet1.20]vlan-type dot1q vid 20             
  6. [r2-Ethernet1.20]ip add 192.168.20.2 255.255.255.0  
  7. [r2-Ethernet1.20]quit  
  8. [r2]vrrp ping-e  
  9. [r2]inter e1.10             
  10. [r2-Ethernet1.10]vrrp vrid 10 virtual-ip 192.168.10.254  
  11. [r2-Ethernet1.10]vrrp vrid 10 priority 120  
  12. [r2-Ethernet1.10]inter e1.20                             
  13. [r2-Ethernet1.20]vrrp vrid 20 virtual-ip 192.168.20.254  
查看配置结果:
 
  
  1. interface Ethernet1.10  
  2.     vlan-type dot1q vid 10   
  3.     ip address 192.168.10.2 255.255.255.0  
  4.     vrrp vrid 10 virtual-ip 192.168.10.254  
  5.     vrrp vrid 10 priority 120               
  6.  !  
  7.  interface Ethernet1.20  
  8.     vlan-type dot1q vid 20   
  9.     ip address 192.168.20.2 255.255.255.0  
  10.     vrrp vrid 20 virtual-ip 192.168.20.254  
 查看vrrp信息:
 
  
  1. [r2] dis vrrp  
  2.    Ethernet1.20 | Virtual Router 20  
  3.        state : Backup   
  4.  Virtual IP : 192.168.20.254  
  5.     Priority : 100  
  6.      Preempt : YES   Delay Time : 0  
  7.        Timer : 1  
  8.    Auth Type : NO  
  9.    
  10.    Ethernet1.10 | Virtual Router 10  
  11.        state : Backup   
  12.  Virtual IP : 192.168.10.254  
  13.     Priority : 120  
  14.      Preempt : YES   Delay Time : 0  
  15.        Timer : 1  
  16.    Auth Type : NO  
 交换机sw1配置:
 
  
  1. <sw1>sys  
  2. [sw1]inter e0/24  
  3. [sw1-Ethernet0/24]port link-type trunk  
  4. [sw1-Ethernet0/24]port trunk permit vlan all  
  5. [sw1-Ethernet0/24]inter e0/1                  
  6. [sw1-Ethernet0/1]port link-type trunk        
  7. [sw1-Ethernet0/1]port trunk permit vlan all  
  8. [sw1-Ethernet0/1]vlan 10  
  9. [sw1-vlan10]port e0/5 to e0/10  
  10. [sw1-vlan10]vlan 20              
  11. [sw1-vlan20]port e0/15 to e0/20  
查看配置结果:
 
  
  1. [sw1]dis cu  
  2. interface Ethernet0/5  
  3.  port access vlan 10                        
  4. #  
  5. interface Ethernet0/6  
  6.  port access vlan 10  
  7. #  
  8. interface Ethernet0/7  
  9.  port access vlan 10  
  10. #  
  11. interface Ethernet0/8  
  12.  port access vlan 10  
  13. #  
  14. interface Ethernet0/9  
  15.  port access vlan 10  
  16. #  
  17. interface Ethernet0/10  
  18.  port access vlan 10  
  19. #  
  20. interface Ethernet0/11  
  21. #  
  22. interface Ethernet0/12  
  23. #  
  24. interface Ethernet0/13  
  25. #                                           
  26. interface Ethernet0/14  
  27. #  
  28. interface Ethernet0/15  
  29.  port access vlan 20  
  30. #  
  31. interface Ethernet0/16  
  32.  port access vlan 20  
  33. #  
  34. interface Ethernet0/17  
  35.  port access vlan 20  
  36. #  
  37. interface Ethernet0/18  
  38.  port access vlan 20  
  39. #  
  40. interface Ethernet0/19  
  41.  port access vlan 20  
  42. #  
  43. interface Ethernet0/20  
  44.  port access vlan 20  
  45. #  
  46. interface Ethernet0/21  
  47. #                                           
  48. interface Ethernet0/22  
  49. #  
  50. interface Ethernet0/23  
  51. #  
  52. interface Ethernet0/24  
  53.  port link-type trunk  
  54.  port trunk permit vlan all  
交换机sw2配置:
 
  
  1. <sw2>sys  
  2. [sw2]inter e0/24  
  3. [sw2-Ethernet0/24]port link-type trunk  
  4. [sw2-Ethernet0/24]port trunk permit vlan all  
  5. [sw2-Ethernet0/24]inter e0/1                  
  6. [sw2-Ethernet0/1]port link-type trunk        
  7. [sw2-Ethernet0/1]port trunk permit vlan all  
  8. [sw2-Ethernet0/1]vlan 10  
  9. [sw2-vlan10]port e0/5 to e0/10  
  10. [sw2-vlan10]vlan 20              
  11. [sw2-vlan20]port e0/15 to e0/20  
  查看配置结果:
 
  
  1. [sw2]dis cu  
  2. interface Ethernet0/5  
  3.  port access vlan 10                        
  4. #  
  5. interface Ethernet0/6  
  6.  port access vlan 10  
  7. #  
  8. interface Ethernet0/7  
  9.  port access vlan 10  
  10. #  
  11. interface Ethernet0/8  
  12.  port access vlan 10  
  13. #  
  14. interface Ethernet0/9  
  15.  port access vlan 10  
  16. #  
  17. interface Ethernet0/10  
  18.  port access vlan 10  
  19. #  
  20. interface Ethernet0/11  
  21. #  
  22. interface Ethernet0/12  
  23. #  
  24. interface Ethernet0/13  
  25. #                                           
  26. interface Ethernet0/14  
  27. #  
  28. interface Ethernet0/15  
  29.  port access vlan 20  
  30. #  
  31. interface Ethernet0/16  
  32.  port access vlan 20  
  33. #  
  34. interface Ethernet0/17  
  35.  port access vlan 20  
  36. #  
  37. interface Ethernet0/18  
  38.  port access vlan 20  
  39. #  
  40. interface Ethernet0/19  
  41.  port access vlan 20  
  42. #  
  43. interface Ethernet0/20  
  44.  port access vlan 20  
  45. #  
  46. interface Ethernet0/21  
  47. #                                           
  48. interface Ethernet0/22  
  49. #  
  50. interface Ethernet0/23  
  51. #  
  52. interface Ethernet0/24  
  53.  port link-type trunk  
  54.  port trunk permit vlan all  
测试
vlan10内主机跟踪到vlan20跳跃点:
 C:\Documents and Settings\Administrator>tracert 192.168.20.101
Tracing route to 192.168.20.101 over a maximum of 30 hops
 
 1     <1 ms    <1 ms    <1 ms 192.168.10.1
 2      1 ms    <1 ms    <1 ms 192.168.20.101
 
vlan20内主机跟踪到vlan10跳跃点:
 C:\Documents and Settings\Administrator>tracert 192.168.10.101
 Tracing route to 192.168.10.101 over a maximum of 30 hops
 
 1     <1 ms    <1 ms    <1 ms 192.168.20.1
 2      1 ms     1 ms    <1 ms 192.168.10.101
 
案例二
 
实验设备:思科虚拟设备(四台3640路由器、两台虚拟机)
实验拓扑图:

 

 
路由器r1配置:
 
  
  1. r1(config)#int f0/0.1  
  2. r1(config-subif)#enca dot1q 10  
  3. r1(config-subif)#ip add 192.168.10.1 255.255.255.0  
  4. r1(config-subif)#int f0/0.2                         
  5. r1(config-subif)#enca dot1q 20                      
  6. r1(config-subif)#ip add 192.168.20.1 255.255.255.0  
  7. r1(config-subif)#exi  
  8. r1(config)#inter f0/0.1  
  9. r1(config-subif)#standy 10 ip 192.168.10.254       
  10. r1(config-subif)#stan 10 pri 120  
  11. r1(config-subif)#standy 10 preempt            //配置优先级权利争夺  
  12. r1(config-subif)#inter f0/0.2                
  13. r1(config-subif)#stand 20 ip 192.168.20.254  
  14. r1(config-subif)#stand 20 pree                      
  15. r1(config-subif)#exit  
  16. r1(config)#exit  
查看配置结果:
 
 
  
  1. interface FastEthernet0/0.1  
  2.  encapsulation dot1Q 10  
  3.  ip address 192.168.10.1 255.255.255.0  
  4.  standby 10 ip 192.168.10.254  
  5.  standby 10 priority 120  
  6.  standby 10 preempt  
  7. !  
  8. interface FastEthernet0/0.2  
  9.  encapsulation dot1Q 20  
  10.  ip address 192.168.20.1 255.255.255.0  
  11.  standby 20 ip 192.168.20.254  
  12.  standby 20 preempt20  
查看hsrp信息:
 
  
  1. r1#sh stan bri  
  2.                      P indicates configured to preempt.  
  3.                      |  
  4. Interface   Grp Prio P State    Active          Standby         Virtual IP       
  5. Fa0/0.1     10 120 P Active   local           192.168.10.2    192.168.10.254   
  6. Fa0/0.2     20 100 P Standby 192.168.20.2    local           192.168.20.254  
 
 路由器r2配置:
 
  
  1. r2(config)#int f0/0.1  
  2. r2(config-subif)#enca dot1q 10  
  3. r2(config-subif)#ip add 192.168.10.2 255.255.255.0  
  4. r2(config-subif)#int f0/0.2                         
  5. r2(config-subif)#enca dot1q 20                      
  6. r2(config-subif)#ip add 192.168.20.2 255.255.255.0  
  7. r2(config-subif)#exi  
  8. r2(config)#inter f0/0.1  
  9. r2(config-subif)#stand 10 ip 192.168.10.254  
  10. r2(config-subif)#stand 10 pree  
  11. r2(config-subif)#inter f0/0.2                
  12. r2(config-subif)#stand 20 ip 192.168.20.254  
  13. r2(config-subif)#stan 20 pri 120  
  14. r2(config-subif)#stand 20 pree                      
  15. r2(config-subif)#exit  
  16. r2(config)#exit  
查看配置结果:
 
  
  1. interface FastEthernet0/0.1  
  2.  encapsulation dot1Q 10  
  3.  ip address 192.168.10.2 255.255.255.0  
  4.  standby 10 ip 192.168.10.254  
  5.  standby 10 preempt  
  6. !  
  7. interface FastEthernet0/0.2  
  8.  encapsulation dot1Q 20  
  9.  ip address 192.168.20.2 255.255.255.0  
  10.  standby 20 ip 192.168.20.254  
  11.  standby 20 priority 120  
  12.  standby 20 preempt  
查看hsrp信息:
 
  
  1. r2#sh stan bri  
  2.                      P indicates configured to preempt.  
  3.                      |  
  4. Interface   Grp Prio P State    Active          Standby         Virtual IP       
  5. Fa0/0.1     10 100 P Standby 192.168.10.1    local           192.168.10.254   
  6. Fa0/0.2     20 120 P Active   local           192.168.20.1    192.168.20.254   
 
交换机sw1配置:
 
  
  1. Router(config)#no ip routing  
  2. Router(config)#exit  
  3. sw-1(config)#inter f0/0  
  4. sw-1(config-if)#switc mod trun  
  5. sw-1(config-if)#inter f0/1      
  6. sw-1(config-if)#switc mod trun  
  7. sw-1(config-if)#exit  
  8. sw-1(config)#exit  
  9. sw-1#vlan database          //打开vlan数据库  
  10. sw-1(vlan)#vlan 10  
  11. sw-1(vlan)#vlan 20  
  12. sw-1(vlan)#exit  
  13. sw-1(config)#inter rang f0/5 -9     
  14. sw-1(config-if-range)#switch acc vlan 10  
  15. sw-1(config)#inter rang f0/10 -15  
  16. sw-1(config-if-range)#switch acc vlan 20  
查看配置结果:
 
  
  1. interface FastEthernet0/0  
  2.  switchport mode trunk  
  3. !  
  4. interface FastEthernet0/1  
  5.  switchport mode trunk  
  6. !  
  7. interface FastEthernet0/2  
  8. !  
  9. interface FastEthernet0/3  
  10. !  
  11. interface FastEthernet0/4  
  12. !  
  13. interface FastEthernet0/5  
  14.  switchport access vlan 10  
  15. !  
  16. interface FastEthernet0/6  
  17.  switchport access vlan 10  
  18. !  
  19. interface FastEthernet0/7  
  20.  switchport access vlan 10  
  21. !  
  22. interface FastEthernet0/8  
  23.  switchport access vlan 10  
  24. !  
  25. interface FastEthernet0/9  
  26.  switchport access vlan 10  
  27. !           
  28. interface FastEthernet0/10  
  29.  switchport access vlan 20  
  30. !  
  31. interface FastEthernet0/11  
  32.  switchport access vlan 20  
  33. !  
  34. interface FastEthernet0/12  
  35.  switchport access vlan 20  
  36. !  
  37. interface FastEthernet0/13  
  38.  switchport access vlan 20  
  39. !  
  40. interface FastEthernet0/14  
  41.  switchport access vlan 20  
  42. !  
  43. interface FastEthernet0/15  
  44.  switchport access vlan 20  
交换机sw2配置:
 
  
  1. Router(config)#no ip routing  
  2. Router(config)#exit  
  3. sw-2(config)#inter f0/0  
  4. sw-2(config-if)#switc mod trun  
  5. sw-2(config-if)#inter f0/1      
  6. sw-2(config-if)#switc mod trun  
  7. sw-2(config-if)#exit  
  8. sw-2(config)#exit  
  9. sw-2#vlan data  
  10. sw-2(vlan)#vlan 10  
  11. sw-2(vlan)#vlan 20  
  12. sw-2(vlan)#exit  
  13. sw-2(config)#inter rang f0/5 -9     
  14. sw-2(config-if-range)#switch acc vlan 10  
  15. sw-2(config)#inter rang f0/10 -15  
  16. sw-2(config-if-range)#switch acc vlan 20   
查看配置结果:
 
  
  1. interface FastEthernet0/0  
  2.  switchport mode trunk  
  3. !  
  4. interface FastEthernet0/1  
  5.  switchport mode trunk  
  6. !  
  7. interface FastEthernet0/2  
  8. !  
  9. interface FastEthernet0/3  
  10. !  
  11. interface FastEthernet0/4  
  12. !  
  13. interface FastEthernet0/5  
  14.  switchport access vlan 10  
  15. !  
  16. interface FastEthernet0/6  
  17.  switchport access vlan 10  
  18. !  
  19. interface FastEthernet0/7  
  20.  switchport access vlan 10  
  21. !  
  22. interface FastEthernet0/8  
  23.  switchport access vlan 10  
  24. !  
  25. interface FastEthernet0/9  
  26.  switchport access vlan 10  
  27. !           
  28. interface FastEthernet0/10  
  29.  switchport access vlan 20  
  30. !  
  31. interface FastEthernet0/11  
  32.  switchport access vlan 20  
  33. !  
  34. interface FastEthernet0/12  
  35.  switchport access vlan 20  
  36. !  
  37. interface FastEthernet0/13  
  38.  switchport access vlan 20  
  39. !  
  40. interface FastEthernet0/14  
  41.  switchport access vlan 20  
  42. !  
  43. interface FastEthernet0/15  
  44.  switchport access vlan 20  
 测试
vlan10内主机跟踪到vlan20跳跃点:
 C:\Documents and Settings\Administrator>tracert 192.168.20.20
 Tracing route to YANG-KC9WHGH6W9 [192.168.20.20]
over a maximum of 30 hops:
 
 1    109 ms    48 ms    24 ms 192.168.10.1
 2     32 ms     9 ms    10 ms YANG-KC9WHGH6W9 [192.168.20.20]
 
vlan20内主机跟踪到vlan10跳跃点:
 C:\Documents and Settings\yang>tracert 192.168.10.10
 Tracing route to 192.168.10.10 over a maximum of 30 hops
 
 1     55 ms    12 ms     6 ms 192.168.20.2
 2      5 ms    10 ms     6 ms 192.168.10.10
 
 当降低r1的f0/0.10端口的优先级到90时:
 
vlan10内主机跟踪到vlan20跳跃点:
 C:\Documents and Settings\Administrator>tracert 192.168.20.20
 Tracing route to YANG-KC9WHGH6W9 [192.168.20.20]
over a maximum of 30 hops:
 
 1     41 ms    16 ms    41 ms 192.168.10.2
 2     27 ms    38 ms     9 ms YANG-KC9WHGH6W9 [192.168.20.20]
 
vlan20内主机跟踪到vlan10跳跃点:
 C:\Documents and Settings\yang>tracert 192.168.10.10
 Tracing route to 192.168.10.10 over a maximum of 30 hops
 
 1     19 ms    44 ms     7 ms 192.168.20.2
 2     9 ms     8 ms    41 ms 192.168.10.10