MHA集羣(gtid複製)和vip漂移

上一片博客中,講述了怎麼去配置MHA架構!這片博客再也不細說,只說明其中MySQL主從搭建,這裏使用的是gtid加上半同步複製!html

步驟與上一片博客同樣,不一樣之處在於MySQL主從的搭建!詳細的gtid搭建過程http://www.javashuo.com/article/p-vizxpzgy-k.html前端

上一片博客中,把MySQL主從的搭建由filename和pos的過程改變爲以下的基於gtid的過程就能夠,所以再也不詳細說明,只展現gtid的搭建!mysql

四臺服務器分配以下:sql

MHA管理節點: 10.0.102.214數據庫

MySQL主節點:   10.0.102.204 服務器

MySQL從節點1:  10.0.102.179 【這個節點能夠做爲備用的主】架構

MySQL從節點2: 10.0.102.221app

搭建基於gtid的數據庫複製!

第一步:保證三個服務器的數據是一致的【處於一致的狀態】less

第二步:在主上和備用的主上建立複製帳戶,用戶名和密碼要保持一致!ssh

第三步:開啓gtid,加載半同步複製的插件!

三臺服務器中配置文件加入如下參照:

plugin_dir=/usr/local/mysql/lib/plugin/               #由於這裏MySQL5.7是源碼安裝的位置,如果使用rpm包安裝,則注意更改位置
plugin_load=semisync_master.so                        #這兩個插件儘可能在每一個服務器都安裝吧
plugin_load=semisync_slave.so
gtid-mode=on                                          #開啓gtid
enforce-gtid-consistency                              #確保gtid全局的一致性
log-bin=
character_set_server=utf8                             #設置字符集
log_slave_updates                                     #gtid複製時,必定要開啓

設置完配置文件以後,重啓服務器,而後再從上執行如下命令!

mysql> change master to master_host="10.0.102.204", master_user="repl",master_password="123456",master_auto_position = 1;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

若上面沒有報錯,則使用show slave status查看複製的狀態!

檢查MHA的狀態

ssh狀態檢查:

