Keepalived+Lvs+Mysql主主複製,keepalivedlvs

來源:互聯網
上載者:User

Keepalived+Lvs+Mysql主主複製,keepalivedlvs

 一簡介

Keepalived+lvs+mysql主主複製是比較常用的一種Mysql高可用方案,其中lvs 提供讀負載平衡,Keepalived通過虛擬vip漂移實現故障自動轉移,而Mysql雖然配置成主主複製,但是一般都是採用單點寫入來保證資料一致性。

整個方案實現非常簡單,僅需兩台伺服器即可,安裝配置也相對簡單,但需要自己寫指令碼實現keepalived服務的開啟和關閉,適用於只有兩台資料庫伺服器並且未實現讀寫分離的情境。系統整體結構框圖如下


 二環境搭建

2.1 環境配置如下

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複製配置

主主複製配置本質上就是配置兩台伺服器互相的主從,實現比較簡單,大致步驟如下

2.2.1 在Master上建立一個複製賬戶

mysql>grantreplication slave on *.* to rep71@10.0.11.244identified by‘123456’;

mysql>flushprivileges;

 

2.2.2 修改兩台mysql設定檔my.cnf如下

##Replication General Config (both master and slave)

server-id= 101

log-bin= mysql-log-bin

auto-increment-increment= 2

auto-increment-offset= 1

binlog_format= row

relay_log= mysql-relay-bin

 

2.2.3 在Master伺服器上執行show master status,記錄下File和Position

mysql>show master status\G;

***************************1. row ***************************

File:mysql-log-bin.000005

Position:327933

Binlog_Do_DB:

Binlog_Ignore_DB:test,mysql,information_schema,performance_schema

2.2.4 在Slave服務上執行

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重複以上四個步驟,在Slave伺服器上建立賬戶rep72,show master status,在Master上start slave,這樣mysql的主主同步就配置好了,分別在兩台伺服器上運行show slave status,如果結果都顯示Slave_IO_Running: Yes,Slave_SQL_Running: Yes,則證明同步正常。

 

2.3 Keepalived的安裝

2.3.1 在 master、backup 伺服器都進行安裝:

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

sudotar zxvf ipvsadm-1.24.tar.gz

cdipvsadm-1.24

sudoln -s /usr/src/kernels/2.6.32-358.el6.x86_64/ /usr/src/linux

make

sudomake install

 

2.4  Keepalived 的安裝

2.4.1在 master、backup 伺服器都進行安裝:

wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

tarzxvf keepalived-1.1.19.tar.gz

cdkeepalived-1.1.19

yuminstall -y openssl openssl-devel

sudoyum install popt-devel –y

./configure--sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.18-308.el5-x86_64/

make

sudomake install

 ln -s /usr/local/sbin/keepalived /sbin/

2.4.2  Master上的Keepalived 的配置

global_defs{

    router_id mysqlmha1

}

 

vrrp_scriptcheck_run {

    script"/etc/keepalived/check_mysqll.sh"

    interval 1

}

 

 

vrrp_instanceVI_1 {

    state MASTER

    interface em1

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

 

     track_script {

         check_run

     }

    virtual_ipaddress {

         10.0.11.190 dev em1 label em1:1

         10.0.11.191 dev em1 label em1:2

    }

  

}

 

virtual_server10.0.11.190 3306 {

delay_loop6

lb_algorr

lb_kindDR

persistence_timeout20

protocolTCP

sorry_server10.0.11.244 3306

real_server10.0.11.243 3306 {

weight3

TCP_CHECK{

connect_timeout3

nb_get_retry3

delay_before_retry3

connect_port3306

}

}

}

 

virtual_server10.0.11.191 3306 {

delay_loop6

lb_algorr

lb_kindDR

#persistence_timeout20

protocolTCP

real_server10.0.11.243 3306 {

weight3

TCP_CHECK{

connect_timeout3

nb_get_retry3

delay_before_retry3

connect_port3306

}

}

real_server10.0.11.244 3306 {

weight3

TCP_CHECK{

connect_timeout3

nb_get_retry3

delay_before_retry3

connect_port3306

}

}

}

2.4.3  Backup 的 keepalived 的配置

global_defs {

   router_id mysqlmha1

}

 

vrrp_script check_run {

   script "/etc/keepalived/check_mysqll.sh"

   interval 1

}

 

 

