手机版
你好,游客 登录 注册
背景:
阅读新闻

MHA高可用部署及测试

[日期:2014-11-17] 来源:Linux社区  作者:Linux [字体:大 中 小]

手动恢复node1操作:

  rm -rf /var/log/mha/app1/app1.failover.complete

  启动node1上的mysql,重新配置node2 node3 主从指向node1(change master to)

MHA Manager后台执行:

nohup masterha_manager –conf=/etc/mha/app1/app1.cnf < /dev/null > /var/log/mha/app1/app1.log 2>&1 &
守护进程方式参考:
https://code.google.com/p/mysql-master-ha/wiki/Runnning_Background

ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/openSUSE/repositories/home:/weberho:/qmailtoaster/openSUSE_Tumbleweed/x86_64/daemontools-0.76-5.3.x86_64.rpm
 
4.5 配置VIP的方式

A.通过全局配置文件实现

vim /etc/mha/masterha_default.cnf

[server default]
  user=root
  password=geekwolf
  ssh_user=root
  repl_user=rep
  repl_password=geekwolf
  ping_interval=1
  secondary_check_script = masterha_secondary_check -s node1 -s node2 -s node3 --user=root --master_host=node1 --master_ip=192.168.10.216 --master_port=3306
  master_ip_failover_script="/etc/mha/scripts/master_ip_failover"
  master_ip_online_change_script="/etc/mha/scripts/master_ip_online_change"
  #shutdown_script= /script/masterha/power_manager
  #report_script=""
 

修改后的master_ip_failover、master_ip_online_change脚本


 
#!/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
);
my $vip = '192.168.10.218'; # Virtual IP
my $gateway = '192.168.10.1';#Gateway IP
my $interface = 'eth0'
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$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" ) {
        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
        # If you manage master ip address at global catalog database,
        # invalidate orig_master_ip here.
        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" ) {
        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        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";
        `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    `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";
}
 

B.通过第三方HA(keepalived、heartbeat)实现VIP,以keepalived为例

以node1 node2互为主备进行配置keepalived

在node1 node2上分别下载安装keepalived

wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz

yum -y install popt-*

./configure –prefix=/usr/local/keepalived –enable-snmp

make && make install

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

chmod +x /etc/rc.d/init.d/keepalived

chkconfig keepalived on

mkdir /etc/keepalived

ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin


修改node1(192.168.10.216)配置文件

vim /etc/keepalived/keepalived.conf


 
! Configuration File for keepalived
global_defs {
 router_id MHA
 notification_email {
 root@localhost  #接收邮件,可以有多个,一行一个
}
 #当主、备份设备发生改变时,通过邮件通知
 notification_email_from  m@localhost
 #发送邮箱服务器
 smtp_server 127.0.0.1
 #发送邮箱超时时间
 smtp_connect_timeout 30
 }
 
varrp_script check_mysql {
    script "/etc/keepalived/check_mysql.sh"
}
vrrp_sync_group VG1 {
    group {
          VI_1
    }
notify_master "/etc/keepalived/master.sh"
}
 
vrrp_instance VI_1 {
    state master   
    interface eth0 
    virtual_router_id 110
    priority 100         
    advert_int 1
    nopreempt #不抢占资源,意思就是它活了之后也不会再把主抢回来
 
    authentication {
    # 认证方式,可以是PASS或AH两种认证方式
    auth_type PASS
    # 认证密码
    auth_pass geekwolf
    }
track_script {
    check_mysql
}
virtual_ipaddress {
    192.168.10.219
    }
 
}
 

修改node2(192.168.10.217)配置文件

vim /etc/keepalived/keepalived.conf

 
! Configuration File for keepalived
global_defs {
 router_id MHA
 notification_email {
 root@localhost  #接收邮件,可以有多个,一行一个
}
 #当主、备份设备发生改变时,通过邮件通知
 notification_email_from  m@localhost
 #发送邮箱服务器
 smtp_server 127.0.0.1
 #发送邮箱超时时间
 smtp_connect_timeout 30
 }
 
varrp_script check_mysql {
    script "/etc/keepalived/check_mysql.sh"
}
vrrp_sync_group VG1 {
    group {
          VI_1
    }
notify_master "/etc/keepalived/master.sh"
}
vrrp_instance VI_1 {
    state backup   
    interface eth0   
    virtual_router_id 110
    priority 99         
    advert_int 1
 
    authentication {
    # 认证方式,可以是PASS或AH两种认证方式
    auth_type PASS
    # 认证密码
    auth_pass geekwolf
    }
track_script {
    check_mysql
}
virtual_ipaddress {
    192.168.10.219
    }
 
}
 

check_mysql.sh

 


#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWORD=geekwolf
CHECK_TIME=3
#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
    MYSQL_OK=1
