利用LVS+Keepalived 實(shí)現(xiàn)高性能高可用負(fù)載均衡服務(wù)器
發(fā)布: 2008-10-27 17:49 | 作者: NetSeek | 來源: IT運(yùn)維專家網(wǎng)
作者:NetSeek http://www.linuxtone.org(IT運(yùn)維專家網(wǎng)|集群架構(gòu)|性能調(diào)優(yōu))
歡迎轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明.
更新時(shí)間:
背景:
隨著你的網(wǎng)站業(yè)務(wù)量的增長你網(wǎng)站的服務(wù)器壓力越來越大?需要負(fù)載均衡方案!商業(yè)的硬件如F5又太貴,你們又是創(chuàng)業(yè)型互聯(lián)公司如何有效節(jié)約成本,節(jié)省不必要的浪費(fèi)?同時(shí)實(shí)現(xiàn)商業(yè)硬件一樣的高性能高可用的功能?有什么好的負(fù)載均衡可伸張可擴(kuò)展的方案嗎?答案是肯定的!有!我們利用LVS+Keepalived基于完整開源軟件的架構(gòu)可以為你提供一個(gè)負(fù)載均衡及高可用的服務(wù)器。
LVS+Keepalived 介紹
LVS
LVS是Linux Virtual Server的簡寫,意即Linux虛擬服務(wù)器,是一個(gè)虛擬的服務(wù)器集群系統(tǒng)。本項(xiàng)目在1998年5月由
十種調(diào)度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalvied
Keepalived在這里主要用作RealServer的健康狀態(tài)檢查以及LoadBalance主機(jī)和BackUP主機(jī)之間failover的實(shí)現(xiàn)
二. 網(wǎng)站負(fù)載均衡拓樸圖
IP信息列表:
名稱 IP
CODE:
LVS-DR-Master 192.168.0.89
LVS-DR-BACKUP 192.168.0.90
LVS-DR-VIP 192.168.0.87
WEB1-Realserver 192.168.0.253
WEB2-Realserver 192.168.0.10
GateWay 192.168.0.1 三. 安裝LVS和Keepalvied軟件包
1. 下載相關(guān)軟件包
CODE:
#mkdir /usr/local/src/lvs
#cd /usr/local/src/lvs
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget http://www.keepalived.org/software/keepalived-
CODE:
#lsmod |grep ip_vs
#uname -r
#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#find / -name ipvsadm # 查看ipvsadm的位置
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
#find / -name keepalived # 查看keepalived位置
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#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/
#service keepalived start|stop #做成系統(tǒng)啟動(dòng)服務(wù)方便管理.四. 配置LVS實(shí)現(xiàn)負(fù)載均衡
1. LVS-DR,配置LVS腳本實(shí)現(xiàn)負(fù)載均衡
注: 此腳本在于演示方便大家理解lvs,在keepalived方案中不要啟動(dòng)此腳本,所以看后面有注掉,很多朋友對(duì)此有問題。關(guān)于LVS的keepalvied的HA方案,完全由keepalived.conf一個(gè)文件搞定,特此聲明!
CODE:
#vi /usr/local/sbin/lvs-dr.sh
#!/bin/bash
# description: start LVS of DirectorServer
#Written by :NetSeek http://www.linuxtone.org
GW=192.168.0.1
# website director vip.
SNS_VIP=192.168.0.87
SNS_RIP1=192.168.0.253
SNS_RIP2=192.168.0.10
./etc/rc.d/init.d/functions
logger $0 called with $1
case "$1" in
start)
# set squid vip
/sbin/ipvsadm --set 30 5 60
/sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/sbin/route add -host $SNS_VIP dev eth0:0
/sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
;;
stop)
/sbin/ipvsadm -C
/sbin/ipvsadm -Z
ifconfig eth0:0 down
route del $SNS_VIP
rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
echo "ipvsadm stoped"
;;
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then
echo "ipvsadm stoped"
exit 1
else
echo "ipvsadm OK"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
2. 配置Realserver腳本.
CODE:
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
SNS_VIP=192.168.0.87
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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 $SNS_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
或者采用secondary ip address方式配置
# vi /etc/sysctl.conf
CODE:
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
[code]
#sysctl –p
#ip addr add 192.168.0.87/32 dev lo
#ip add list 查看是否綁定
3. 啟動(dòng)lvs-dr腳本和realserver啟本,在DR上可以查看LVS當(dāng)前狀態(tài):
#watch ipvsadm –ln五.利用Keepalvied實(shí)現(xiàn)負(fù)載均衡和和高可用性
1.配置在主負(fù)載均衡服務(wù)器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf
CODE:
! Configuration File for keepalived
global_defs {
notification_email {
cnseek@gmail.com
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #備份服務(wù)器上將MASTER改為BACKUP
interface eth0
virtual_router_id 51
priority 100 # 備份服務(wù)上將100改為99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.87
#(如果有多個(gè)VIP,繼續(xù)換行填寫.)
}
}
virtual_server 192.168.0.87 80 {
delay_loop 6 #(每隔10秒查詢realserver狀態(tài))
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的連接60秒內(nèi)被分配到同一臺(tái)realserver)
protocol TCP #(用TCP協(xié)議檢查realserver狀態(tài))
real_server 192.168.0.253 80 {
weight 3 #(權(quán)重)
TCP_CHECK {
connect_timeout 10 #(10秒無響應(yīng)超時(shí))
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.0.10 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
2. BACKUP服務(wù)器同上配置,先安裝lvs再按裝keepalived,仍后配置/etc/keepalived/keepalived.conf,只需將紅色標(biāo)示的部分改一下即可.
3. 停掉lvs_dr.sh演示腳本
vi /etc/rc.local
#/usr/local/sbin/lvs-dr.sh 將lvs-dr.sh這個(gè)腳本注釋掉。
#/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr腳本
#/etc/init.d/keepalived start 啟動(dòng)keepalived 服務(wù),keepalived就能利用keepalived.conf 配置文件,實(shí)現(xiàn)負(fù)載均衡和高可用.
整個(gè)LVS負(fù)均衡HA方案,由keepalived.conf一個(gè)文件即可搞定!
4. 查看lvs服務(wù)是否正常
#watch ipvsadm –ln
CODE:
IP Virtual Server version
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 192.168.0.87:80 wrr persistent 60
-> 192.168.0.10:80 Route 3 0 0
-> 192.168.0.253:80 Route 3 0 0#tail –f /var/log/message 監(jiān)聽日志,查看狀態(tài),測試LVS負(fù)載均衡及高可用性是否有效。
5.停Master服務(wù)器的keepalived服務(wù),查看BAKCUP服務(wù)器是否能正常接管服務(wù)。
四.相關(guān)參考
1.LVS 基礎(chǔ)知識(shí)匯總
LVS的算法介紹 http://www.linuxtone.org/viewthread.php?tid=69
學(xué)習(xí)LVS的三種轉(zhuǎn)發(fā)模式 http://www.linuxtone.org/viewthread.php?tid=77
LVS中的IP負(fù)載均衡技術(shù) http://www.linuxtone.org/viewthread.php?tid=68
更多的請(qǐng)到http://www.linuxtone.org 負(fù)載均衡版查看
Keepalived 相關(guān)參考資料。
http://www.keepalived.org/documentation.html
聯(lián)系客服