1.Keepalived高可用列脑

由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方的心跳信息,各自取得资源及服务器的所有权,而此时的两台高可用服务器又都还活着。

  • ​ 服务器网线松动等网络故障
  • ​ 服务器硬件古装发生损坏现象而崩溃
  • ​ 主备都开启firewalld防火墙
  • ​ Nginx服务器死掉等

2. backup节点操作

1.在备上编写检测脚本,检测如果能ping通主并且备节点还有vip的话测认为列脑

[root@lb02 ~]# vim /etc/keepalived/nginx_check.sh
#!/bin/bash
lb01_vip=10.0.21.117
lb01_ip=10.0.21.115
A=`ssh $lb01_ip "ip add | grep "$lb01_vip"|wc -l"`
if [ $A -eq 0 -a `ip add | grep "$lb01_vip"|wc -l ` -eq 1 ];then
sleep 5
ssh $lb01_ip "ip add | grep "$lb01_vip"|wc -l"
if [ $? -eq 0 -a `ip add | grep "$lb01_vip"|wc -l ` -eq 1 ];then
systemctl stop keepalived
fi

fi

2.修改配置文件

[root@lb02 ~]#  cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 50
}
#添加关联脚本
vrrp_script check_web {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight 50
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.21.117
}
#关联检测脚本
track_script {
check_web
}
}
[root@lb02 ~]# systemctl restart keepalived

3. maskter节点操作

如果nginx宕机,会导致用户请求失败,但keepalived并不会进行切换,所以需要编写一个检测nginx的存活脚本,如果不存活则kill nginx和keepalived

1.编写脚本

[root@lb01 keepalived]# vim check_web_pid.sh 
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl restart nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi

2.修改配置文件

[root@lb01 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
1124740487@qq.com
}
router_id master
}
#关联检测脚本
vrrp_script check_web {
script "/etc/keepalived/check_web_pid.sh"
interval 2
weight 50
}
nginx_check.sh
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.21.117
}
#关联检测脚本
track_script {
check_web
}
}

[root@lb01 ~]# systemctl restart keepalived

验证

1.关闭nginx服务测试nginx是否会通过keepalived脚本启动
2.更改nginx配置文件,模拟nginx无法正常启动,是否会关闭lb01 keepalived服务。从而 vip切换到db02上。