LVS+Keepalived 实现WEB负载均衡和DNS负载均衡
一、安装环境及功能说明:
操作系统:RHEL5.4
软件:ipvsadm-1.24.tar.gz,keepalived-1.1.18.tar.gz
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。
功能:两台机器做HA,负责web集群和DNS集群的虚拟server高可用。为了防止频繁切换,两个负载均衡服务器都设置成辅
负载均衡服务器,权值高的竞争为主负载均衡服务器,在权值高的辅负载均衡服务器失效并且恢复后不接管权值低的辅负载均衡器,
只有在权值低的辅负载均衡服务器失效后才接管。
三台机器做真实server,其中一台既做web集群的真实server,又做DNS集群的真实server。
二. 网站负载均衡拓朴图
IP信息列表:
名称 IP
LVS-DR-Master 192.168.168.101
LVS-DR-BACKUP 192.168.168.102
LVS-DR-VIP 192.168.168.201
LVS-DR-VIP 192.168.168.202
WEB1-Realserver 192.168.168.111
WEB2-Realserver 192.168.168.112
DNS-Realserver 192.168.168.113
DNS-Realserver 192.168.168.112
GateWay 192.168.168.1
三. 安装LVS和Keepalvied软件包
1. 下载相关软件包
#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-1.1.18.tar.gz
2. 在LVS-DR-Master和LVS-DR-BACKUP上分别安装LVS和Keepalived
#lsmod |grep ip_vs
#uname -r
2.6.18-164.el5
#ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#find / -name ipvsadm # 查看ipvsadm的位置
# cd ..
#tar zxvf keepalived-1.1.18.tar.gz
#cd keepalived-1.1.18
#./configure ##如果出现错误configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
##这是缺了openssl-devel这个包。
#make
#make install
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# chmod 700 /etc/rc.d/init.d/keepalived
#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 #做成系统启动服务方便管理.
#chkconfig keepalived on
四. 配置LVS实现负载均衡
1.配置在主负载均衡服务器(LVS-DR-Master)上配置keepalived.conf
#vi /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 192.168.200.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# VIP1
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 55
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.168.201
192.168.168.202
}
}
virtual_server 192.168.168.201 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.168.111 80 {
weight 5
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.168.112 80 {
weight 5
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
! Balancer-Set for udp/53
virtual_server 192.168.168.202 53 {
delay_loop 10
lb_algo wrr
lb_kind DR
protocol UDP
real_server 192.168.168.112 53 {
weight 5
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
real_server 192.168.168.113 53 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 53
}
}
}
注意:如果出现如下错误:
bogus VRRP packet received on eth0 !!!
Apr 1 05:18:21 lvs-master Keepalived_vrrp: VRRP_Instance(VI_1) Dropping received VRRP packet...
Apr 1 05:18:22 lvs-master Keepalived_vrrp: receive an invalid ip number count associated with VRID!
解决方法:改变配置文件/etc/keepalived/keepalived.conf中virtual_router_id为另一个值即可
2.配置在辅负载均衡服务器上配置keepalived.conf
BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf。只有state和priority不同。
state BACKUP
priority 100
3.配置真实server:
(1)192.168.168.112和192.168.168.113上配置文件内容。
#vim /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek [url=http://www.linuxtone.org]http://www.linuxtone.org[/url]
CDN_VIP1=192.168.168.201
CDN_VIP2=192.168.168.202
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $CDN_VIP1 netmask 255.255.255.255 broadcast $CDN_VIP1 up
/sbin/route add -host $CDN_VIP1 dev lo:0
ifconfig lo:1 $CDN_VIP2 netmask 255.255.255.255 broadcast $CDN_VIP2 up
/sbin/route add -host $CDN_VIP2 dev lo:1
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 $CDN_VIP1 >/dev/null 2>&1
ifconfig lo:1 down
route del $CDN_VIP2 >/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
#chmod 700 /usr/local/sbin/realserver.sh
#/usr/local/sbin/realserver.sh start|stop
增加启动:
#vim /etc/rc.local
/usr/local/sbin/realserver.sh start
/etc/init.d/named start
(2)192.168.168.111配置文件内容:
#vim /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek [url=http://www.linuxtone.org]http://www.linuxtone.org[/url]
CDN_VIP=192.168.168.201
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $CDN_VIP netmask 255.255.255.255 broadcast $CDN_VIP up
/sbin/route add -host $CDN_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 $CDN_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
#chmod 700 /usr/local/sbin/realserver.sh
#/usr/local/sbin/realserver.sh start|stop
增加启动:
#vim /etc/rc.local
/usr/local/sbin/realserver.sh start
五.测试
1、启动所有真实机的realserver.sh,http和DNS服务。
#/usr/local/sbin/realserver.sh start
#service httpd start
#service named start #在192.168.168.111上不启动此服务
2、启动LVS-DR-Master和LVS-DR-BACKUP的keepalived。
#service keepalived start
3、查看lvs服务是否正常
#ip ad sh查看VIP
#watch ipvsadm –ln
Every 2.0s: ipvsadm ln
Thu Apr 1 11:20:05 2010
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
6dTCP 192.168.168.201:80 rr persistent 6
-> 192.168.168.112:80 Route 5 0 0
-> 192.168.168.111:80 Route 5 0 0
UDP 192.168.168.202:53 wrr
-> 192.168.168.113:53 Route 100 0 0
-> 192.168.168.112:53 Route 5 0 0
#tail –f /var/log/messages 监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。
4.停止主负载均衡服务器的keepalived服务,查看辅负载均衡服务器是否能正常接管服务。