Keepalived with MHA for MySQL high availability

Source: Internet
Author: User
Tags reflector pkill


This example combined with this blog another article mha-large kill device to achieve MySQL single main downtime, VIP drift to achieve high availability, environment please refer to the previous article


This example uses the keepalived-1.1.20.tar.gz version


[Email protected] keepalived-1.1.20]#/configure--sysconf=/etc/#此项表示设置keepalived的根目录


If compile error "configure:error:Popt libraries is required" then yum-y install Popt-devel


[[email protected] keepalived-1.1.20]# make && make install


[Email protected] keepalived]# cp/usr/local/sbin/keepalived/usr/sbin/#拷贝命令文件



#################### #主机的配置文件设置 ####################


[Email protected] keepalived]# cat/etc/keepalived/keepalived.conf

! Configuration File for Keepalived


global_defs {#全局配置项

router_id node1#本机标识号, setting globally unique

}

Vrrp_script Check_run {#设置检测服务的脚本

Script "/root/check_mysql.sh"

Interval 1#每隔一秒检测一次

}


Vrrp_instance MHA { #定义keepalived组

State MASTER#定义本机的keepalived状态

Interface eth0#设置监听网卡

virtual_router_id#设置虚拟router-id, all keepalived machines have to be the same.

priority #设置权值, master must be higher than backup

Advert_int 5#定义master与backup之间同步检查间隔, Unit is seconds

track_interface {#设置监听网卡组

Eth0

Eth1

}

Authentication {#设置keepalived组的验证类型和密码, all hosts must be the same

Auth_type PASS

Auth_pass 1111

}

Track_script {#设置服务监测脚本运行

Check_run

}

virtual_ipaddress {#设置VIP

10.2.16.250/24

}

}


#################### #备机的配置文件设置 ####################


[Email protected] ~]# cat/etc/keepalived/keepalived.conf

! Configuration File for Keepalived


Global_defs {

router_id Node2

}


Vrrp_script Check_run {

Script "/root/check_mysql.sh"

Interval 1

}


Vrrp_instance MHA {

State BACKUP

Interface eth0

VIRTUAL_ROUTER_ID 100

Priority 99

Advert_int 5

Track_interface {

Eth0

Eth1

}

Track_interface {

Eth0

Eth1

}

Track_script {

Check_run

}

Authentication {

Auth_type PASS

Auth_pass 1111

}

virtual_ipaddress {

10.2.16.250/24

}

}



The MySQL service detects the script content and modifies it as needed:


[email protected] ~]# cat check_mysql.sh

#!/bin/bash

Mysql=/usr/bin/mysql

mysql_host=127.0.0.1

Mysql_user=root

mysql_password=123456

Check_time=3

#mysql is working mysql_ok are 1, MySQL down MYSQL_OK is 0

Mysql_ok=1

function Check_mysql_helth () {

$MYSQL-H $MYSQL _host-u $MYSQL _user-e "show status;"--password=123456 >/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



Start keepalived:


[[Email protected] ~] #service keepalived Start


Start log:

[Email protected] ~]# tail-f/var/log/messages

June 11:48:47 Node2 keepalived:starting VRRP child process, pid=21545

June 11:48:47 Node2 keepalived_vrrp:registering Kernel netlink Reflector

June 11:48:47 node2 keepalived_vrrp:registering Kernel netlink command channel

June 11:48:47 Node2 keepalived_vrrp:registering gratutious ARP shared channel

June 11:48:47 node2 keepalived_vrrp:opening file '/etc/keepalived/keepalived.conf '.

June 11:48:47 Node2 Keepalived_vrrp:configuration is using:65535 Bytes

June 11:48:47 Node2 keepalived_vrrp:using linkwatch kernel netlink reflector ...

June 11:48:47 Node2 keepalived_vrrp:vrrp_instance (MHA) Entering BACKUP State

June 11:48:47 Node2 KEEPALIVED_VRRP:VRRP sockpool: [IfIndex (2), Proto (FDS), FD (9,10)]

June 11:48:47 Node2 Keepalived_vrrp:vrrp_script (Check_run) succeeded



June 11:49:03 Node2 keepalived_vrrp:vrrp_instance (MHA) Transition to MASTER State

June 11:49:08 Node2 keepalived_vrrp:vrrp_instance (MHA) Entering MASTER State

June 11:49:08 Node2 Keepalived_vrrp:vrrp_instance (MHA) setting protocol VIPs.

June 11:49:08 Node2 Keepalived_vrrp:vrrp_instance (MHA) sending gratuitous ARPs on eth0 for 10.2.16.250

June 11:49:13 Node2 Keepalived_vrrp:vrrp_instance (MHA) sending gratuitous ARPs on eth0 for 10.2.16.250



View VIP:

[[Email protected] ~]# IP addr

1:lo: <LOOPBACK,UP,LOWER_UP> MTU 16436 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 Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0c:29:c5:22:55 BRD FF:FF:FF:FF:FF:FF

inet 10.2.16.253/24 BRD 10.2.16.255 Scope Global eth0

inet 10.2.16.250/24 BRD 10.2.16.255 Scope Global eth0

Inet6 FE80::20C:29FF:FEC5:2255/64 Scope link

Valid_lft Forever Preferred_lft Forever

3:eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast State up Qlen 1000

Link/ether 00:0c:29:c5:22:5f BRD FF:FF:FF:FF:FF:FF

inet 10.0.0.1/24 BRD 10.0.0.255 Scope Global eth1

Inet6 FE80::20C:29FF:FEC5:225F/64 Scope link

Valid_lft Forever Preferred_lft Forever


This VIP can not be seen with ifconfig, but it is real.

# # # #check_mysql. SH needs to exist on both machines


############## #keepalived +mha+check_mysql Operation Principle #################

To achieve VIP drift, the Node1 MySQL service needs to be stopped, at this time the script running in keepalived will detect MySQL, after detecting MySQL hangs, will pkill keepalived

When Backup-keepalived detects that the main keepalived is hung, the VIP is automatically started, and MHA detects that Master's MySQL is dead, it will also move master to Bakcup

At this point all permissions are moved to Mysql-backup, and he has become master, taking over all the work of the downtime master, thus achieving high availability.














This article is from the "Fate" blog, make sure to keep this source http://czybl.blog.51cto.com/4283444/1431693

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.