前言
复习MHA,通过两种方式实现MAH的VIP漂移
|NODE|IP|ROLE|
|-|-|-|
|T1|192.168.179.191|master,MHA-node|
|T2|192.168.179.192|slave,MHA-manager,binlog-server|
|T3|192.168.179.193|slave,MHA-node|
配置了binlog-server,减轻主库压力,保持日志的长时间保持(用于主从切换后复制)
MHA自带脚本方式
这个自带脚本需要下载tgz包查看,位于mha4mysql-manager-0.58/samples/scripts,rpm包(yum安装)没有脚本模板
请先完成mysql一主二从的搭建,完成mha-manage与node的安装
1.添加故障切换脚本
vim /app/tools/masterha/app1.cnf
#故障切换VIP脚本
master_ip_failover_script= /app/tools/masterha/master_ip_failover
#在线切换VIP脚本
master_ip_online_change_script= /app/tools/masterha/master_ip_online_changeMHA提供了故障切换脚本模板master_ip_failover
2.修改脚本
修改master_ip_failover
cp /app/tools/masterha/app1/master_ip_failover.pl \
/app/tools/masterha/app1/master_ip_failovervim /app/tools/masterha/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
#添加或修改如下内容
#指定VIP
my $vip = '192.168.179.200/24';
my $key = '0';
#这里可能需要修改网卡
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
sub stop_vip() {
return 0 unless ($ssh_user);
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}修改master_ip_online_change
#太长了,其他略,基本与master_ip_failover修改的内容相同
my $vip = '192.168.179.200/24';
my $key = '0';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip;/usr/sbin/arping -q -A -c 1 -I ens33 $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";3.添加权限
#修改权限
chmod a+x /app/tools/masterha/master_ip_failover4.master手动加VIP
#master节点执行
#这里的0是key,192.168.179.200/24是VIP
ifconfig ens33:0 192.168.179.200/24
ip a |grep ens335.测试
#测试ssh,失败请参考MHA实践(1)
masterha_check_ssh --conf=/app/tools/masterha/app1.cnf
#测试mysql账户,失败请参考MHA实践(1)
masterha_check_repl --conf=/app/tools/masterha/app1.cnf 6.重启mha
nohup masterha_manager --conf=/app/tools/masterha/app1.cnf \
&>/app/tools/masterha/app1/manager.log &7.停止旧master
systemctl stop mysqld8.检查新master的VIP
ip a |grep ens33其他
在线切换
#相较于没有配置VIP切换脚本时,执行该命令会同时切换VIP masterha_master_switch --master_state=alive \ --conf=/app/tools/masterha/app1.cnf --new_master_host=192.168.179.193 \ --orig_master_is_new_slave --running_updates_limit=1000 --interactive=0
错误
- 脑裂


发现出现脑裂现象,MHA并没有把原来的旧master的VIP去除导致切换主从后有两个VIP - 原因
经过大量测试与日志检测,发现是脚本问题,删除mysql时的grep过滤范围过大,将mhamysql也删除了,虽然后面我发现了这个问题,又安装了一遍,但是少安装了T3的(新master)上的mha-node节点
虽然没有对脚本仔细研究,不知道为什么少安装新master节点上的node会导致已经安装node的旧master没有删除VIP,甚至mha检测时也没有报错(猜测这个工作可能是node来做的),但是重置后为每个节点安装上node就不会导致脑裂 - 解决
为每个节点安装mha-node
Keepalived方式
需要说明的是,使用Keepalived也需要MHA的脚本,上一个方式是通过ifconfig添加VIP的,现在通过keepalived添加VIP
请先完成mysql一主二从的搭建,完成mha-manage与node的安装
1.配置keepalived
yum install -y popt-devel gcc
mkdir -p /app/tools/keepalived
wget https://www.keepalived.org/software/keepalived-2.3.1.tar.gz
tar zxvf keepalived-2.3.1.tar.gz
cd keepalived-2.3.1
./configure --prefix=/app/tools/keepalived/
make
make install
#安装路径为/app/tools/keepalived/经测试,centos7.9不支持通过该方式安装2.3.2版本(可能要安装其他依赖)
2.修改配置文件keepalived(每个节点都要)
vim /app/tools/keepalived/keepalived.conf
global_defs {
#本节点标识
router_id T1
}
vrrp_instance vrrptest {
#都设为BACKUP且开启非抢占模式防止mysql服务波动
state BACKUP
nopreempt
interface ens33
#虚拟路由的标志,一个集群的必须相同
virtual_router_id 51
#优先级
priority 150
#服务器之间的存活检查时间
advert_int 1
authentication {
#认证类型
auth_type PASS
#认证密码,一个集群的必须相同
auth_pass 1111
}
#VIP
virtual_ipaddress {
192.168.179.200
}
}
#不配置vrrp_script,因为检测的工作由MHA做,停止本机的VIP也由MHA完成建议配置为都是BACKUP的非抢占模式,如果出现master那么非抢占模式就会失效(即使添加了nopreempt非抢占),所以必须都是BACKUP,优先级建议相同
当然由于是由MHA启动keepalived,同一时间只会有一个keepalived启动,所以即使配置了抢占或者配置了MASTER也不会影响,但是为了防止MHA出问题导致出现脑裂或者VIP漂错位置,还是建议按照非抢占配置keepalived
3.修改master_ip_failover
my $vip = '192.168.192.200/24';
my $ssh_start_vip ="systemctl start keepalived";
my $ssh_stop_vip ="systemctl stop keepalived";4.修改修改master_ip_online_change
my $vip = '192.168.192.200/24';
my $ssh_start_vip ="systemctl start keepalived";
my $ssh_stop_vip ="systemctl stop keepalived";5.添加权限
#修改权限
chmod a+x /app/tools/masterha/master_ip_failover6.master手动加VIP
注意,只要master开启(如果当前VIP在T1,关闭master后,都开启keepalived后因为设置优先级相同,VIP随机漂移,不一定飘到新的master上,所以必须由MHA开启)
#master节点执行
systemctl start keepalived
ip a |grep ens337.测试
masterha_check_ssh --conf=/app/tools/masterha/app1.cnf
masterha_check_repl --conf=/app/tools/masterha/app1.cnf 8.重启mha
nohup masterha_manager --conf=/app/tools/masterha/app1.cnf &>/app/tools/masterha/app1/manager.log &9.停止旧master
systemctl stop mysqld10.检查新master的VIP
ip a |grep ens33错误
1.配置错误
- 错误
keepalive启动后,没有VIP,即使单点启动也没有 - 原因
经过仔细检查,发现是配置文件中少了个},这个错误keepalived竟然不报错 - 解决
修改配置文件




