软件下载:
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
IP列表:
LVS-DR-Master:192.168.9.36
LVS-DR-Backup:192.168.9.4
LVS-DR-VIP:192.168.9.6
Web1-RealServer:192.168.9.48
Web2-RealServer:192.168.9.43
首先保证realserver上web服务都能正常访问:
一、Master和Backup上安装软件:
#ln -s /usr/src/kernels/2.6.18-164.el5-x86_64/ /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 #做成系统启动服务方便管理.
二、配置LVS:
1、LVS-DR,配置LVS脚本实现负载均衡脚本(不用LVS此步可省略):
#vim /usr/local/sbin/lvs-dr.sh
#!/bin/bash
# description: start LVS of DirectorServer
GW=192.168.10.1
#WebSite director VIP.
SNS_VIP=192.168.9.6
SNS_RIP1=192.168.9.48
SNS_RIP2=192.168.9.4
. /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
ifconfig eth0:1 down
route del $SNS_VIP
route del $SS_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脚本:
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.9.6
. /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
三、利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
# [email protected]
# [email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
#VIP1
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.9.6
}
}
virtual_server 192.168.9.6 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.9.48 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.9.43 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
2.配置在从负载均衡服务器上配置keepalived.conf
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
# [email protected]
# [email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
#VIP1
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.9.6
}
}
virtual_server 192.168.9.6 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.9.48 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.9.43 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
配置完以后service keepalived restart
查看lvs服务是否正常
四、测试
#watch ipvsadm –ln
#tail –f /var/log/message 监听日志,查看状态
停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管.
停止其中一台的realserver的httpd服务,wget看是否能正常下载,以及连接是否平均
#for i in {1..10};do wget -S -O /dev/null http://192.168.9.6/index.php;done