[root@test3 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf 
Sun Dec  9 11:42:50 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Dec  9 11:42:50 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:42:50 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:42:50 2018 - [info] Starting SSH connection tests..
Sun Dec  9 11:42:51 2018 - [debug] 
Sun Dec  9 11:42:50 2018 - [debug]  Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.179(10.0.102.179:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.221(10.0.102.221:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug] 
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.204(10.0.102.204:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.221(10.0.102.221:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [debug] 
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.204(10.0.102.204:22)..
Sun Dec  9 11:42:52 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [debug]  Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.179(10.0.102.179:22)..
Sun Dec  9 11:42:52 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [info] All SSH connection tests passed successfully.
masterha_check_ssh --conf=/etc/masterha/app1.cnf
[root@test3 ~]# masterha_check_ssh --conf=/etc/masterha/app1.cnf 
Sun Dec  9 11:42:50 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Dec  9 11:42:50 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:42:50 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:42:50 2018 - [info] Starting SSH connection tests..
Sun Dec  9 11:42:51 2018 - [debug] 
Sun Dec  9 11:42:50 2018 - [debug]  Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.179(10.0.102.179:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.204(10.0.102.204:22) to root@10.0.102.221(10.0.102.221:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug] 
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.204(10.0.102.204:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.179(10.0.102.179:22) to root@10.0.102.221(10.0.102.221:22)..
Sun Dec  9 11:42:51 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [debug] 
Sun Dec  9 11:42:51 2018 - [debug]  Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.204(10.0.102.204:22)..
Sun Dec  9 11:42:52 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [debug]  Connecting via SSH from root@10.0.102.221(10.0.102.221:22) to root@10.0.102.179(10.0.102.179:22)..
Sun Dec  9 11:42:52 2018 - [debug]   ok.
Sun Dec  9 11:42:52 2018 - [info] All SSH connection tests passed successfully.
[root@test3 ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf 
Sun Dec  9 11:43:39 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Dec  9 11:43:39 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:43:39 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Dec  9 11:43:39 2018 - [info] MHA::MasterMonitor version 0.56.
Sun Dec  9 11:43:39 2018 - [info] GTID failover mode = 1
Sun Dec  9 11:43:39 2018 - [info] Dead Servers:
Sun Dec  9 11:43:39 2018 - [info] Alive Servers:
Sun Dec  9 11:43:39 2018 - [info]   10.0.102.204(10.0.102.204:3306)
Sun Dec  9 11:43:39 2018 - [info]   10.0.102.179(10.0.102.179:3306)
Sun Dec  9 11:43:39 2018 - [info]   10.0.102.221(10.0.102.221:3306)
Sun Dec  9 11:43:39 2018 - [info] Alive Slaves:
Sun Dec  9 11:43:39 2018 - [info]   10.0.102.179(10.0.102.179:3306)  Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
Sun Dec  9 11:43:39 2018 - [info]     GTID ON
Sun Dec  9 11:43:39 2018 - [info]     Replicating from 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 11:43:39 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Sun Dec  9 11:43:39 2018 - [info]   10.0.102.221(10.0.102.221:3306)  Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
Sun Dec  9 11:43:39 2018 - [info]     GTID ON
Sun Dec  9 11:43:39 2018 - [info]     Replicating from 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 11:43:39 2018 - [info]     Not candidate for the new Master (no_master is set)
Sun Dec  9 11:43:39 2018 - [info] Current Alive Master: 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 11:43:39 2018 - [info] Checking slave configurations..
Sun Dec  9 11:43:39 2018 - [info]  read_only=1 is not set on slave 10.0.102.179(10.0.102.179:3306).
Sun Dec  9 11:43:39 2018 - [info]  read_only=1 is not set on slave 10.0.102.221(10.0.102.221:3306).
Sun Dec  9 11:43:39 2018 - [info] Checking replication filtering settings..
Sun Dec  9 11:43:39 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
Sun Dec  9 11:43:39 2018 - [info]  Replication filtering check ok.
Sun Dec  9 11:43:39 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Sun Dec  9 11:43:39 2018 - [info] Checking SSH publickey authentication settings on the current master..
Sun Dec  9 11:43:39 2018 - [info] HealthCheck: SSH to 10.0.102.204 is reachable.
Sun Dec  9 11:43:39 2018 - [info] 
10.0.102.204(10.0.102.204:3306) (current master)
 +--10.0.102.179(10.0.102.179:3306)
 +--10.0.102.221(10.0.102.221:3306)

Sun Dec  9 11:43:39 2018 - [info] Checking replication health on 10.0.102.179..
Sun Dec  9 11:43:39 2018 - [info]  ok.
Sun Dec  9 11:43:39 2018 - [info] Checking replication health on 10.0.102.221..
Sun Dec  9 11:43:39 2018 - [info]  ok.
Sun Dec  9 11:43:39 2018 - [info] Checking master_ip_failover_script status:
Sun Dec  9 11:43:39 2018 - [info]   /usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.204 --orig_master_ip=10.0.102.204 --orig_master_port=3306 


IN SCRIPT TEST====service keepalived stop==service keepalived start===

Checking the Status of the script.. OK 
Sun Dec  9 11:43:39 2018 - [info]  OK.
Sun Dec  9 11:43:39 2018 - [warning] shutdown_script is not defined.
Sun Dec  9 11:43:39 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
[root@test3 

複製狀態檢查:

Sun Dec  9 12:14:28 2018 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sun Dec  9 12:14:28 2018 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sun Dec  9 12:14:28 2018 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sun Dec  9 12:14:28 2018 - [info] MHA::MasterMonitor version 0.56.
Sun Dec  9 12:14:28 2018 - [info] GTID failover mode = 1
Sun Dec  9 12:14:28 2018 - [info] Dead Servers:
Sun Dec  9 12:14:28 2018 - [info] Alive Servers:
Sun Dec  9 12:14:28 2018 - [info]   10.0.102.204(10.0.102.204:3306)
Sun Dec  9 12:14:28 2018 - [info]   10.0.102.179(10.0.102.179:3306)
Sun Dec  9 12:14:28 2018 - [info]   10.0.102.221(10.0.102.221:3306)
Sun Dec  9 12:14:28 2018 - [info] Alive Slaves:
Sun Dec  9 12:14:28 2018 - [info]   10.0.102.179(10.0.102.179:3306)  Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
Sun Dec  9 12:14:28 2018 - [info]     GTID ON
Sun Dec  9 12:14:28 2018 - [info]     Replicating from 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 12:14:28 2018 - [info]     Primary candidate for the new Master (candidate_master is set)
Sun Dec  9 12:14:28 2018 - [info]   10.0.102.221(10.0.102.221:3306)  Version=5.7.22-log (oldest major version between slaves) log-bin:enabled
Sun Dec  9 12:14:28 2018 - [info]     GTID ON
Sun Dec  9 12:14:28 2018 - [info]     Replicating from 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 12:14:28 2018 - [info]     Not candidate for the new Master (no_master is set)
Sun Dec  9 12:14:28 2018 - [info] Current Alive Master: 10.0.102.204(10.0.102.204:3306)
Sun Dec  9 12:14:28 2018 - [info] Checking slave configurations..
Sun Dec  9 12:14:28 2018 - [info]  read_only=1 is not set on slave 10.0.102.179(10.0.102.179:3306).
Sun Dec  9 12:14:28 2018 - [info]  read_only=1 is not set on slave 10.0.102.221(10.0.102.221:3306).
Sun Dec  9 12:14:28 2018 - [info] Checking replication filtering settings..
Sun Dec  9 12:14:28 2018 - [info]  binlog_do_db= , binlog_ignore_db= 
Sun Dec  9 12:14:28 2018 - [info]  Replication filtering check ok.
Sun Dec  9 12:14:28 2018 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Sun Dec  9 12:14:28 2018 - [info] Checking SSH publickey authentication settings on the current master..
Sun Dec  9 12:14:28 2018 - [info] HealthCheck: SSH to 10.0.102.204 is reachable.
Sun Dec  9 12:14:28 2018 - [info] 
10.0.102.204(10.0.102.204:3306) (current master)
 +--10.0.102.179(10.0.102.179:3306)
 +--10.0.102.221(10.0.102.221:3306)

Sun Dec  9 12:14:28 2018 - [info] Checking replication health on 10.0.102.179..
Sun Dec  9 12:14:28 2018 - [info]  ok.
Sun Dec  9 12:14:28 2018 - [info] Checking replication health on 10.0.102.221..
Sun Dec  9 12:14:28 2018 - [info]  ok.
Sun Dec  9 12:14:28 2018 - [info] Checking master_ip_failover_script status:
Sun Dec  9 12:14:28 2018 - [info]   /usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.204 --orig_master_ip=10.0.102.204 --orig_master_port=3306 


IN SCRIPT TEST====service keepalived stop==service keepalived start===

Checking the Status of the script.. OK 
Sun Dec  9 12:14:28 2018 - [info]  OK.
Sun Dec  9 12:14:28 2018 - [warning] shutdown_script is not defined.
Sun Dec  9 12:14:28 2018 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.
masterha_check_repl --conf=/etc/masterha/app1.cnf

複製狀態信息中會顯示gtid的信息提示!

若上面兩步檢查都沒有錯誤,能夠開啓MHA的監控!

nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover &

查看MHA的運行狀態:

[root@test3 ~]# masterha_check_status --conf=/etc/masterha/app1.cnf 
app1 (pid:22124) is running(0:PING_OK), master:10.0.102.204

#顯示app1正在運行,集羣中主服務器爲10.0.102.204

這時候MHA集羣已經搭建完畢,只要停掉主服務器,就會自動選擇備用的從做爲主服務器的!

 

配置VIP漂移

目前集羣master是204這臺服務器,想一個場景,前端應用正在鏈接這臺數據庫,而後master服務器因某種緣由宕掉了,這時候在MHA看來,咱們能夠把備用的主也就是179這臺服務器做爲新的master,維持集羣的正常運行;可是對於前端應用來講,不可能集羣沒宕一次就修改一次源碼中的數據庫的IP地址。咱們須要一個VIP來和前端相連,這個VIP老是指向正常運行數據庫服務器!

MHA引入VIP有兩種方式,一種是使用keepalive,另外一種是使用MHA自帶的腳本!

使用keepalive配置VIP漂移

按照keepalive軟件,在當前的主和要做爲備份的主上,按照keepalive!

#直接yum按照便可
yum install -y keepalived

而後編輯配置文件,yum安裝默認配置在/etc/下面

#當前主服務器的配置文件
cat
/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id MYSQL_HA } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.102.110 } }
#當前做爲備份主的服務器的配置文件
cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id MYSQL_HA } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.102.110 } }

注意:上面兩臺服務器的keepalived都設置爲了BACKUP模式,在keepalived中2種模式,分別是master->backup模式和backup->backup模式。這兩種模式有很大區別。在master->backup模式下,一旦主庫宕機,虛擬ip會自動漂移到從庫,當主庫修復後,keepalived啓動後,還會把虛擬ip搶佔過來,即便設置了非搶佔模式(nopreempt)搶佔ip的動做也會發生。在backup->backup模式下,當主庫宕機後虛擬ip會自動漂移到從庫上,當原主庫恢復和keepalived服務啓動後,並不會搶佔新主的虛擬ip,即便是優先級高於從庫的優先級別,也不會發生搶佔。爲了減小ip漂移次數,一般是把修復好的主庫當作新的備庫。

keepalive配置完成以後,啓動keepalive,能夠先測試keepalive是否會完成VIP的漂移!

 

keepalive配置完成以後,能夠設置failover腳本!

須要注意的是,這裏咱們要測試VIP漂移,所以須要指定failover的腳本位置,在配置文件中指定!

[root@test3 masterha]# cat masterha_default.cnf 
[server default]
user=root
password=123456
ssh_user=root
ssh_port=22
ping_interval=3
repl_user=repl
repl_password=123456

master_binlog_dir= /data/mysql/

remote_workdir=/data/log/masterha

secondary_check_script= masterha_secondary_check -s test1 -s mgt01 --user=root --port=22 --master_host=test2 --master_port=3306


#failover參數位置
master_ip_failover_script
= /usr/local/bin/master_ip_failover --ssh_user=root # shutdown_script= /script/masterha/power_manager #report_script= /usr/local/bin/send_report
#在線修改腳步位置, master_ip_online_change_script
= /script/masterha/master_ip_online_change --ssh_user=root

failover腳步須要本身編輯,在網上找了http://www.ywnds.com/?p=8116   這片博客中給出的failover腳步,測試的時候有點問題,MHA得不到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
);
 
my $ssh_start_vip = "service keepalived start";
#my $ssh_start_vip = "systemctl start keepalived.service";
#my $ssh_stop_vip = "systemctl stop keepalived.service";
my $ssh_stop_vip = "service keepalived stop";
 
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 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() {
     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";
}
cat master_ip_failover

配置了腳本以後,再次檢查複製狀態:

masterha_check_repl --conf=/etc/masterha/app1.cnf

若複製狀態檢查中,顯示failover錯誤的信息,能夠單獨執行下面的命令,查看腳本的狀態及報錯提示!

/usr/local/bin/master_ip_failover --ssh_user=root --command=status --ssh_user=root --orig_master_host=10.0.102.179 --orig_master_ip=10.0.102.179 --orig_master_port=3306

開啓MHA的監控狀態,若正常啓動沒有報錯,則說明配置完成!

當前的狀態是:

204的服務器是當前的master,而且有VIP。

179服務器上是備用的主。

221服務器僅是從!

停掉204的數據庫服務,查看vip是否會轉移到179上,集羣的主服務器是否也會轉到179上?

停掉204的數據庫

[root@test2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:1d:ae:12:52:00 brd ff:ff:ff:ff:ff:ff
    inet 10.0.102.204/22 brd 10.0.103.255 scope global eth0
    inet 10.0.102.110/32 scope global eth0
    inet6 fe80::f81d:aeff:fe12:5200/64 scope link 
       valid_lft forever preferred_lft forever
[root@test2 ~]# service mysqld stop              #停掉數據庫
Shutting down MySQL............ SUCCESS! 
[root@test2 ~]# ip addr #能夠看到VIP已經不在204上了
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:1d:ae:12:52:00 brd ff:ff:ff:ff:ff:ff
    inet 10.0.102.204/22 brd 10.0.103.255 scope global eth0
    inet6 fe80::f81d:aeff:fe12:5200/64 scope link
       valid_lft forever preferred_lft forever
[root@test2 ~]#

查看179服務器的VIP:

#能夠看到VIP已經漂移,而且179服務器已經變爲master服務器!
[root@test1 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether fa:bc:66:8d:2e:00 brd ff:ff:ff:ff:ff:ff inet 10.0.102.179/22 brd 10.0.103.255 scope global eth0 inet 10.0.102.110/32 scope global eth0 inet6 fe80::f8bc:66ff:fe8d:2e00/64 scope link valid_lft forever preferred_lft forever [root@test1 ~]# mysql -e "show processlist;" +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+ | 15 | repl | mgt01:2735 | NULL | Binlog Dump GTID | 135 | Master has sent all binlog to slave; waiting for more updates | NULL | | 16 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+------+------------+------+------------------+------+---------------------------------------------------------------+------------------+

還能夠MHA的日誌:

cat /data/log/app1/manager.log

上面使用keepalive來作高可用,完成了VIP的漂移的測試,下面使用MHA自帶的腳原本測試一下!

使用自帶腳本配置VIP漂移

須要修改master_ip_failover腳本。

注意修改腳本中的VIP地址,以及ifconfig命令的絕對路徑!

#!/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 = '10.0.102.110/22';
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";
}
cat /usr/local/bin/master_ip_failover

注意要給腳本加上可執行權限

chmod +x master_ip_failover 

而後修改配置文件:

[server default]
manager_log=/data/log/app1/manager.log
manager_workdir=/data/log/app1
master_binlog_dir=/data/mysql
master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_failover
password=123456
ping_interval=3
remote_workdir=/data/log/masterha
repl_password=123456
repl_user=repl
report_script=/usr/local/bin/send_report
secondary_check_script=masterha_secondary_check -s test1 -s mgt01 --user=root --port=22 --master_host=test2 --master_port=3306
ssh_port=22
ssh_user=root
user=root

[server1]
candidate_master=1
hostname=10.0.102.204
port=3306

[server2]
candidate_master=1 hostname=10.0.102.179 port=3306

[server3] hostname=10.0.102.221 no_master=1 port=3306

而後進行repl測試,若測試經過,在啓動MHA監控!

使用自帶的腳本作failover須要手動添加虛擬IP,在當前的master上添加VIP!

[root@test1 ~]# ifconfig eth0:0 10.0.102.110/22                              #添加虛擬IP
[root@test1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr FA:BC:66:8D:2E:00  
          inet addr:10.0.102.179  Bcast:10.0.103.255  Mask:255.255.252.0
          inet6 addr: fe80::f8bc:66ff:fe8d:2e00/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3861480 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1279028 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:7747611623 (7.2 GiB)  TX bytes:2717743084 (2.5 GiB)

eth0:0    Link encap:Ethernet  HWaddr FA:BC:66:8D:2E:00  
          inet addr:10.0.102.110  Bcast:10.0.103.255  Mask:255.255.252.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2742 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2742 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:370558 (361.8 KiB)  TX bytes:370558 (361.8 KiB)

這是179的服務器上即有虛擬IP,也是MySQL集羣的主服務器!而後停掉MySQL服務,查看VIP的漂移和主從的切換!

[root@test1 ~]# service mysqld stop                          #停掉主服務器,虛擬IP已經漂移
Shutting down MySQL............ SUCCESS! 
[root@test1 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr FA:BC:66:8D:2E:00  
          inet addr:10.0.102.179  Bcast:10.0.103.255  Mask:255.255.252.0
          inet6 addr: fe80::f8bc:66ff:fe8d:2e00/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3864276 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1280993 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:7747800974 (7.2 GiB)  TX bytes:2717898739 (2.5 GiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:2744 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2744 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:370642 (361.9 KiB)  TX bytes:370642 (361.9 KiB)

在備用主服務器204上查看:

[root@test2 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:1d:ae:12:52:00 brd ff:ff:ff:ff:ff:ff
    inet 10.0.102.204/22 brd 10.0.103.255 scope global eth0
    inet 10.0.102.110/22 brd 10.0.103.255 scope global secondary eth0:0
    inet6 fe80::f81d:aeff:fe12:5200/64 scope link 
       valid_lft forever preferred_lft forever
[root@test2 ~]# mysql -e "show processlist"
+----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
| Id | User | Host               | db   | Command          | Time | State                                                         | Info             |
+----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
| 19 | repl | 10.0.102.221:52375 | NULL | Binlog Dump GTID | 1860 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 20 | root | localhost          | NULL | Query            |    0 | starting                                                      | show processlist |
+----+------+--------------------+------+------------------+------+---------------------------------------------------------------+------------------+
[root@test2 ~]# 

能夠看到虛擬IP已經漂移,而且主從已經切換!

相關文章
相關標籤/搜索