MySQL的主从复制和主主复制可以参考一下两篇文章:
master服务器IP | 192.168.7.67 |
slave服务器IP | 192.168.7.71 |
1、keepalived部署(master和slave服务器都安装)
安装依赖包:
[root@docker mysql]# yum install gcc libnl libnl-devel libnfnetlink libnfnetlink-devel openssl-devel
安装keepalived
[root@docker ~]# wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
[root@docker ~]# tar -zxvf keepalived-2.2.8.tar.gz
[root@docker ~]# cd keepalived-2.2.8
[root@docker keepalived-2.2.8]# ./configure --prefix=/data/keepalived
[root@docker keepalived-2.2.8]# make && make install
[root@docker keepalived-2.2.8]# mkdir /etc/keepalived
#配置服务文件
[root@docker keepalived-2.2.8]# cp /data/keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@docker keepalived-2.2.8]# cp /data/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@docker keepalived-2.2.8]# cp /data/keepalived/sbin/keepalived /usr/sbin/
[root@docker keepalived-2.2.8]# cp keepalived/etc/init.d/keepalived /etc/rc.d/init.d/keepalived
#加入keepalived服务
[root@docker keepalived-2.2.8]# chkconfig --add keepalived
[root@docker keepalived-2.2.8]# chkconfig keepalived on
2、配置master服务器上的keepalived配置
[root@docker keepalived]# cd /etc/keepalived/
[root@docker keepalived]# cp keepalived.conf keepalived.conf.bak
master的keepalived配置:
aglobal_defs {
router_id LVS_MASTER #名称标记为master,名字随便取
vrrp_gna_interval 0
}
vrrp_script check_mysql{
script "/etc/keepalived/check_mysql.sh" #心跳执行的脚本,检测nginx是否启动
interval 2 #(检测脚本执行的间隔,单位是秒)
rise 1
fall 1
}
vrrp_instance VI_1 {
state MASTER #指定当前节点为master节点,只能有一个master,其余只能是backup
interface ens33 #绑定此虚拟路由使用的网卡的名称,使用ifconfig或者ip addr查看
virtual_router_id 99 #指定虚拟路由id,虚拟路由的唯一标识,范围是0-255,mater和backup节点需要指定一样的,相同id为一组
priority 200 #指定当前结点的优先级,master节点要大于backup节点。
advert_int 1 #指定发送VRRP通告的间隔,单位是秒
virtual_ipaddress {
192.168.7.72 #指定虚拟ip,自己定义的虚拟ip
}
#添加跟踪(执行脚本)
track_script{
check_mysql
}
}
check_mysql.sh脚本:
#如果mysql服务挂掉了,就会关闭keepalived的服务,让VIP漂移到其他服务节点
-------------------------------------------------------------------------------------------
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
service keepalived stop
fi
#当然,你也可以抢救1次再关闭....
-------------------------------------------------------------------------------------------
##!/bin/bash
#counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
#if [ "${counter}" -eq 0 ]; then
# systemctl restart mysql
# sleep 5;
# counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
# if [ "${counter}" -eq 0 ]; then
# killall keepalived
# fi
chmod +x check_mysql.sh #脚本执行权限
service keepalived start #启动
3、配置slave服务器上的keepalived配置
slave的keepalived配置:
global_defs {
router_id LVS_SLAVE #名称标记为master,名字随便取
vrrp_gna_interval 0
}
vrrp_script check_mysql{
script "/etc/keepalived/check_mysql.sh" #心跳执行的脚本,检测nginx是否启动
interval 2 #(检测脚本执行的间隔,单位是秒)
rise 1
fall 1
}
vrrp_instance VI_1 {
state Master #指定当前节点为master节点,只能有一个master,其余只能是backup
interface ens33 #绑定此虚拟路由使用的网卡的名称,使用ifconfig或者ip addr查看
virtual_router_id 99 #指定虚拟路由id,虚拟路由的唯一标识,范围是0-255,mater和backup节点需要指定一样的,相同id为一组
priority 199 #指定当前结点的优先级,master节点要大于backup节点。
advert_int 1 #指定发送VRRP通告的间隔,单位是秒
virtual_ipaddress {
192.168.7.72 #指定虚拟ip,自己定义的虚拟ip
}
#添加跟踪(执行脚本)
track_script{
check_mysql
}
}
check_mysql.sh脚本从master传过来;
service keepalived start #启动
4、验证
通过VIP:192.168.7.72可以连接成功。
停止master的MySQL服务器,查看是否还可以连接成功。
可以在slave服务器通过ip a 查看到VIP已经切换过来了。