zabbix监控部署keepalived高可用
一. 脑裂
在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果——或者共享资源被瓜分、2边“服务”都起不来了;或者2边“服务”都起来了,但同时读写“共享存储”,导致数据损坏(常见如数据库轮询着的联机日志出错)。
1. 对付HA系统“裂脑”的对策,目前达成共识的的大概有以下几条:
- 添加冗余的心跳线,例如:双线条线(心跳线也HA),尽量减少“裂脑”发生几率;
- 启用磁盘锁。正在服务一方锁住共享磁盘,“裂脑”发生时,让对方完全“抢不走”共享磁盘资源。但使用锁磁盘也会有一个不小的问题,如果占用共享盘的一方不主动“解锁”,另一方就永远得不到共享磁盘。现实中假如服务节点突然死机或崩溃,就不可能执行解锁命令。后备节点也就接管不了共享资源和应用服务。于是有人在HA中设计了“智能”锁。即:正在服务的一方只在发现心跳线全部断开(察觉不到对端)时才启用磁盘锁。平时就不上锁了。
- 设置仲裁机制。例如设置参考IP(如网关IP),当心跳线完全断开时,2个节点都各自ping一下参考IP,不通则表明断点就出在本端。不仅“心跳”、还兼对外“服务”的本端网络链路断了,即使启动(或继续)应用服务也没有用了,那就主动放弃竞争,让能够ping通参考IP的一端去起服务。更保险一些,ping不通参考IP的一方干脆就自我重启,以彻底释放有可能还占用着的那些共享资源
二.脑裂产生的原因
2.1 一般来说,脑裂的发生,有以下几种原因:
- 高可用服务器对之间心跳线链路发生故障,导致无法正常通信
- 因心跳线坏了(包括断了,老化)
- 因网卡及相关驱动坏了,ip配置及冲突问题(网卡直连)
- 因心跳线间连接的设备故障(网卡及交换机)
- 因仲裁的机器出问题(采用仲裁的方案)
- 高可用服务器上开启了 iptables防火墙阻挡了心跳消息传输
- 高可用服务器上心跳网卡地址等信息配置不正确,导致发送心跳失败
- 其他服务配置不当等原因,如心跳方式不同,心跳广插冲突、软件Bug等
注意:Keepalived配置里同一 VRRP实例如果 virtual_router_id两端参数配置不一致也会导致裂脑问题发生。
三. 脑裂的常见解决方案
在实际生产环境中,我们可以从以下几个方面来防止裂脑问题的发生:
- 同时使用串行电缆和以太网电缆连接,同时用两条心跳线路,这样一条线路坏了,另一个还是好的,依然能传送心跳消息
- 当检测到裂脑时强行关闭一个心跳节点(这个功能需特殊设备支持,如Stonith、feyce)。相当于备节点接收不到心跳消患,通过单独的线路发送关机命令关闭主节点的电源
- 做好对裂脑的监控报警(如邮件及手机短信等或值班).在问题发生时人为第一时间介入仲裁,降低损失。例如,百度的监控报警短信就有上行和下行的区别。报警消息发送到管理员手机上,管理员可以通过手机回复对应数字或简单的字符串操作返回给服务器.让服务器根据指令自动处理相应故障,这样解决故障的时间更短.
当然,在实施高可用方案时,要根据业务实际需求确定是否能容忍这样的损失。对于一般的网站常规业务.这个损失是可容忍的
四. 对脑裂进行监控
对脑裂的监控应在备用服务器上进行,通过添加zabbix自定义监控进行。监控什么信息呢?监控备上有无VIP地址
备机上出现VIP有两种情况:
- 发生了脑裂
- 正常的主备切换
监控只是监控发生脑裂的可能性,不能保证一定是发生了脑裂,因为正常的主备切换VIP也是会到备上的。
编写脚本时要注意,网卡要改成你自己的网卡名称,VIP也要改成你自己的VIP,最后不要忘了给脚本赋予执行权限,且要修改/scripts目录的属主属组为zabbix
五.使用zabbix监控脑裂
zabbix监控部署
[root@server ~]# yum -y install vim wget gcc gcc-c++
[root@server ~]# yum -y install keepalived nginx
[root@server ~]# vim /etc/nginx/nginx.conf
server {
listen 8080 default_server; //修改为8080
listen [::]:8080 default_server; //修改为8080
server_name _;
root /usr/share/nginx/html;
[root@server ~]# cd /usr/share/nginx/html/
[root@server html]# echo "master node" > index.html
[root@server html]# systemctl restart nginx.service
[root@server html]# systemctl enable nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service
→ /usr/lib/systemd/system/nginx.service.
[root@agent ~]# yum -y install vim wget gcc gcc-c++
[root@agent ~]# yum -y install keepalived nginx
[root@agent ~]# vim /etc/nginx/nginx.conf
server {
listen 8080 default_server; //修改为8080
listen [::]:8080 default_server; //修改为8080
server_name _;
root /usr/share/nginx/html;
[root@agent ~]# cd /usr/share/nginx/html/
[root@agent html]# echo "slave node" > index.html
[root@agent html]# systemctl restart nginx
[root@agent html]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service
→ /usr/lib/systemd/system/nginx.service.
配置VIP
[root@server html]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR1=192.168.223.100
PREFIX1=32
[root@server html]# systemctl restart NetworkManager
[root@server html]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/Ne
tworkManager/ActiveConnection/3)
修改内核参数,开启侦听VIP功能
[root@server html]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@server html]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
[root@agent html]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
[root@agent html]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
监控nginx负载均衡
[root@server ~]# mkdir /scripts
[root@server ~]# cd /scripts/
[root@server scripts]# vim check.sh
[root@server scripts]# cat check.sh
#!/bin/bash
nginx_status=`ps -ef | grep -v "grep" | grep "nginx" | wc -l`
if [ $nginx_status -lt 1 ];then
systemctl stop keepalived
fi
[root@server scripts]# chmod +x check.sh
[root@server scripts]# vim notify.sh
[root@server scripts]# cat notify.sh
#!/bin/bash
VIP=$2
sendmail () {
subject="${VIP}'s server keepalived state is translate"
content="`date +'%F %T'`: `hostname`'s state change to master"
echo $content | mail -s "$subject" [email protected]
}
case "$1" in
master)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -lt 1 ];then
systemctl start nginx
fi
sendmail
;;
backup)
nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)
if [ $nginx_status -gt 0 ];then
systemctl stop nginx
fi
;;
*)
echo "Usage:$0 master|backup VIP"
;;
esac
[root@server scripts]# chmod +x notify.sh
[root@agent html]# mkdir /scripts
[root@agent html]# cd /scripts/
[root@agent scripts]# scp [email protected]:/scripts/check.sh .
[email protected]'s password:
check.sh 100% 143 152.4KB/s 0
0:00
[root@agent scripts]# scp [email protected]:/scripts/notify.sh .
[email protected]'s password:
notify.sh 100% 588 651.3KB/s 0
0:00
配置keepalived
[root@server scripts]# vim /etc/keepalived/keepalived.conf
[root@server scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id yhm01
}
vrrp_script nginx_check {
script "/scripts/check.sh"
interval 10
weight -20
}
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.223.100
}
track_script {
nginx_check
}
notify_master "/scripts/notify.sh master 192.168.223.100"
notify_backup "/scripts/notify.sh backup 192.168.223.100"
}
virtual_server 192.168.223.100 8080 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.223.149 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.144 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@server scripts]# systemctl restart keepalived.service
[root@server scripts]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.se
rvice → /usr/lib/systemd/system/keepalived.service.
[root@server scripts]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group
default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state
UP group default qlen 1000
link/ether 00:0c:29:be:f1:34 brd ff:ff:ff:ff:ff:ff
inet 192.168.223.149/24 brd 192.168.223.255 scope global noprefixrout
e ens33
valid_lft forever preferred_lft forever
inet 192.168.223.100/32 scope global ens33
valid_lft forever preferred_lft forever
[root@agent scripts]# vim /etc/keepalived/keepalived.conf
[root@agent scripts]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id yhm02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.223.100
}
notify_master "/scripts/notify.sh master 192.168.223.100"
notify_backup "/scripts/notify.sh backup 192.168.223.100"
}
virtual_server 192.168.223.100 8080 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.223.149 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.223.144 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@agent scripts]# systemctl restart keepalived.service
[root@agent scripts]# systemctl enable keepalived.service
Created symlink /etc/systemd/system/multi-user.target.wants/keepalived.se
rvice → /usr/lib/systemd/system/keepalived.service.
测试
[root@server scripts]# systemctl stop nginx
[root@server scripts]# systemctl restart keepalived.service
[root@agent scripts]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state
UP group default qlen 1000
link/ether 00:0c:29:26:5a:1f brd ff:ff:ff:ff:ff:ff
inet 192.168.223.144/24 brd 192.168.223.255 scope global dynamic nopr
efixroute ens33
valid_lft 1422sec preferred_lft 1422sec
inet 192.168.223.100/32 scope global ens33
valid_lft forever preferred_lft forever
在备节点中,新建脚本,检查VIP
[root@agent ~]# cd /scripts/
[root@agent scripts]#
[root@agent scripts]# vim check_backupip.sh
[root@agent scripts]# cat check_backupip.sh
#!/bin/bash
a=`ip a show ens33 | grep 192.168.223.100 | wc -l`
if [ $a -eq 0 ];then
echo "0"
else
echo "1"
fi
[root@agent scripts]# chmod +x check_backupip.sh
在备份节点添加自定义监控项
[root@agent scripts]# vim /etc/zabbix/zabbix_agentd.conf
//在最末尾加入这两行,自定义监控项
UnsafeUserParameters=1
UserParameter=check.backup,/scripts/check_backupip.sh
[root@agent scripts]# systemctl restart zabbix-agent.service
在主节点中测试该监控项
[root@server scripts]# yum -y install zabbix-get
[root@server scripts]# zabbix_get -s 192.168.223.144 -k "check.backup"
0
配置监控项
配置触发器
添加媒介
[root@server scripts]# find / -name mail-send.sh
/usr/lib/zabbix/alertscripts/mail-send.sh
配置动作
模拟脑裂,关闭主节点的nginx服务
[root@server scripts]# systemctl stop nginx
[root@server scripts]# systemctl restart keepalived.service