这里介绍如何配合前者实现Keepalived双机热备
Keepalived版本:keepalived-1.2.7
MySQL-master2:192.168.7.249
首先关闭iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
注: 若线上需要开启iptables,需加一条规则使keepalived的vrrp通行
# iptables -A INPUT -p vrrp -j ACCEPT
1.在MySQL-master1:192.168.7.201服务器上keepalived安装及配置
# make && make install
设置keepalived开机启动脚本
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig keepalived on
}
##当主、备份设备发生改变时,通过邮件通知
authentication {
# 认证方式,可以是PASS或AH两种认证方式
auth_type PASS
# 认证密码
virtual_ipaddress {
# 虚拟IP地址,随着state的变化而增加删除
real_server 192.168.7.201 3306 {
# 权重
}
}
----------------------
另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP.
以上脚本是为了在测试环境实现VIP切换功能,生产环境建议使用如下脚本
---------------------------------
#!/bin/bash
# 环境变量
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
# 暂停5秒执行,防止数据库人工正常重启
sleep 5
# mysql_id(存活=1 死掉=0)
mysql_id=`ps -C mysqld --noheader |wc -l`
# 判断mysql_id若死掉,则重启mysql一次,若仍然无法启动mysql则杀掉keepaliaved进程实现VIP切换
if [ $mysql_id -eq 0 ];then
/etc/init.d/mysqld restart
sleep 5
if [ $mysql_id -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
---------------------------------
# /etc/init.d/keepalived start
root 1387 1 0 21:13 ? 00:00:00 keepalived -D
root 1390 1387 0 21:13 ? 00:00:00 keepalived -D
root 1391 1387 0 21:13 ? 00:00:00 keepalived -D
root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived
--------------------
测试
查看VIP信息
# ip addr
--------------------------
............
inet 192.168.7.201/24 brd 192.168.7.255 scope global eth0
inet 192.168.7.253/32 scope global eth0
inet6 fe80::20c:29ff:feb2:9199/64 scope link
valid_lft forever preferred_lft forever
--------------------------
注:如果出现主从在切换VIP时,主的VIP未自动关闭,从的正常开启,导致都获取到VIP,造成互相冲突的情况,可手动输入如下命令删除一方的VIP地址
# ip addr del "虚拟ip" dev eth0
但此方法只是一个临时的解决方案,当手动删除VIP后,重启keepalived服务会无法正常开启vip,需要重启服务器方能恢复。
查看vrrp通信
# tcpdump vrrp
-------------------------
.....
18:10:56.365730 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
18:10:57.366825 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
18:10:58.367914 IP 192.168.7.201 > vrrp.mcast.net: VRRPv2, Advertisement, vrid 52, prio 40, authtype simple, intvl 1s, length 20
-------------------------
注:如果主keepalived挂掉,这里的日志会切换到备keeplived的信息
无返回结果
# make && make install
设置keepalived开机启动脚本
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig keepalived on
}
}
----------------------
# chmod +x /etc/rc.d/keepalived.sh
# /etc/init.d/keepalived start
root 1387 1 0 21:13 ? 00:00:00 keepalived -D
root 1390 1387 0 21:13 ? 00:00:00 keepalived -D
root 1391 1387 0 21:13 ? 00:00:00 keepalived -D
root 1976 1911 0 21:16 pts/0 00:00:00 grep keepalived
--------------------
这里实际连接的master数据库,以哪台master先开mysql服务为准。
当master1:192.168.7.201 mysql端口挡掉或者服务器异常关闭,keepalived会自动跳转到master2:192.168.7.249.
注:当某一台master服务器挂掉恢复后,需同时打开MYSQL服务和keepalived服务,保证另一台服务器如果挂掉会无缝转接。
# chkconfig keepalived on
----------大功告成------------
转载于:https://blog.51cto.com/showerlee/1188549