else
    MYSQL_OK=0
fi
    return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
    let "CHECK_TIME -= 1"
    check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
    CHECK_TIME=0
    exit 0
fi
if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]
then
    pkill keepalived
exit 1
fi
sleep 1
done
 

master.sh


 
#!/bin/bash
VIP=192.168.10.219
GATEWAY=1.1
/sbin/arping -I eth0 -c 5 -s $VIP $GATEWAY &>/dev/null
 

chmod +x /etc/keepalived/check_mysql.sh

chmod +x /etc/keepalived/master.sh
 
五.MHA常用命令

查看manager状态

masterha_check_status –conf=/etc/mha/app1/app1.cnf

查看免密钥是否正常

masterha_check_ssh –conf=/etc/mha/app1/app1.cnf

查看主从复制是否正常

masterha_check_repl –conf=/etc/mha/app1/app1.cnf

添加新节点server4到配置文件

masterha_conf_host –command=add –conf=/etc/mha/app1/app1.cnf –hostname=geekwolf –block=server4 –params=“no_master=1;ignore_fail=1”
删除server4节点

masterha_conf_host –command=delete –conf=/etc/mha/app1/app1.cnf –block=server4

注:

block:为节点区名,默认值 为[server_$hostname],如果设置成block=100,则为[server100]
params:参数,分号隔开(参考https://code.google.com/p/mysql-master-ha/wiki/Parameters)

关闭manager服务

masterha_stop –conf=/etc/mha/app1/app1.cnf

主手动切换(前提不要启动masterha_manager服务)

在主node1存活情况下进行切换

交互模式:

masterha_master_switch –master_state=alive –conf=/etc/mha/app1/app1.cnf –new_master_host=node2

非交互模式:

masterha_master_switch –master_state=alive –conf=/etc/mha/app1/app1.cnf –new_master_host=node2 –interactive=0

在主node1宕掉情况下进行切换

masterha_master_switch –master_state=dead –conf=/etc/mha/app1/app1.cnf –dead_master_host=node1 –dead_master_ip=192.168.10.216 –dead_master_port=3306 –new_master_host=192.168.10.217
详细请参考:https://code.google.com/p/mysql-master-ha/wiki/TableOfContents?tm=6
*
 
六.注意事项


A. 以上两种vip切换方式,建议采用第一种方法

B. 发生主备切换后,manager服务会自动停掉,且在/var/log/mha/app1下面生成
app1.failover.complete,若再次发生切换需要删除app1.failover.complete文件

C. 测试过程发现一主两从的架构(两从都设置可以担任主角色candidate_master=1),当旧主故障迁移到备主后,删除app1.failover.complete,再次启动manager,停掉新主后,发现无法正常切换(解决方式:删除/etc/mha/app1/app1.cnf里面的旧主node1的信息后,重新切换正常)

D. arp缓存导致切换VIP后,无法使用问题;keepalived已经内部实现可不必使用master.sh

E. 使用Semi-Sync能够最大程度保证数据安全

F. Purge_relay_logs脚本删除中继日志不会阻塞SQL线程,在每台从节点上设置计划任务定期清除中继日志

  0 5 * * * root /usr/bin/purge_relay_logs –user=root –password=geekwolf –disable_relay_log_purge » /var/log/mha/purge_relay_logs.log 2>&1
 
七.部署过程遇到的问题

问题1:
[root@node1 mha4mysql-node-0.56]# perl Makefile.PL

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at inc/Module/Install/Makefile.pm line 4.

BEGIN failed–compilation aborted at inc/Module/Install/Makefile.pm line 4.
Compilation failed in require at inc/Module/Install.pm line 283.

Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: inc /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/
vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at inc/Module/Install/Can.pm line 6.

BEGIN failed–compilation aborted at inc/Module/Install/Can.pm line 6.

Compilation failed in require at inc/Module/Install.pm line 283.

Can't locate ExtUtils/MM_Unix.pm in @INC (@INC contains: inc /usr/local/lib64/
perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at inc/Module/Install/
Metadata.pm line 349.

解决办法:

yum -y install perl-CPAN perl-devel perl-DBD-MySQL

问题2:

Can't locate Time/HiRes.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at /usr/local/share/perl5/MHA/SSHCheck.pm line 28.

BEGIN failed–compilation aborted at /usr/local/share/perl5/MHA/SSHCheck.pm line 28.

Compilation failed in require at /usr/local/bin/masterha_check_ssh line 25.
BEGIN failed–compilation aborted at /usr/local/bin/masterha_check_ssh line 25.

解决办法:

yum -y install perl-Time-HiRes

每个节点都做好mysql命令的软链

ln -s /usr/local/mysql/bin/* /usr/local/bin/

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/109460.htm

linux
相关资讯       MHA 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款