路由器开启VRRP功能后,会根据优先级确定自己在备份组中的角色。优先级高的路由器成为主用路由器,优先级低的成为备用路由器。主用路由器定期发送VRRP通告报文,通知备份组内的其他路由器自己工作正常;备用路由器则启动定时器等待通告报文的到来。
2.VRRP在不同的主用抢占方式下,主用角色的替换方式不同:
l在抢占方式下,当主用路由器收到VRRP通告报文后,会将自己的优先级与通告报文中的优先级进行比较。如果大于通告报文中的优先级,则成为主用路由器;否则将保持备用状态。
l在非抢占方式下,只要主用路由器没有出现故障,备份组中的路由器始终保持主用或备用状态,备份组中的路由器即使随后被配置了更高的优先级也不会成为主用路由器。
3.如果备用路由器的定时器超时后仍未收到主用路由器发送来的VRRP通告报文,则认为主用路由器已经无法正常工作,此时备用路由器会认为自己是主用路由器,并对外发送VRRP通告报文。备份组内的路由器根据优先级选举出主用路由器,承担报文的转发功能。
高可用负载均衡 Lvs+keepalived
Lvs+keepalived 实现
Server5: lvs(DR模式)+keepalived server4: lvs(DR模式)+keepalived
Server5:
配置lvs,根据前面操作配置好yum源
yum install ipvsadm -y
ipvsadm -A -t 172.25.40.100:80 -s rr
ipvsadm -a -t 172.25.40.100:80 -r 172.25.40.2 -g
ipvsadm -a -t 172.25.40.100:80 -r 172.25.40.3 -g
/etc/init.d/ipvsadm save
keepalived源码编译:
tar zxf keepalived-1.4.3.tar.gz
cd keepalived-1.4.3
./configure --prefix=/usr/local/keepalived --with-init=SYSV
make
make install
cd /usr/local/keepalived/etc/rc.d/init.d/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc
chmod +x keepalived
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
scp /etc/yum.repos.d/rhel-source.repo root@172.25.40.4:/etc/yum.repos.d/
scp -r keepalived/ root@172.25.40.4:/usr/local/
[root@server5init.d]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@server5 ~]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
vim /etc/keepalived/keepalived.conf
#全局定义模块
! Configuration File for keepalived
global_defs {
notification_email {br/>root@localhost
}
notification_email_from keepalived@server5.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL #此处注意router_id为负载均衡标识,在局域网内应该是唯一的。
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER #状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状态。
interface eth0
virtual_router_id 40 #虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的vitrual_router_id 是一致的.
priority 100 #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,
advert_int 1 #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,
authentication {
auth_type PASS #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,
auth_pass 1111 据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。
}
virtual_ipaddress { #虚拟ip地址,可以有多个地址,每个地址占一行,不需要子网掩码,同时这个ip 必须与我们在lvs 客户端设定的vip 相一致!
172.25.254.100
}
}
virtual_server 172.25.40.100 80 { ? #虚拟IP,来源与上面的虚拟IP地址,后面加空格加端口号
delay_loop 6 #健康检查间隔,单位为秒
lb_algo rr #负载均衡调度算法,一般用wrr、rr、wlc
lb_kind DR #负载均衡转发规则。一般包括DR,NAT,TUN,FULLNAT 4种 #persistence_timeout 50 #会话保持时间,会话保持,就是把用户请求转发给同一个服务器,不然刚在1上提交完帐号密码,就跳转到另一台服务器2上了。 protocol TCP
real_server 172.25.40.2 80 { #真实服务器,包括IP和端口号
weight 1 #权重,数值越大,权重越高
TCP_CHECK {#通过tcpcheck判断RealServer的健康状态
connect_timeout 3 #连接超时时间
retry 3 #重连次数
delay_before_retry 3 #重连时间间隔
}
}
real_server 172.25.40.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
scp /etc/keepalived/keepalived.conf root@172.25.40.4:/etc/keepalived/
Server4:
yum install ipvsadm -y
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
[root@server4 ~]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@server4 ~]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
编辑/etc/keepalived/keepalived.conf文件要做的改动:
notification_email_from keepalived@server4.example.com
state BACHUP
priority 50
在server5.server4 端重启keepalived服务。
测试:
在server5.server4 端安装邮件服务(mailx)
在real-server(server2,server3)上开启或者关闭httpd服务,将会给server4,server5上发送邮件:
[root@server2 ~]# /etc/init.d/httpd start
[root@server3 ~]# /etc/init.d/httpd start
![](http://i2.51cto.com/images/blog/201804/11/9fcfd0dafadb62090bd7652edf30569d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://i2.51cto.com/images/blog/201804/11/15be27e794f852fdcafd6a45eee7dd04.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
原文地址:http://blog.51cto.com/12183531/2096980