IPVS-DR+keepalived高可用负载均衡集群方案前端的负载调度器采用双机热备方式,Keepalived主要用来监测RealServer的健康状态以及主负载均衡器和备份负载均衡器之间的故障转移,一旦主负载调度器恢复正常工作,主负载调度器重新恢复对集群的资源管理。
系统平台:Redhat 5.4
网络拓扑如下:
服务器IP规划如下:
所需软件包:
1.ipvsadm-1.24-10.i386.rpm (RHEL 5光盘自带)
2.keepalived-1.2.2.tar.gz(目前最新版)
下载地址:http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
一.主负载均衡器(MASTER)的配置
1.修改/etc/hosts文件,增加以下两行内容:
[root@localhost~]# vi /etc/hosts
200.10.1.1 MASTER
200.10.1.2 BACKUP
2.修改主机名,编辑/etc/sysconfig/network文件:
HOSTNAME=MASTER
以上两步非必要步骤。
3.配置本机IP。(略)
4.设置VIP脚本:
[root@lvs-master~] # vi vip.sh
#!/bin/bash
VIP=200.10.1.10
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
5.给vip脚本设置可执行权限:
[root@MASTER~] # chmod a+x vip.sh
运行vip脚本并设置为开机启动:
[root@MASTER~]# ./vip.sh
[root@MASTER~]# echo “/root/vip.sh”>> /etc/rc.local
6.安装与配置并启动ipvsadm:
[root@MASTER~] #rpm –ivh ipvsadm-1.24-10.i386.rpm
[root@MASTER~] # ipvsadm –A –t 200.10.1.10:80 –s rr –p 600
[root@MASTER~]# ipvsadm –a –t 200.10.1.10:80 –r 200.10.1.3:80 –g -w 1
[root@MASTER~]# ipvsadm –a –t 200.10.1.10:80 –r 200.10.1.4:80 –g -w 1
[root@MASTER~]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@MASTER~]# service ipvsadm start
7.安装与配置keepalived:
[root@MASTER~]# tar zxf keepalived-1.2.2.tar.gz
[root@MASTER~]# cd keepalived-1.2.2
[root@MASTER~]# ./configure && make && make install
[root@MASTER~]# find / -name keepalived //查看keepalived安装后产生的相前目录和文件的位置
[root@MASTER~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@MASTER~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@MASTER~]# mkdir /etc/keepalived
[root@MASTER~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@MASTER~]# cp /usr/local/sbin/keepalived /usr/sbin/
7.1.修改keepalived.conf配置文件
[root@MASTER~]# 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
}
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 {
200.10.1.10
}
}
virtual_server 200.10.1.10 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 200.10.1.3 80 {
weight 3
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 200.10.1.4 80 {
weight 3
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
7.2.启动keepalived服务
[root@MASTER~]# service keepalived start
二.备份负载均衡器(BACKUP)的配置
1.IP配置(略)
2.主机名和VIP脚本设定(同MASTER一致)
3.需要安装的软件同MASTER一致,ipvsadm配置一致。
4.keepalived.conf需要修改的地方:
state MASTER #备份服务器上将MASTER改为BACKUP
priority 100 # 备份服务上将100改为99
三.Realserver1(Web)的配置(已经安装好apache)
1.添加以下脚本:
[root@localhost~] # vi lvs.sh
#!/bin/bash
VIP="200.10.1.10"
/sbin/ifconfig eth0 200.10.1.3/24
/sbin/ifconfig lo:0 $VIP braodcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $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
2.给脚本增加可执行权限,然后启动并设置为开机启动:
[root@localhost~] # chmod a+x lvs.sh
[root@localhost~] # ./lvs.sh
[root@localhost~] # echo “/root/lvs.sh”>> /etc/rc.local
3.创建测试页面并启动web服务:
[root@localhost~] # echo “This is Realserver1”> /var/www/html/index.html
[root@localhost~] # service httpd start
四.Realserver2(Web)上的配置(已经安装好apache)
1.添加以下脚本:
[root@localhost~] # vi lvs.sh
#!/bin/bash
VIP="200.10.1.10"
/sbin/ifconfig eth0 200.10.1.4/24
/sbin/ifconfig lo:0 $VIP braodcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $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
2.给脚本增加可执行权限,然后启动并设置为开机启动:
[root@localhost~] # chmod a+x lvs.sh
[root@localhost~] # ./lvs.sh
[root@localhost~] # echo “/root/lvs.sh”>> /etc/rc.local
3.创建测试页面并启动web服务:
[root@localhost~] # echo “This is Realserver2”> /var/www/html/index.html
[root@localhost~] # service httpd start
五.测试及验证
1.分别在两台PC上打开浏览器,输入“http://200.10.1.10”,如果看到的是不同的页面,说明LVS配置成功!

2.可以使用命令ipvsadm –L查看lvs服务器是否正常。
3.tail –f /var/log/message 监听日志,查看状态。
4.停卡MASTER上面的ipvsadm和keepalived服务,看BACKUP是否正常接管MASTER的工作,然后再恢复MASTER的服务,看MASTER是否恢复工作。

参考文章:NetSeek http://www.linuxtone.org
利用LVS+Keepalived 实现高性能高可用负载均衡