【高可用】replication-manager+VIP实现高可
2020-12-06 本文已影响0人
醉红尘丶
[TOC]
环境
- CentOS 7.6
- MySQL 5.7.27-log
- 已部署主从,复制方式为
增强半同步 - 高可用中间件 replication-manager
可实现自动切换、从库恢复自动加入、从库自动设置为read_only等功能
详细请查看:https://docs.signal18.io/](https://docs.signal18.io
这边采用了主从台,多从一样
node1 192.168.20.101 MySQL主
node2 192.168.20.102 MySQL从
initnode 192.168.20.200 中间件服务器,不需要什么配置,可与其他服务共用服务器
安装replication-manager
hosts配置[所有机器配置]
vi /etc/hosts
192.168.20.101 node1
192.168.20.102 node2
192.168.20.200 initnode
配置免密
中间件服务器(20.200)操作
# 一路回车
ssh-keygen
ssh-copy-id node1
ssh-copy-id node2
- 验证ssh配置,查看是否还需要密码
ssh node1
ssh node2
安装yum源[192.200操作]
vi /etc/yum.repos.d/signal18.repo
# 添加如下内容
[signal18]
name=Signal18 repositories
# 可以先试试这个路径,之前可以的,当前再次测试时候发现有问题,可替换为下面这个,注意版本
# baseurl=http://repo.signal18.io/centos/$releasever/$basearch/
baseurl=http://repo.signal18.io/centos/2.1/$releasever/$basearch/
gpgcheck=0
enabled=1
yum安装
yum install replication-manager-osc -y
MySQL主库添加监控用户【101操作】
# MySQL主库操作
create user 'rep_monitor'@'%' identified by '123456';
GRANT RELOAD, PROCESS, SUPER, REPLICATION SLAVE, REPLICATION CLIENT, EVENT ON *.* TO 'rep_monitor'@'%';
GRANT SELECT ON `mysql`.`event` TO 'rep_monitor'@'%';
GRANT SELECT ON `mysql`.`user` TO 'rep_monitor'@'%';
GRANT SELECT ON performance_schema.setup_consumers TO 'rep_monitor'@'%';
修改配置文件【200操作】
在 /etc/replication-manager/cluster.d 下有模版
实际需要修改的参数
db-servers-hosts、db-servers-prefered-master、db-servers-credential、replication-credential、、
vim /etc/replication-manager/config.toml
[yqtest] # 集群名称
title = "MySQL-Monitor"
db-servers-hosts = "192.168.20.101:3306,192.168.20.102:3306"
db-servers-prefered-master = "192.168.20.101:3306"
db-servers-credential = "rep_monitor:123456"
db-servers-connect-timeout = 2
replication-credential = "rep_monitor:123456"
# 可配置忽略某些机器,不会被用于切换
# db-servers-ignored-hosts="127.0.0.1:5057"
##############
## FAILOVER ##
##############
# 故障自动切换
failover-mode = "automatic"
# 30s内再次发生故障不切换,防止硬件问题或网络问题
failover-time-limit=30
# vip切换脚本
failover-post-script = "/etc/replication-manager/vip_up.sh"
# 【默认】如果一个从站仍可以从主站获取事件,则取消故障转移
failover-falsepositive-heartbeat = true
# 【默认】故障转移和切换将从库设置为只读
failover-readonly-state = true
#【默认】调度程序相关
failover-event-scheduler = false
failover-event-status = false
# 【默认】当从库延迟超过30s时,选主时忽略此服务器
failover-max-slave-delay = 30
[Default]
#########
## LOG ##
#########
log-file = "/var/log/replication-manager.log"
log-heartbeat = false
log-syslog = false
monitoring-datadir = "/var/lib/replication-manager"
log-level=1
replication-multi-master = false
replication-multi-tier-slave = false
failover-readonly-state = true
http-server = true
http-bind-address = "0.0.0.0"
http-port = "10001"
VIP脚本【200】
vi /etc/replication-manager/vip_up.sh
需要更改
interface、vip、ssh_user
#!/bin/bash
# 当前脚本适用于中间件为 replication-manager 的高可用VIP切换,
# 接收传入参数 cluster.oldMaster.Host cluster.master.Host cluster.oldMaster.Port cluster.master.Port
orig_master=$1
new_master=$2
old_port=$3
new_port=$4
emailaddress="email@example.com"
sendmail=0
# 根据环境配置
# 网卡名称
interface=ens32
# VIP
vip=192.168.20.111
# ssh用户
ssh_options=''
ssh_user='root'
# discover commands from our path
ssh=$(which ssh)
arping=$(which arping)
ip2util=$(which ip)
# command for adding our vip
cmd_vip_add="sudo -n $ip2util address add ${vip} dev ${interface}"
# command for deleting our vip
cmd_vip_del="sudo -n $ip2util address del ${vip}/32 dev ${interface}"
# command for discovering if our vip is enabled
cmd_vip_chk="sudo -n $ip2util address show dev ${interface} to ${vip%/*}/32"
# command for sending gratuitous arp to announce ip move
cmd_arp_fix="sudo -n $arping -c 1 -I ${interface} ${vip%/*}"
# command for sending gratuitous arp to announce ip move on current server
cmd_local_arp_fix="sudo -n $arping -c 1 ${vip%/*}"
vip_stop() {
rc=0
# ensure the vip is removed
$ssh ${ssh_options} -tt ${ssh_user}@${orig_master} \
"[ -n \"\$(${cmd_vip_chk})\" ] && ${cmd_vip_del} && sudo ${ip2util} route flush cache || [ -z \"\$(${cmd_vip_chk})\" ]"
rc=$?
return $rc
}
vip_start() {
rc=0
# ensure the vip is added
# this command should exit with failure if we are unable to add the vip
# if the vip already exists always exit 0 (whether or not we added it)
$ssh ${ssh_options} -tt ${ssh_user}@${new_master} \
"[ -z \"\$(${cmd_vip_chk})\" ] && ${cmd_vip_add} && ${cmd_arp_fix} || [ -n \"\$(${cmd_vip_chk})\" ]"
rc=$?
$cmd_local_arp_fix
return $rc
}
vip_status() {
$arping -c 1 ${vip%/*}
if ping -c 1 -W 1 "$vip"; then
return 0
else
return 1
fi
}
echo "`date +'%Y-%m-%d %T'` Master is dead, failover"
# make sure the vip is not available
if vip_status; then
if vip_stop; then
if [ $sendmail -eq 1 ]; then mail -s "$vip is removed from orig_master." "$emailaddress" < /dev/null &> /dev/null ; fi
else
if [ $sendmail -eq 1 ]; then mail -s "Couldn't remove $vip from orig_master." "$emailaddress" < /dev/null &> /dev/null ; fi
exit 1
fi
fi
if vip_start; then
echo "`date +'%Y-%m-%d %T'` $vip is moved to $new_master."
if [ $sendmail -eq 1 ]; then mail -s "$vip is moved to $new_master." "$emailaddress" < /dev/null &> /dev/null ; fi
else
echo "`date +'%Y-%m-%d %T'` Can't add $vip on $new_master!"
if [ $sendmail -eq 1 ]; then mail -s "Can't add $vip on $new_master!" "$emailaddress" < /dev/null &> /dev/null ; fi
exit 1
fi
更改权限【200】
cd /etc/replication-manager
chmod +x vip_up.sh
主库上手动添加VIP【101】
ip address add 192.168.20.111 dev ens32
启动【200】
systemctl start replication-manager
# 开机启动
systemctl enable replication-manager
登陆页面查看
打开浏览器输入 http://192.168.20.200:10001
2.1 起可配置用户名密码,默认用户名密码为 admin/repman
image.png
搞定!,replication-manager 部署就是这么简单。
切换测试
关闭主MySQL[192.101操作]
mysqladmin -h127.0.0.1 -uroot -p shutdown
检测到主库挂掉
image.png
切换到102,提拔102为新主
image.png
发现VIP已漂移到 102上
image.png
启动原主
image.png
自动加入到集群中来了
image.png