LVS+Keepalived實現MySQL從庫讀操做負載均衡配置

轉載於osyunweimysql

說明:linux

操做系統:CentOS 5.X 64位c++

MySQL主服務器:192.168.21.126算法

MySQL從服務器:192.168.21.127,192.168.21.128sql

MySQL主從同步的數據庫爲:osyunweidb數據庫

實現目的:後端

增長兩臺服務器(主備),經過LVS+Keepalived實現MySQL從庫讀操做負載均衡服務器

架構規劃:架構

操做系統:CentOS 5.X 64位負載均衡

LVS主服務器:192.168.21.129

LVS備服務器:192.168.21.130

LVS虛擬服務器(VIP):192.168.21.254

部署完成以後,經過VIP:192.168.21.254,根據LVS調度算法來訪問後端真實的MySQL從服務器,實現負載均衡。

具體操做:

第一部分:分別在兩臺MySQL從服務器上操做

1、關閉SELINUX

vi /etc/selinux/config

#SELINUX=enforcing #註釋掉

#SELINUXTYPE=targeted #註釋掉

SELINUX=disabled #增長

:wq!  #保存退出

setenforce 0 #使配置當即生效

2、配置防火牆,開啓3306端口

vi /etc/sysconfig/iptables  #編輯

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  #容許3306端口經過防火牆

:wq! #保存退出

/etc/init.d/iptables restart #重啓防火牆使配置生效

系統運維  www.osyunwei.com  舒適提醒:qihang01原創內容 版權全部,轉載請註明出處及原文連接

3、綁定LVS虛擬服務器(VIP):192.168.21.254到lo:0

vi  /etc/rc.d/init.d/realserver  #編輯,添加如下代碼

#################################################

#!/bin/sh

# chkconfig: - 80 90

# description:realserver

# mysql_vip start realserver

mysql_vip=192.168.21.254 #LVS虛擬服務器(VIP)

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

case "$1" in

start)

ifconfig lo:0 $mysql_vip netmask 255.255.255.255 broadcast $mysql_vip

/sbin/route add -host $mysql_vip dev lo:0

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

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

echo "RealServer Start OK"

;;

stop)

ifconfig lo:0 down

route del $mysql_vip >/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 "RealServer Stoped"

;;

*)

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

exit 1

esac

exit 0

#################################################

chmod +x /etc/rc.d/init.d/realserver  #添加腳本執行權限

chkconfig realserver on #添加開機啓動

/etc/rc.d/init.d/realserver start #開啓,參數stop爲關閉

4、調整服務器參數,使LVS虛擬服務器(VIP)忽略ARP廣播包

vi /etc/sysctl.conf  #編輯

net.ipv4.ip_forward= 1  #修改0爲1,開啓轉發

net.ipv4.conf.lo.arp_ignore= 1

net.ipv4.conf.lo.arp_announce= 2

net.ipv4.conf.all.arp_ignore= 1

net.ipv4.conf.all.arp_announce= 2

:wq! #保存退出

/sbin/sysctl -p   #使配置當即生效

5、設置可以遠程監控MySQL主從同步狀態的MySQL用戶和密碼

mysql -u root -p #進入MySQL控制檯

insert into mysql.user(Host,User,Password) values('localhost','checkslave',password('123456'));    #新建帳戶checkslave,密碼123456

flush privileges;    #刷新系統受權表

grant all on *.* to 'checkslave'@'192.168.21.129' identified by '123456' with grant option;    #容許帳戶checkslave從LVS主服務器192.168.21.129鏈接到數據庫服務器

grant all on *.* to 'checkslave'@'192.168.21.130' identified by '123456' with grant option;    #容許帳戶checkslave從LVS從服務器192.168.21.130鏈接到數據庫服務器

第二部分:分別在兩臺LVS主備服務器上操做

1、關閉SElinux、配置防火牆

一、vi /etc/selinux/config

#SELINUX=enforcing #註釋掉

#SELINUXTYPE=targeted #註釋掉

SELINUX=disabled #增長

:wq!  #保存退出

setenforce 0 #使配置當即生效

二、vi /etc/sysconfig/iptables  #編輯

-A RH-Firewall-1-INPUT -d 224.0.0.18 -j ACCEPT  #容許VRRP(虛擬路由器冗餘協議)組播地址通訊

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT  #容許3306端口經過防火牆

:wq! #保存退出

/etc/init.d/iptables restart #重啓防火牆使配置生效

2、安裝lvs軟件

yum install ipvsadm  #Linux 2.6內核已經集成了lvs軟件,此處ipvsadm爲lvs管理工具

modprobe ip_vs  #加載ip_vs

3、安裝keepalived

yum install  gcc gcc-c++ make openssl-devel kernel-devel ncurses-devel  #安裝編譯工具包

下載keeplived:http://www.keepalived.org/software/keepalived-1.2.12.tar.gz

上傳keepalived-1.2.12.tar.gz到/usr/local/src目錄

cd /usr/local/src

tar zxvf keepalived-1.2.12.tar.gz

cd keepalived-1.2.12

./configure  #配置,必須看到如下提示,說明配置正確,才能繼續安裝

Use IPVS Framework : Yes

IPVS sync daemon support : Yes

Use VRRP Framework       : Yes

make #編輯

make install  #安裝

cp /usr/local/etc/sysconfig/keepalived  /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

cp /usr/local/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/

chmod +x /etc/rc.d/init.d/keepalived  #添加執行權限

chkconfig keepalived on  #設置開機啓動

service keepalived start #啓動

service keepalived stop  #關閉

