mysql双主互从通过KeepAlived虚拟IP实现高可用
在mysql 双主互从的基础上,
架构图:
Keepalived有两个主要的功能:
- 提供虚拟IP,实现双机热备
- 通过LVS,实现负载均衡
安装
# 安装
yum -y install keepalived
# 卸载
yum remove keepalived
修改 keepalived 配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.original
vi /etc/keepalived/keepalived.conf
master
keepalived.conf
global_defs {router_id LVS_1script_user root
}vrrp_script check_mysql_port {script "/etc/keepalived/check_mysql_port.sh" # 脚本存放的位置interval 2 # 每隔两秒运行上一行脚本weight 2
}vrrp_instance VI_1 {# 主机=MASTER;备用机=BACKUPstate MASTER# 该实例绑定的网卡名称interface ens192# 保证主备节点一致virtual_router_id 148# 权重,master值 > backup值priority 100# 主备组播报发送间隔时间1秒advert_int 1# 认证权限密码,防止非法节点进入authentication {auth_type PASSauth_pass 1111} # 虚拟出来的ip,可以有多个(vip)virtual_ipaddress {178.119.30.13}# 调用监控脚本track_script {check_mysql_port}
}
检测xxx.sh
check_mysql_port.sh
安装killall 命令
# 查询
yum search killall
# 安装对应组件
yum install psmisc -y
安装netstat
yum install -y net-tools
#!/bin/bash
A=`netstat -nltp|grep 3306|wc -l`
if [ $A -eq 2 ];
thenecho "$host mysql login successfully"exit 0
elseecho "mysql login failed"killall keepalivedexit 2
fi
修改脚本xxx.sh权限
chmod 777 check_mysql_port.sh
slave
keepalived.conf
global_defs {router_id LVS_1script_user root
}vrrp_script check_mysql_port {script "/etc/keepalived/check_mysql_port.sh" # 脚本存放的位置interval 2 # 每隔两秒运行上一行脚本weight 2
}vrrp_instance VI_1 {# 主机=MASTER;备用机=BACKUPstate BACKUP# 该实例绑定的网卡名称interface ens192# 保证主备节点一致virtual_router_id 148# 权重,master值 > backup值priority 80# 主备组播报发送间隔时间1秒advert_int 1# 认证权限密码,防止非法节点进入authentication {auth_type PASSauth_pass 1111} # 虚拟出来的ip,可以有多个(vip)virtual_ipaddress {178.119.30.13}# 调用监控脚本track_script {check_mysql_port}
}
master VS slave
结果展示
启动keepalived
- master
- slave
停止容器,使vip飘移
- master
- slave
systemctl 命令
# 启动 keepalived
systemctl start keepalived # 加入开机启动 keepalived
systemctl enable keepalived # 重新启动 keepalived
systemctl restart keepalived # 查看 keepalived 状态
systemctl status keepalived # 加载配置文件
systemctl daemon-reload