高可用-增加容错性(HA:High availability)
协议:VRRP(虚拟路由冗余协议) 公有协议 224.0.0.18
HSRP(热备份路由协议) 私有协议,Cisco公司 224.0.0.5 224.0.0.6
高可用软件:
keepalived:使用vrrp实现多台主机高可用群集
高可用角色:master 主服务器
backup 备服务器
实施步骤:
目的:实现两台负载均衡器的高可用
环境:两台负载均衡器
lb1:192.168.8.5
lb2:192.168.8.6
(1)安装keepalived(两台都装)
yum -y install keepalived
(2)配置keepalived
主服务器:lb1
vim /etc/keepalived/keepalived.conf
修改为:
> /etc/keepalived/keepalived.conf
cat >> /etc/keepalived/keepalived.conf << END
global_defs {
router_id lb1 #当前服务器主机名
}
vrrp_instance VI_1 {
state MASTER #MASTER表示活跃状态
interface ens33 #接口ens33
virtual_router_id 51 #虚拟服务器ID(主机名)
priority 100 #优先级0-100 优先级高的成为MASTER
advert_int 1 #心跳时间1秒钟1次
authentication {
auth_type PASS #验证类型
auth_pass 1111 #验证密码
}
virtual_ipaddress {
192.168.8.254 #虚拟服务器的IP地址
}
}
END
保存退出
启动服务:systemctl restart keepalived
备服务器:lb2
vim /etc/keepalived/keepalived.conf
修改为:
>/etc/keepalived/keepalived.conf
cat >> /etc/keepalived/keepalived.conf << END
global_defs {
router_id lb2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.254 #虚拟路由ip,公共ip
}
}
END
保存退出
启动服务:systemctl restart keepalived
(3)查看虚拟ip(漂移ip地址)
ip a show dev ens33
(4)复制lb1(192.168.8.129)关于nginx的配置到lb2(192.168.8.132)
现在lb2安装nginx:yum -y install nginx
在lb1上scp复制nginx的所有配置:
scp -rp /etc/nginx/* root@192.168.8.132:/etc/nginx
然后,在lb2上启动nginx服务:
systemctl restart nginx
(5)客户端修改hosts文件,访问验证(访问成功,关闭主服务器,再访问)
vim /etc/hosts
修改为:
192.168.8.254 blog.benet.com
保存退出
3.高可用裂脑
高可用节点之间互相失去联系,自认为自己是主服务器,就会出现多主现象,即裂脑现象
裂脑出现的原因:
心跳线松动或网卡故障
服务器硬件故障,崩溃
节点服务器开启防火墙,却没有做vrrp例外
nginx服务死掉,不会出现裂脑现象,但整个集群都无法正常运作
(1)检测裂脑脚本(在备用服务器:192.168.8.6运行)
vim split_brain.sh
#!/bin/sh
while true
do
ping -c 2 -W 3 192.168.8.5 &> /dev/null
if [ $? -eq 0 -a `ip add|grep 192.168.8.254|wc -l` -eq 1 ]
then
echo "split brain....."
else
echo "HA is ok"
fi
sleep 5
done
保存退出
chmod +x split_brain.sh
source split_brain.sh
开启防火墙验证:systemctl start firewalld
解决因为防火墙出现的裂脑现象:
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
(2)解决nginx故障造成群集无法工作
编辑nginx监控脚本
mkdir /sh
cat >> /sh/check_nginx_proxy.sh << END
#!/bin/bash
killall -0 nginx
if [ \$? -ne 0 ];then
systemctl stop keepalived
fi
END
chmod +x /sh/check_nginx_proxy.sh
添加脚本追踪模块到keepalived配置文件
vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb1
}
vrrp_script check_nginx_proxy {
script “/sh/check_nginx_proxy.sh”
interval 2
weight 5
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.254
}
track_script {
check_nginx_proxy
}
}
保存退出
重启服务:systemctl restart keepalived
(3)主服务器添加计划任务
crontab -e
* * * * * /bin/bash /sh/check_nginx_proxy.sh
(4)主服务器关闭nginx,测试keepalived地址漂移