service keepalived restart  #重啓

4、安裝perl、perl-DBI、DBD-mysql模塊以及MySQL客戶端   #perl腳本鏈接MySQL數據庫須要

yum install  perl  perl-DBI  perl-DBD-MySQL  mysql  #執行此命令安裝

vi /tmp/test_perl.pl

#!/usr/bin/perl

print "Hello, world!\n";

:wq! #保存退出

perl /tmp/test_perl.pl  #運行測試腳本,若是出現:Hello,world! 說明perl安裝成功

perldoc DBI  #查看DBI模塊是否安裝

perldoc DBD::mysql   #查看DBD::mysql模塊是否安裝

5、配置keepalived

cp /etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf-bak

vi /etc/keepalived/keepalived.conf  #編輯,修改成如下代碼

##################################################################################################

! Configuration File for keepalived

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

notification_email_from Alexandre.Cassen@firewall.loc

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_instance VI_1 {

state MASTER  #LVS備機修改成BACKUP

interface eth0

virtual_router_id 51

priority 100  #LVS備機修改成80

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.21.254

}

notify_master "/etc/keepalived/clean_arp.sh"  #LVS主備機都爲notify_master

}

virtual_server 192.168.21.254 3306 {

delay_loop 30

lb_algo wlc

lb_kind DR

#nat_mask 255.255.255.0

persistence_timeout 120

protocol TCP

real_server 192.168.21.127 3306 {

weight 1

MISC_CHECK {

misc_path "/etc/keepalived/check_slave.pl 192.168.21.127"

misc_dynamic

}

}

real_server 192.168.21.128 3306 {

weight 1

MISC_CHECK {

misc_path "/etc/keepalived/check_slave.pl 192.168.21.128"

misc_dynamic

}

}

##################################################################################################

:wq! #保存退出

系統運維  www.osyunwei.com  舒適提醒:qihang01原創內容 版權全部,轉載請註明出處及原文連接

6、設置MySQL主從監控腳本check_slave.pl

vi /etc/keepalived/check_slave.pl #編輯,添加如下代碼

##################################################################################################

#!/usr/bin/perl -w

use DBI;

use DBD::mysql;

# CONFIG VARIABLES

$SBM = 120;

$db = "osyunweidb";

$host = $ARGV[0];

$port = 3306;

$user = "checkslave";

$pw = "123456";

# SQL query

$query = "show slave status";

$dbh = DBI->connect("DBI:mysql:$db:$host:$port", $user, $pw, { RaiseError => 0,PrintError => 0 });

if (!defined($dbh)) {

exit 1;

}

$sqlQuery = $dbh->prepare($query);

$sqlQuery->execute;

$Slave_IO_Running =  "";

$Slave_SQL_Running = "";

$Seconds_Behind_Master = "";

while (my $ref = $sqlQuery->fetchrow_hashref()) {

$Slave_IO_Running = $ref->{'Slave_IO_Running'};

$Slave_SQL_Running = $ref->{'Slave_SQL_Running'};

$Seconds_Behind_Master = $ref->{'Seconds_Behind_Master'};

}

$sqlQuery->finish;

$dbh->disconnect();

if ( $Slave_IO_Running eq "No" || $Slave_SQL_Running eq "No" ) {

exit 1;

} else {

if ( $Seconds_Behind_Master > $SBM ) {

exit 1;

} else {

exit 0;

}

}

##################################################################################################

:wq! #保存退出

chmod +x  /etc/keepalived/check_slave.pl  #添加腳本執行權限

7、設置更新LVS虛擬服務器(VIP)地址的arp記錄到網關腳本

vi  /etc/keepalived/clean_arp.sh  #編輯,添加如下代碼

#!/bin/sh

VIP=192.168.21.254

GATEWAY=192.168.21.2

/sbin/arping -I eth0 -c 5 -s $VIP $GATEWAY &>/dev/null

:wq!  #保存退出

chmod +x /etc/keepalived/clean_arp.sh #添加腳本執行權限

8、測試LVS+Keepalived是否正常運行

service keepalived restart  #在兩臺LVS主備服務器上重啓keepalived

ipvsadm -L  #在兩臺LVS主備服務器上執行此命令,看到以下圖所示

ip addr show  #在兩臺LVS主備服務器上執行此命令,看到以下圖所示

#從圖中能夠看到VIP:192.168.21.254如今指向的是LVS主服務器

一、關閉LVS主服務器的keepalived服務,查看LVS備服務器是否正常接管keepalived

service keepalived stop  #在LVS主服務器上運行

此時,在兩臺LVS主備服務器上執行ip addr show命令,以下圖所示

系統運維  www.osyunwei.com  舒適提醒:qihang01原創內容 版權全部,轉載請註明出處及原文連接

能夠看到VIP:192.168.21.254如今指向的是LVS備服務器,說明LVS備服務器已經接管keepalived,測試成功。

二、中止MySQL從服務器192.168.21.127上面的主從同步服務

mysql -u root -p  #進入MySQL控制檯

slave start; #中止同步

在LVS備服務器執行ipvsadm -L命令,能夠看到MySQL從服務器192.168.21.127已經從負載均衡列表中被剔除了,測試成功。

三、從客戶端鏈接LVS虛擬服務器(VIP):192.168.21.254

telnet 192.168.21.254 3306  #執行此命令,以下圖所示,說明測試成功。

tail  -f /var/log/messages  #查看LVS日誌信息

至此,LVS+Keepalived實現MySQL從庫讀操做負載均衡配置完成。

相關文章
相關標籤/搜索