Keepalived + Lvs + Mysql master-master replication _ MySQL

Source: Internet
Author: User
1. Keepalived43; lvs43; master-master replication of mysql is a common Mysql high-availability solution. lvs provides read load balancing and Keepalived implements automatic failover through virtual vip drift, although Mysql is configured to master-master replication, it generally uses single point of writing (SPOF ).

1 Introduction

Keepalived + lvs + mysql master/master replication is a common Mysql high-availability solution. lvs provides read load balancing and Keepalived implements automatic failover through virtual vip drift, although Mysql is configured as Master-master replication, it generally uses single-point writing to ensure data consistency.

The implementation of the entire solution is very simple. it only requires two servers, and the installation and configuration are relatively simple. However, you need to write a script to enable and disable the keepalived service, it is applicable to scenarios where there are only two database servers and no read/write splitting is implemented. The overall system architecture is as follows:

2. environment construction

2.1 Environment configuration:

Keepalived

Server

LVS

Server

LVS

Realserver

MySQL

Master-Master

Write VIP

Read VIP

OS

MySQL

Master

10.0.11.243

10.0.11.243

10.0.11.243

10.0.11.243

10.0.11.190

10.0.11.191

Centos6

Percona5.6

Backup

10.0.11.244

10.0.11.244

10.0.11.244

10.0.11.244

10.0.11.190

10.0.11.191

Centos6

Percona5.6

2.2 mysql master-master replication configuration

The master-master replication configuration is essentially to configure the master-slave relationship between the two servers. the implementation is relatively simple. the general steps are as follows:

2.2.1 create a duplicate account on the Master

Mysql> grantreplication slave on *. * to rep71@10.0.11.244identified by '123 ';

Mysql> flushprivileges;

2.2.2 modify the two mysql configuration files my. cnf as follows

##Replication General Config (both master and slave)server-id= 101log-bin= mysql-log-binauto-increment-increment= 2auto-increment-offset= 1binlog_format= rowrelay_log= mysql-relay-bin

2.2.3 execute show Master status on the master server to record File and Position

mysql>show master status\G;***************************1. row ***************************File:mysql-log-bin.000005Position:327933Binlog_Do_DB:Binlog_Ignore_DB:test,mysql,information_schema,performance_schema

2.2.4 run on the Slave service

mysql> slave stop;Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> change master to-> master_host='10.0.11.243',-> master_user='rep71',-> master_password='123456',-> master_log_file=' mysql-log-bin.000005',-> master_log_pos=327933;Query OK, 0 rows affected (0.01 sec)mysql> start slave;

2.2.5 repeat the above four steps to create the account rep72 and show master status on the Slave server and start slave on the Master server, so that the master synchronization of mysql is configured, run show slave status on both servers. if the results show Slave_IO_Running: Yes and Slave_ SQL _Running: Yes, the synchronization is normal.

2.3 install Keepalived

2.3.1 install on both the master and backup servers:

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gzsudotar zxvf ipvsadm-1.24.tar.gzcdipvsadm-1.24sudoln -s /usr/src/kernels/2.6.32-358.el6.x86_64/ /usr/src/linuxmakesudomake install

2.4 install Keepalived

2.4.1 install on both the master and backup servers:

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gztarzxvf keepalived-1.1.19.tar.gzcdkeepalived-1.1.19yuminstall -y openssl openssl-develsudoyum install popt-devel –y./configure--sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.18-308.el5-x86_64/makesudomake installln -s /usr/local/sbin/keepalived /sbin/

2.4.2 Keepalived configuration on the Master

