Lvs+keepalived 高可用性負載均衡配置linux
前言* 隨着互聯網的發展,提供用戶訪問的web服務器,必需要保證天天24不間斷服務,訪問量不斷增長,有什麼好的web架構既能實現高可用性負載均衡,並且價格又是免費的呢?答案有木有?有!lvs+keepalived 是不錯的選擇!
1、實驗環境:4臺centos 5.4 ,以及簡單的拓撲圖:
LVS-Master 192.168.2.108
LVS-BACKUP 192.168.2.109
LVS-DR-VIP
192.168.2.100
WEB1-Realserver 192.168.2.79
WEB2-Realserver 192.168.2.80
2、安裝ipvsadm+keepalived,用腳本自動安裝:
因爲咱們使用的是lvs+keepalived,因此這裏不須要配置lvs-dr腳本,直接在keepalived.conf裏面配置便可!
#!/bin/sh
###腳本編寫目的:自動安裝lvs+keepalived
###編寫時間: 2011
年7月6日17:25:39
###初稿人:wugk
###定義變量
DIR1=/usr/src
DIR2=/usr/local
cat << EOF
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++Welcome to use Linux installed a key LVS+KEEPALIVED shells scripts
+++++++++++++++++++++*************************++++++++++++++++++++++++
EOF
if
[ $UID -ne 0 ];then
echo 「This script must use root user ,please exit……」
sleep 2
exit 0
fi
download ()
{
cd $DIR1 && wget -c http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
if
[ $? = 0 ];then
echo "Download LVS Code is OK!"
else
echo "Download LVS Code is failed,Please check!"
exit 1
fi
}
ipvsadm_install ()
{
ln -s $DIR1/kernels/2.6.* $DIR1/linux
cd $DIR1 && tar xzvf ipvsadm-1.24.tar.gz &&cd ipvsadm-1.24 && make && make install
if
[ $? -eq 0 ];then
echo "Install ipvsadm success,please waiting install keepalived ..............."
else
echo "Install ipvsadm failed ,please check !"
exit 1
fi
}
keepalived_install ()
{
cd $DIR1 && tar -xzvf keepalived-1.1.15.tar.gz &&cd keepalived-1.1.15 && ./configure && make && make install
if
[ $? -eq 0 ];then
echo "Install keepalived success,please waiting configure keepalived ..............."
else
echo "Install keepalived failed ,please check install version !"
exit 1
fi
}
######若是以上軟件包編譯報錯的話,請檢查相關的版本跟系統版本之間的關係,而後手動下載安裝.
keepalived_config ()
{
cp $DIR2/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp $DIR2/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived &&cp $DIR2/etc/keepalived/keepalived.conf /etc/keepalived/ && cp $DIR2/sbin/keepalived /usr/sbin/
if
[ $? -eq 0 ];then
echo "Keepalived system server config success!"
else
echo "Keepalived system server config failed ,please check keepalived!"
exit 1
fi
}
PS3="Please select Install Linux Packages:"
select option in download ipvsadm_install keepalived_install keepalived_config
do
$option
done
以上腳本分別在lvs-master和lvs-backup上執行安裝。
3、配置keepalived.conf:內容以下是lvs-master配置
! Configuration File for keepalived
global_defs {
notification_email {
wgkgood@163.com
}
notification_email_from wgkgood@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.100
}
}
#REAL_SERVER_1
virtual_server 192.168.2.100 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.2.79 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
#REAL_SERVER_2
real_server 192.168.2.80 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
注意***Lvs-backup端一樣配置,只須要更改state MASTER爲state BACKUP,修改priority 100爲priority 90便可。
4、分別在web一、web2上配置好apache,而後分別執行以下腳本:
以下的VIP1指的是lvs-dr-vip地址,及對外提供訪問的虛擬ip:
#!/bin/sh
PS3="Please Choose whether or not to start a realserver VIP1 configuration:"
select i in "start" "stop"
do
case "$i" in
start)
read -p "Please enter the virtual server IP address:" VIP1
ifconfig lo:0 $VIP1 netmask 255.255.255.255 broadcast $VIP1
/sbin/route add -host $VIP1 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"
exit 0
;;
stop)
ifconfig lo:0 down
route del $VIP1 >/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"
exit 1
;;
*)
echo "Usage: $0 {start|stop}"
exit 2
esac
done
腳本會提示是否啓動,按1即啓動,而後輸入vip地址 192.168.2.100 ,用ifconfig你會看到:lo:0的ip即表示配置ip成功。
lo:0
Link encap:Local Loopback
inet addr:192.168.2.100 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
5、最後啓動lvs-master、lvs-backup上面的keepalived服務便可:
/etc/init.d/keepalived restart 查看lvs-master日誌:
在瀏覽器裏面訪問
http://192.168.2.100/ 便可!當你宕掉lvs-master後,lvs-backup會接管變成lvs-master提供服務,當lvs-master故障恢復後,lvs-backup又會變成原來的狀態!
所安裝的腳本能夠在這裏下載:
本文出自 「【煙雨樓臺】」 博客,請務必保留此出處http://wgkgood.blog.51cto.com/1192594/605322web