当前位置: > CentOS > CentOS服务器 > 负载均衡 >

LVS+KEEPALIVED实现WEB服务器80端口的负载均衡

时间:2014-05-19 00:30来源:linux.it.net.cn 作者:IT网

LVS

LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR)本文是按照VS/DR的方式配置,十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。

Keepalvied

Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现

部署环境:

系统:linux centos 5.8

浮动IP:192.168.1.150

主LVS IP:192.168.1.86

备LVS IP:192.168.1.87

真实服务器WEB1 IP:192.168.1.88

真实服务器WEB2 IP:192.168.1.250

安装与配置步骤

1.主备LVS下载软件包

#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

2.主备LVS安装LVS和Keepalived

#lsmod |grep ip_vs

#uname -r

2.6.18-53.el5PAE

#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux

(执行ln -s 出错请看最后的常见问题处理)

#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

3.主备LVS配置Keepalived
修改配置文件/etc/keepalived/keepalived.conf为如下内容(注意真实环境下将#注释说明的内容去掉)

1 global_defs {
2 notification_email {
3 admin@gmail.com
4 }
5 notification_email_from sns-lvs@gmail.com
6 smtp_server 127.0.0.1
7 router_id LVS_DEVEL
8 }
9  
10 vrrp_instance VI_1 {
11 state MASTER   #备份服务器上将MASTER改为BACKUP    
12 interface eth0
13 virtual_router_id 88
14 priority 100  # 备份服务上将100改为99
15 advert_int 1
16 authentication {
17 auth_type PASS
18 auth_pass 123123
19 }
20 virtual_ipaddress {
21 192.168.1.150
22 #(如果有多个VIP,继续换行填写.)
23 }
24 }
25  
26 virtual_server 192.168.1.150 80 {
27 delay_loop 3 #(每隔3秒查询realserver状态)
28 lb_algo wrr #(lvs 算法)
29 lb_kind DR #(Direct Route)
30 persistence_timeout 3 #(同一IP的连接3秒内被分配到同一台realserver)
31 protocol TCP #(用TCP协议检查realserver状态)
32  
33 real_server 192.168.1.88 80 {
34 weight 3  #(权重)
35 TCP_CHECK {
36 connect_timeout 10 #(10秒无响应超时)
37 nb_get_retry 3
38 delay_before_retry 3
39 connect_port 80
40 }
41 }
42  
43 real_server 192.168.1.250 80 {
44 weight 3
45 TCP_CHECK {
46 connect_timeout 10
47 nb_get_retry 3
48 delay_before_retry 3
49 connect_port 80
50 }
51 }
52 }

4.配置提供真实服务的主机
添加配置文件/etc/realserver/realserver.sh为以下内容

#!/bin/bash

1 SNS_VIP=192.168.1.150
2  
3 /etc/rc.d/init.d/functions
4  
5 case "$1" in
6 start)
7 /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
8 /sbin/route add -host $SNS_VIP dev lo:0
9 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
10 echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
11 echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
12 echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
13 sysctl -p >/dev/null 2>&1
14 echo "RealServer Start OK"
15 ;;
16  
17 stop)
18 /sbin/ifconfig lo:0 down
19 /sbin/route del $SNS_VIP >/dev/null 2>&1
20 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
21 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
22 echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
23 echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
24 echo "RealServer Stoped"
25 ;;
26  
27 *)
28 echo "Usage: $0 {start|stop}"
29 exit 1
30 esac
31  
32 exit 0

5.提供真实服务的主机修改/etc/rc.d/rc.loal

#vi /etc/rc.d/rc.local

添加
/etc/realserver/realserver.sh

6.主备LVS执行

#chkconfig --level keepalived 35 on

7.主备LVS启动keepalived服务

#/etc/init.d/keepalived start

8.提供真实服务的主机运行/etc/realserver/realserver.sh

#chmod +x /etc/realserver/realserver.sh

#/etc/realserver/realserver.sh

9.查看当前LVS的状态

#ipvsadm -ln

常见问题处理:

1.安装时发现没有目录:/usr/src/kernels,安装kernel-devel包可以解决

#yum install kernel-devel

2.安装编译keeepalived时需要支持LVS时才可以做LVS调度:
例如出现下面情况,将不支持lvs
Keepalived configuration
------------------------
Keepalived version : 1.1.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
解决办法,指定kernel目录:
/configure --with-kernel-dir=/usr/src/kernels/2.6.9-55.EL-i686 (rhel4.5_32平台)
./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686/(rhel5.4_32平台)

正确情况:
Keepalived configuration
------------------------
Keepalived version : 1.1.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
然后在make,make install

3.安装编译keeepalived时
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!

#yum install e2fsprogs-devel keyutils-libs-devel libsepol-devel libselinux-devel krb5-devel zlib-devel openssl-devel

参考资料:
http://i-david.iteye.com/blog/1684342
官方文档:
http://www.keepalived.org/documentation.html

(责任编辑:IT)
------分隔线----------------------------