Mha vip switch script

Source: Internet
Author: User
Tags failover perl script

Mha vip switch script

MHA can ~ Automatic Fault Detection and Failover within 30 seconds, suitable for high availability and high data integrity requirements. To achieve seamless switching, you also need to rely on VIP drift. The commonly used method of VIP drift is keepalived or script. Script Mode is relatively simple without installation and complex configuration. This article describes how to implement VIP Switching Based on scripts.

For configuration of keepalived, see install and configure Keepalived in CentOS 5.9.

1. Current host environment and MHA Configuration
[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 environment
[Root @ vdbsrv4 ~] # More/etc/issue
CentOS release 5.9 (Final)
Kernel \ r on an \ m

 

### Mysql Environment
[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 configuration information
[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. Test VIP Switching
### Test whether VIP (192.168.1.13) is enabled
[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

### Add a VIP address to host vdbsrv1
[Root @ vdbsrv4 ~] # Ssh vdbsrv1 "/sbin/ifconfig eth0: 0 192.168.1.13 netmask 255.255.255.0 up"

### Check whether the VIP is enabled successfully
[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

### Enable MHA
[Root @ vdbsrv4 ~] # Masterha_manager -- conf =/etc/masterha/app1.cnf &

### Simulate master database downtime
[Root @ vdbsrv4 ~] # Ssh vdbsrv1 "killall-r mysqld"

### View the management node log and you can see that the VIP has drifted
[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

### Verify whether the VIP address is on the vdbsrv2 Node
[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

###### View MHA switching logs of a management Node
[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 switch perl script

[Root @ vdbsrv4 app1] # more/tmp/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
);

My $ vip = '1970. 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 ";
}

 

This article permanently updates the link address:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.