real_server 192.168.1.23 3306 {
weight 1
notify_down /etc/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
备库配置:
[root@node2 keepalived-1.4.4]# cp -rp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
[root@node2 keepalived-1.4.4]# >/etc/keepalived/keepalived.conf
[root@node2 keepalived-1.4.4]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
rangle@163.com
}
notification_email_from rangle@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Node_backup
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 43
priority 90 ##主优先级高于备,主设置100,备可设置90,(1-254之间)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.27 label eth1:1
}
}
virtual_server 192.168.1.27 3306 {
delay_loop 2 ##每隔2秒检查真实服务器状态
lb_algo wrr ##LVS算法,如果仅做高可用可不设
lb_kind DR ##LVS模式,如果仅做高可用可不设
persistence_timeout 60
protocol TCP
real_server 192.168.1.24 3306{
weight 3
notify_down /etc/keepalived/mysql.sh ##如果mysql服务宕掉,执行的脚本
echo '3' > /etc/keepalived/t.log
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
mysql.sh配置内容:
#!/bin/bash
run_status=`service keepalived status|grep running|wc -l`
pro_status=`ps -ef |grep keepalived|grep -v grep |wc -l`
service keepalived stop
if [ ${run_status} != 0 ] || [ ${pro_status} != 0 ]
then
pkill keepalived
fi
五、keepalived优化
1、keepalived+mysql风险点
1)脑裂风险
现象:
如果由于主备内部网络通信(vrrp组播)故障(网络波动、防火墙等)导致VIP切换,但实际主备服务均正常,此时VIP会存在两个节点上,这个就会导致主备数据同时写入导致数据不一致。
优化方案:
****主库节点增加脑裂检查脚本,通过本机增加网关链路的检查,增加仲裁节点,判断是否本机对外的网络出现问题,此时在配合VRRP组播,如果网络存在问题则直接关闭keepalived和mysql服务,然后发送告警邮件、短信。
2)主从同步漂移风险
现象:
VIP漂移不会检查从库状态,即无论从库数据是否同步,VIP都将完成漂移,数据不一致风险较大。
优化方案:
****在从节点增加主从状态检查脚本,判断主从同步进程是否正常,同时判断是否有数据延迟,如果有延迟或同步错误则不能完成切换,直接关闭keepalived服务
2、优化后配置如下
具体优化配置和脚本如下:
主库配置:
keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@163.com
}
notification_email_from root@localhost
smtp_server root@localhost
smtp_connect_timeout 30
router_id node1
}
vrrp_script check_local {
script "/etc/keepalived/check_gateway.sh"
interval 5
fall 4
rise 4
weight 3
}