原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://navyaijm.blog.51cto.com/4647068/809397
一、实验环境: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
二、安装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上执行安装。
三、配置keepalived.conf:内容如下是lvs-master配置
也可以参考配置:
http://chinaapp.sinaapp.com/download/keepalived.conf 可以直接打开
! Configuration File for keepalived
global_defs {
}
# VIP1
vrrp_instance VI_1 {
}
#REAL_SERVER_1
virtual_server 192.168.2.100 80 {
}
#REAL_SERVER_2
}
注意***Lvs-backup端同样配置,只需要更改state MASTER为state BACKUP,修改priority 100为priority 90即可。
四、分别在web1、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
五、最后启动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又会变成原来的状态!
所安装的脚本可以在这里下载: