...
分类: LINUX
2012-02-15 15:57:46
前言* 随着互联网的发展,提供用户访问的web服务器,必须要保证每天24小时不间断服务,访问量及各种需求不断增加,有什么好的web架构既能实现高可用性负载均衡,而且价格又是免费的呢?答案有木有?有!Lvs+keepalived将能够实现以上需求。
一、实验环境: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
二、安装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 administrator or root user ,please exit …….”
sleep 2
exit 0
fi
download ()
{
cd $DIR1 && wget -c
if
[ $? = 0 ];then
echo "Download LVS Code is OK!"
else
echo "Download LVS Code is failed,Please check!"
exit 1
fi
}
ipvsadm ()
{
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 ()
{
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
}
keepconfig ()
{
cp $DIR2/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp
$DIR2/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p
/etc/keepali
ved &&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 LVS Packages:"
select option in download ipvsadm keepalived keepconfig
do
$option
done
以上脚本分别在lvs-master和lvs-backup上执行安装。
三、配置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 99即可。
四、分别在web1、web2上配置好apache,然后分别执行如下脚本ok!
如下的VIP1指的是lvs-dr-ip地址,及对外提供访问的虚拟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
最后启动lvs-master、lvs-backup上面的keepalived服务器即可:
/etc/init.d/keepalived restart 查看lvs-master日志: