在部署网络时,我们要考虑到负载均衡和高可用,如何将流量分布到两个设备上面,如何在一台设备坏掉时,另一个设备负责负载所有的流量。这里我们可以通过思科的VRRP实现。

实验拓扑

拓扑如下:

操作命令

  • R1路由器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
R1#vlan database
R1(vlan)#vlan 10
R1(vlan)#vlan 20
R1(vlan)#vlan 30
R1(vlan)#vlan 40
R1(vlan)#exit
R1#configure terminal
R1(config)#interface range fastEthernet 0/0-1
R1(config-if-range)#switchport trunk encapsulation dot1q
R1(config-if-range)#switchport mode trunk
R1(config-if-range)#exit
R1(config)#interface vlan 10
R1(config-if)#ip address 192.168.10.252 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#vrrp 10 ip 192.168.10.254
R1(config-if)#vrrp 10 priority 150
R1(config-if)#vrrp 10 preempt
R1(config-if)#exit
R1(config)#interface vlan 20
R1(config-if)#ip add 192.168.20.252 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#vrrp 20 ip 192.168.20.254
R1(config-if)#vrrp 20 priority 150
R1(config-if)#vrrp 20 preempt
R1(config-if)#exit
R1(config)#interface vlan 30
R1(config-if)#ip address 192.168.30.252 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#vrrp 30 ip 192.168.30.254
R1(config-if)#vrrp 30 priority 100
R1(config-if)#vrrp 30 preempt
R1(config-if)#exit
R1(config)#interface vlan 40
R1(config-if)#ip address 192.168.40.252 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#vrrp 40 ip 192.168.40.254
R1(config-if)#vrrp 40 priority 100
R1(config-if)#vrrp 40 preempt
  • R2路由器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
R2#vlan database  
R2(vlan)#vlan 10
R2(vlan)#vlan 20
R2(vlan)#vlan 30
R2(vlan)#vlan 40
R2(vlan)#exit
R2#configure terminal
R2(config)#interface range fastEthernet 0/0-1
R2(config-if-range)#switchport trunk encapsulation dot1q
R2(config-if-range)#switchport mode trunk
R2(config-if-range)#exit
R2(config)#interface vlan 10
R2(config-if)#ip address 192.168.10.253 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#vrrp 10 ip 192.168.10.254
R2(config-if)#vrrp 10 priority 100
R2(config-if)#vrrp 10 preempt
R2(config-if)#exit
R2(config)#interface vlan 20
R2(config-if)#ip add 192.168.20.253 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#vrrp 20 ip 192.168.20.254
R2(config-if)#vrrp 20 priority 100
R2(config-if)#vrrp 20 preempt
R2(config-if)#exit
R2(config)#interface vlan 30
R2(config-if)#ip address 192.168.30.253 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#vrrp 30 ip 192.168.30.254
R2(config-if)#vrrp 30 priority 150
R2(config-if)#vrrp 30 preempt
R2(config-if)#exit
R2(config)#interface vlan 40
R2(config-if)#ip address 192.168.40.253 255.255.255.0
R2(config-if)#no shutdown
R2(config-if)#vrrp 40 ip 192.168.40.254
R2(config-if)#vrrp 40 priority 150
R2(config-if)#vrrp 40 preempt
  • R3路由器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
R3#vlan database  
R3(vlan)#vlan 10
R3(vlan)#vlan 20
R3(vlan)#vlan 30
R3(vlan)#vlan 40
R3(vlan)#exit
R3#configure terminal
R3(config)#interface range fastEthernet 0/0-1
R3(config-if-range)#switchport trunk encapsulation dot1q
R3(config-if-range)#switchport mode trunk
R3(config-if-range)#exit
R3(config)#interface fastEthernet 0/2
R3(config-if)#switchport access vlan 10
R3(config)#interface fastEthernet 0/3
R3(config-if)#switchport access vlan 20
  • R4路由器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
R4#vlan database  
R4(vlan)#vlan 10
R4(vlan)#vlan 20
R4(vlan)#vlan 30
R4(vlan)#vlan 40
R4(vlan)#exit
R4#configure terminal
R4(config)#interface range fastEthernet 0/0-1
R4(config-if-range)#switchport trunk encapsulation dot1q
R4(config-if-range)#switchport mode trunk
R4(config-if-range)#exit
R4(config)#interface fastEthernet 0/2
R4(config-if)#switchport access vlan 30
R4(config)#interface fastEthernet 0/3
R4(config-if)#switchport access vlan 40

实现效果

根据上面配置,当VLAN10/VLAN20发送数据,会通过R1,而VLAN30/VLAN40发送数据,会通过R2。

这就是负载均衡了,流量均衡到两台设备上,降低了设备的负载;当出现故障时,例如R2宕机了,R1就会承担R2流量。

实现原理

终端在发送数据时,会先去找网关,而网关地址是通过R1/R2的优先级决定的,优先级高的会抢虚拟网关IP,而优先级低的只能等高的设备宕机后再抢过来,以此实现高可用性。

而将两台设备的优先级平均分配,这样就实现了负载均衡。

评论