【Mysql】MHA配置虛擬ip

小亮520cl發表於2016-08-04

虛擬ip飄逸

  1. 說到Failover,通常有兩種方式:一種是虛擬IP地址,一種是全域性配置檔案。
  2. MHA並沒有限定使用哪一種方式,而是讓使用者自己選擇,虛擬IP地址的方式會牽扯到其它的軟體,這裡就不贅述了
  3. 上篇文章搭建mha+keepalive就是透過keepalive來控制虛擬ip,本片文章在介紹一種虛擬ip的方式

配置虛擬ip

  1. 採用ifconfig的方式
  2. /sbin/ifconfig eth0:1 192.168.6.66/24
刪除VIP:
  1. /sbin/ifconfig eth0:1 down
網上找了一個master_ip_failover指令碼就是用此方法更改VIP:
  1. [root@localhost app1]# more /usr/local/bin/master_ip_online_change_script
  2. #!/usr/bin/env perl
  3. use strict;
  4. use warnings FATAL => 'all';

  5. use Getopt::Long;

  6. my (
  7.     $command, $ssh_user, $orig_master_host, $orig_master_ip,
  8.     $orig_master_port, $new_master_host, $new_master_ip, $new_master_port
  9. );

  10. my $vip = '192.168.6.66/24'; # Virtual IP
  11. my $key = "1";
  12. my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
  13. my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";

  14. GetOptions(
  15.     'command=s' => \$command,
  16.     'ssh_user=s' => \$ssh_user,
  17.     'orig_master_host=s' => \$orig_master_host,
  18.     'orig_master_ip=s' => \$orig_master_ip,
  19.     'orig_master_port=i' => \$orig_master_port,
  20.     'new_master_host=s' => \$new_master_host,
  21.     'new_master_ip=s' => \$new_master_ip,
  22.     'new_master_port=i' => \$new_master_port,
  23. );

  24. exit &main();

  25. sub main {

  26.     print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";

  27.     if ( $command eq "stop" || $command eq "stopssh" ) {

  28.         # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
  29.         # If you manage master ip address at global catalog database,
  30.         # invalidate orig_master_ip here.
  31.         my $exit_code = 1;
  32.         eval {
  33.             print "Disabling the VIP on old master: $orig_master_host \n";
  34.             &stop_vip();
  35.             $exit_code = 0;
  36.         };
  37.         if ($@) {
  38.             warn "Got Error: $@\n";
  39.             exit $exit_code;
  40.         }
  41.         exit $exit_code;
  42.     }
  43.     elsif ( $command eq "start" ) {

  44.         # all arguments are passed.
  45.         # If you manage master ip address at global catalog database,
  46.         # activate new_master_ip here.
  47.         # You can also grant write access (create user, set read_only=0, etc) here.
  48.         my $exit_code = 10;
  49.         eval {
  50.             print "Enabling the VIP - $vip on the new master - $new_master_host \n";
  51.             &start_vip();
  52.             $exit_code = 0;
  53.         };
  54.         if ($@) {
  55.             warn $@;
  56.             exit $exit_code;
  57.         }
  58.         exit $exit_code;
  59.     }
  60.     elsif ( $command eq "status" ) {
  61.         print "Checking the Status of the script.. OK \n";
  62.         `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
  63.         exit 0;
  64.     }
  65.     else {
  66.         &usage();
  67.         exit 1;
  68.     }
  69. }

  70. # A simple system call that enable the VIP on the new master
  71. sub start_vip() {
  72.     `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
  73. }
  74. # A simple system call that disable the VIP on the old_master
  75. sub stop_vip() {
  76.     `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
  77. }

  78. sub usage {
  79.     print
  80.     "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=po
  81. rt\n";
  82. }

將此指令碼複製兩次到/usr/local/bin, 分別命名為master_ip_failover 和master_ip_online_change_script

然後將/etc/app1.cnf 中下面兩行註釋去掉:

master_ip_failover_script=/usr/local/bin/master_ip_failover
master_ip_online_change_script=/usr/local/bin/master_ip_online_change_script


實驗

  1. 1.主庫(115)新增一個vip

  1. [root@node2 .ssh]# /sbin/ifconfig eth0:1 192.168.6.66/24
    [root@node2 .ssh]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
        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 qlen 1000
        link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0
        inet 192.168.6.66/24 brd 192.168.6.255 scope global secondary eth0:1
        inet6 fe80::a00:27ff:fe03:1ba1/64 scope link 
           valid_lft forever preferred_lft forever

  1. 2.關閉主庫mysql

  1. [root@node2 .ssh]# /etc/init.d/mysqld stop
    Stopping mysqld:                                           [  OK  ]
    [root@node2 .ssh]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
        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 qlen 1000
        link/ether 08:00:27:03:1b:a1 brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.115/24 brd 192.168.6.255 scope global eth0           ---vip飄走了
        inet6 fe80::a00:27ff:fe03:1ba1/64 scope link 
           valid_lft forever preferred_lft forever

  1. 3 檢視newmaster(114)的ip                                                    ----vip已飄到new master上了

  1. [root@node1 ~]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
        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 qlen 1000
        link/ether 08:00:27:7a:c6:9c brd ff:ff:ff:ff:ff:ff
        inet 192.168.6.114/24 brd 192.168.6.255 scope global eth0
        inet 192.168.6.66/24 brd 192.168.6.255 scope global secondary eth0:1
        inet6 fe80::a00:27ff:fe7a:c69c/64 scope link 
           valid_lft forever preferred_lft forever

  2. 4 重構mysql.重啟mha監控
  3. 5 關閉114mysql,vip飄回115上去了




來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2122973/,如需轉載,請註明出處,否則將追究法律責任。

相關文章