MHA VIP切換指令碼,mhavip切換
MHA能夠在10~30秒內實現自動故障檢測和容錯移轉,適用於對高可用性,資料完整性要求較高的場合。要做到無縫切換,還需要依賴於VIP漂移。VIP漂移比較常用的方式為使用keepalived或者使用指令碼直接實現。指令碼方式無須安裝及複雜配置,相對簡單。本文描述了基於指令碼實現VIP切換。
對於keepalived的相關配置可以參考:CentOS 5.9下安裝配置keepalived
1、當前主機環境及MHA配置
[root@vdbsrv1 ~]# more /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.1.6 vdbsrv1 #master
192.168.1.7 vdbsrv2 #slave1
192.168.1.8 vdbsrv3 #slave2
192.168.1.12 vdbsrv4 #manager
###os環境
[root@vdbsrv4 ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
###mysql環境
[root@vdbsrv4 ~]# mysql -e "show variables like 'version'"
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.22-log |
+---------------+------------+
[root@vdbsrv4 ~]# masterha_manager --version
masterha_manager version 0.56.
###MHA配置資訊
[root@vdbsrv4 ~]$ more /etc/masterha/app1.cnf
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log
user=mha
password=xxx
ssh_user=root
repl_user=repl
repl_password=repl
ping_interval=1
shutdown_script=""
master_ip_online_change_script=""
report_script=""
master_ip_failover_script=/tmp/master_ip_failover
[server1]
hostname=vdbsrv1
master_binlog_dir=/data/mysqldata
[server2]
hostname=vdbsrv2
master_binlog_dir=/data/mysqldata
[server3]
hostname=vdbsrv3
master_binlog_dir=/data/mysqldata/
#candidate_master=1
2、測試VIP切換
###測試VIP(192.168.1.13)是否被啟用
[root@vdbsrv4 ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
From 192.168.1.12 icmp_seq=10 Destination Host Unreachable
###為主機vdbsrv1添加VIP
[root@vdbsrv4 ~]# ssh vdbsrv1 "/sbin/ifconfig eth0:0 192.168.1.13 netmask 255.255.255.0 up"
###校正VIP是否成功啟用
[root@vdbsrv4 ~]# ping 192.168.1.13
PING 192.168.1.13 (192.168.1.13) 56(84) bytes of data.
64 bytes from 192.168.1.13: icmp_seq=1 ttl=64 time=1.82 ms
###開啟MHA
[root@vdbsrv4 ~]# masterha_manager --conf=/etc/masterha/app1.cnf &
###類比主庫宕機
[root@vdbsrv4 ~]# ssh vdbsrv1 "killall -r mysqld"
###查看管理節點日誌,可以看到VIP已經漂移
[root@vdbsrv4 ~]# grep VIP /var/log/masterha/app1/manager.log
Disabling the VIP on old master: vdbsrv1
Enabling the VIP - 192.168.1.13/24 on the new master - vdbsrv2
###驗證VIP是否位於節點vdbsrv2
[root@vdbsrv4 ~]# ssh vdbsrv2 "ifconfig |grep 1.13 -B1"
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:5F:B2:EB
inet addr:192.168.1.13 Bcast:192.168.1.255 Mask:255.255.255.0
######查看管理節點MHA切換日誌
[root@vdbsrv4 ~]# tail /var/log/masterha/app1/manager.log
Invalidated master IP address on vdbsrv1(192.168.1.6:3306)
The latest slave vdbsrv2(192.168.1.7:3306) has all relay logs for recovery.
Selected vdbsrv2(192.168.1.7:3306) as a new master.
vdbsrv2(192.168.1.7:3306): OK: Applying all logs succeeded.
vdbsrv2(192.168.1.7:3306): OK: Activated master IP address.
vdbsrv3(192.168.1.8:3306): This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
vdbsrv3(192.168.1.8:3306): OK: Applying all logs succeeded. Slave started, replicating from vdbsrv2(192.168.1.7:3306)
vdbsrv2(192.168.1.7:3306): Resetting slave info succeeded.
Master failover to vdbsrv2(192.168.1.7:3306) completed successfully.
3、VIP切換perl指令碼
[root@vdbsrv4 app1]# more /tmp/master_ip_failover #!/usr/bin/env perluse 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.1.13/24';my $key = '0';my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";my $ssh_stop_vip = "/sbin/ifconfig eth0:$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";}