LVS+Keepalived實現MySQL從庫讀操作負載平衡

來源:互聯網
上載者:User
說明:
作業系統:CentOS 5.X 64位
MySQL主伺服器:192.168.21.126
MySQL從伺服器:192.168.21.127,192.168.21.128
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從伺服器上操作
一、關閉SELINUX
vi /etc/selinux/config
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加
:wq!  #儲存退出
setenforce 0 #使配置立即生效
二、配置防火牆,開啟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 #重啟防火牆使配置生效
三、綁定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為關閉
四、調整伺服器參數,使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   #使配置立即生效
五、設定能夠遠程監控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主備伺服器上操作
一、關閉SElinux、配置防火牆
1、vi /etc/selinux/config
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加
:wq!  #儲存退出
setenforce 0 #使配置立即生效
2、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 #重啟防火牆使配置生效
二、安裝lvs軟體
yum install ipvsadm  #Linux 2.6核心已經整合了lvs軟體,此處ipvsadm為lvs管理工具
modprobe ip_vs  #載入ip_vs
三、安裝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  #重啟
四、安裝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模組是否安裝
五、配置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! #儲存退出
六、設定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  #添加指令碼執行許可權
七、設定更新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 #添加指令碼執行許可權
八、測試LVS+Keepalived是否正常運行
service keepalived restart  #在兩台LVS主備伺服器上重啟keepalived
ipvsadm -L  #在兩台LVS主備伺服器上執行此命令,看到如下圖所示
ip addr show  #在兩台LVS主備伺服器上執行此命令,看到如下圖所示
#從圖中可以看到VIP:192.168.21.254現在指向的是LVS主伺服器
1、關閉LVS主伺服器的keepalived服務,查看LVS備伺服器是否正常接管keepalived
service keepalived stop  #在LVS主伺服器上運行
此時,在兩台LVS主備伺服器上執行ip addr show命令,如下圖所示
可以看到VIP:192.168.21.254現在指向的是LVS備伺服器,說明LVS備伺服器已經接管keepalived,測試成功。
2、停止MySQL從伺服器192.168.21.127上面的主從同步服務
mysql -u root -p  #進入MySQL控制台
slave start; #停止同步
在LVS備伺服器執行ipvsadm -L命令,可以看到MySQL從伺服器192.168.21.127已經從負載平衡列表中被剔除了,測試成功。
3、從用戶端串連LVS虛擬伺服器(VIP):192.168.21.254
telnet 192.168.21.254 3306  #執行此命令,如下圖所示,說明測試成功。
tail  -f /var/log/messages  #查看LVS日誌資訊
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.