global_defs{router_id mysqlmha1}vrrp_scriptcheck_run {script"/etc/keepalived/check_mysqll.sh"interval 1}vrrp_instanceVI_1 {state MASTERinterface em1virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_run}virtual_ipaddress {10.0.11.190 dev em1 label em1:110.0.11.191 dev em1 label em1:2}}virtual_server10.0.11.190 3306 {delay_loop6lb_algorrlb_kindDRpersistence_timeout20protocolTCPsorry_server10.0.11.244 3306real_server10.0.11.243 3306 {weight3TCP_CHECK{connect_timeout3nb_get_retry3delay_before_retry3connect_port3306}}}virtual_server10.0.11.191 3306 {delay_loop6lb_algorrlb_kindDR#persistence_timeout20protocolTCPreal_server10.0.11.243 3306 {weight3TCP_CHECK{connect_timeout3nb_get_retry3delay_before_retry3connect_port3306}}real_server10.0.11.244 3306 {weight3TCP_CHECK{connect_timeout3nb_get_retry3delay_before_retry3connect_port3306}}}

2.4.3 keepalived configuration of Backup

global_defs {router_id mysqlmha1}vrrp_script check_run {script "/etc/keepalived/check_mysqll.sh"interval 1}vrrp_instance VI_1 {state BACKUPinterface em1virtual_router_id 51priority 90advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_run}virtual_ipaddress {10.0.11.190 dev em1 label em1:110.0.11.191 dev em1 label em1:2}}virtual_server 10.0.11.190 3306 {delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 20protocol TCPsorry_server 10.0.11.244 3306real_server 10.0.11.243 3306 {weight 3TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}}}virtual_server 10.0.11.191 3306 {delay_loop 6lb_algo rrlb_kind DR#persistence_timeout 20protocol TCPreal_server 10.0.11.243 3306 {weight 3TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}}real_server 10.0.11.244 3306 {weight 3TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}}}

2.5 configure the realserver of Master and backup

The configuration of the realserver master and backup are consistent. the script content is as follows:

#!/bin/bash#description: Config realserver lo and apply noarpSNS_VIP=10.1.11.190SNS_VIP2=10.0.11.191/etc/rc.d/init.d/functionscase"$1" instart)ifconfiglo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIPifconfiglo:1 $SNS_VIP2 netmask 255.255.255.255 broadcast $SNS_VIP2/sbin/routeadd -host $SNS_VIP dev lo:0/sbin/routeadd -host $SNS_VIP2 dev lo:1echo"1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho"2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho"1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho"2" >/proc/sys/net/ipv4/conf/all/arp_announceecho"1" >/proc/sys/net/ipv4/conf/em1/arp_ignoreecho"2" >/proc/sys/net/ipv4/conf/em1/arp_announcesysctl-p >/dev/null 2>&1echo"RealServer Start OK";;stop)ifconfiglo:0 downifconfiglo:1 downroutedel $SNS_VIP >/dev/null 2>&1routedel $SNS_VIP2 >/dev/null 2>&1echo"0" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho"0" >/proc/sys/net/ipv4/conf/lo/arp_announceecho"0" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho"0" >/proc/sys/net/ipv4/conf/all/arp_announceecho"0" >/proc/sys/net/ipv4/conf/em1/arp_ignoreecho"0" >/proc/sys/net/ipv4/conf/em1/arp_announceecho"RealServer Stoped";;*)echo"Usage: ___FCKpd___7 {start|stop}"exit1esacexit0

2.6 edit the script to control enabling and stopping Keepalived

In general, as long as the mysql service is detected to stop keepalived, because keepalived tells the CIDR block that it is still alive by means of multicast. when the mysql service is stopped and keepalived is still running, stop keepalived to allow another host to obtain the virtual IP address, you can run this script in the background or add this script to the keepalived configuration file. Both the master and slave nodes need this script, but the database username and password are different.

[root@244 ~]#more /etc/keepalived/check_mysql.sh#20140722#!/bin/bashMYSQL=/usr/bin/mysqlMYSQL_HOST=10.0.11.243MYSQL_USER=rootMYSQL_PASSWORD=mysqlCHECK_TIME=3#mysql isworking MYSQL_OK is 1 , mysql down MYSQL_OK is 0MYSQL_OK=1function check_mysql_helth (){$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "showstatus;" >/dev/null 2>&1if [ $? = 0 ] ;thenMYSQL_OK=1elseMYSQL_OK=0fireturn $MYSQL_OK}while [ $CHECK_TIME -ne 0 ]dolet "CHECK_TIME -= 1"check_mysql_helthif [ $MYSQL_OK = 1 ] ; thenCHECK_TIME=0exit 0fiif [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]thenpkill keepalivedexit 1fisleep 1done

This script requires the execution permission. use the following command to add the execution permission.
# Chmod + x check_mysql.sh

2.7 test Keepalived automatic failover

Run sudo servicekeepalived start on the Master and slave, and run ip a on the master.

Run ip a on Slave

From the above we can see that the virtual IP address is successfully bound on masteMySQL, and the keepalived service on backup MySQL runs normally, but there is no virtual IP address (this is normal)

The client can connect to mysql through vip

Stop the mysql service on the master node. it can be found that the Keepalived service will also stop.

At this time, slave runs ip a and finds that it is bound to a virtual ip address, and mysql runs normally from the database, while the client can still connect to the database through vip.

Restart the master's mysql and Keepalived, and run start slave in the slave database. master runs ip a to find the service before recovery.

2.8 test the load balancing effect of lvs

After mysql of master and backup is started, run the following command on master and backup to start

Run keepalived and realserver scripts:

/Etc/rc. d/init. d/realserver. sh start

/Etc/rc. d/init. d/keepalived start

Run sudo ipvsadm-ln on the master and backup nodes. the following information is displayed:

When the client connects to mysql through read vip and executes the select statement several times, the inaction is changed, which proves that the load balancing is realized.

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.