vrrp_instance VI_1 {

   state BACKUP

   interface em1

   virtual_router_id 51

   priority 90

   advert_int 1

   authentication {

        auth_type PASS

       auth_pass 1111

    }

 

    track_script {

        check_run

    }

   virtual_ipaddress {

        10.0.11.190 dev em1 label em1:1

        10.0.11.191 dev em1 label em1:2

    }

  

}

 

virtual_server 10.0.11.190 3306 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 20

protocol TCP

sorry_server 10.0.11.244 3306

real_server 10.0.11.243 3306 {

weight 3

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

}

 

virtual_server 10.0.11.191 3306 {

delay_loop 6

lb_algo rr

lb_kind DR

#persistence_timeout 20

protocol TCP

real_server 10.0.11.243 3306 {

weight 3

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

real_server 10.0.11.244 3306 {

weight 3

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 3306

}

}

}

2.5  Master 和 backup 的 realserver 的配置

對於 realserver 的配置 master 和 backup 是一致的,指令碼內容如下:

#!/bin/bash

#description: Config realserver lo and apply noarp

SNS_VIP=10.1.11.190

SNS_VIP2=10.0.11.191

/etc/rc.d/init.d/functions

case"$1" in

start)

ifconfiglo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP

ifconfiglo: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:1

echo"1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo"2" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo"1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo"2" >/proc/sys/net/ipv4/conf/all/arp_announce

echo"1" >/proc/sys/net/ipv4/conf/em1/arp_ignore

echo"2" >/proc/sys/net/ipv4/conf/em1/arp_announce

sysctl-p >/dev/null 2>&1

echo"RealServer Start OK"

;;

stop)

ifconfiglo:0 down

ifconfiglo:1 down

routedel $SNS_VIP >/dev/null 2>&1

routedel $SNS_VIP2 >/dev/null 2>&1

echo"0" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo"0" >/proc/sys/net/ipv4/conf/lo/arp_announce

echo"0" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo"0" >/proc/sys/net/ipv4/conf/all/arp_announce

echo"0" >/proc/sys/net/ipv4/conf/em1/arp_ignore

echo"0" >/proc/sys/net/ipv4/conf/em1/arp_announce

echo"RealServer Stoped"

;;

*)

echo"Usage: $0 {start|stop}"

exit1

esac

exit0

 

2.6 編輯指令碼控制Keepalived的開啟和停止

大體意思是只要檢測到mysql服務停止keepalived服務也停止 ,因為keepalived是通過組播方式告訴本網段自己還活著當mysql服務停止後keepalived還依然運行 這時就需要停止keepalived讓另一個主機獲得虛擬IP,可以在後台運行這個指令碼 也可以在keepalived設定檔加入這個指令碼,master和slave節點都需要,只是資料庫使用者名稱和密碼有區別
[root@244 ~]#more /etc/keepalived/check_mysql.sh 
#20140722
#!/bin/bash
MYSQL=/usr/bin/mysql 
MYSQL_HOST=10.0.11.243

MYSQL_USER=root 
MYSQL_PASSWORD=mysql 
CHECK_TIME=3

#mysql  isworking MYSQL_OK is 1 , mysql down MYSQL_OK is 0

MYSQL_OK=1
function check_mysql_helth (){ 
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "showstatus;" >/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

該指令碼需要有執行許可權,通過以下命令加上執行許可權
# chmod +x check_mysql.sh 

2.7 測試Keepalived自動容錯移轉

Master和slave上執行sudo servicekeepalived start,在master上執行ip a


Slave上執行 ip a


從上可以看到masteMySQL上虛擬IP綁定成功,backup MySQL上keepalived服務正常運行,但無虛擬IP(這屬於正常)

用戶端能通過vip串連上mysql


停止master上的mysql服務,可發現Keepalived服務也會停止


這時候slave運行ip a發現綁定了虛擬ip,並且從庫mysql運行正常,而用戶端依舊能通過vip連上資料庫



重啟master的mysql和Keepalived以後,並在slave庫運行start slave,master運行ip a發現恢複之前的服務


2.8 測試lvs的負載平衡效果

啟動 master 和 backup 的 mysql 以後,再在 master 和 backup 執行如下命令啟

動 keepalived 和 realserver 指令碼:

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

/etc/rc.d/init.d/keepalived start

運行master和backup上的sudo ipvsadm -ln可看到如下資訊


在用戶端通過read vip串連到mysql並執行幾次select,可考到inaction發生了變化,證明實現了負載平衡


 

相關文章

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.