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