以下内容均参考这篇文章:,IP地址和部分内容做了修改和增加。
一. LVS+Keepalived 介绍
LVS
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。
Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
二. 网站负载均衡拓朴图
系统:CentOS5.2
LVS-DR-Master 10.129.60.185
LVS-DR-BACKUP 10.129.60.188
LVS-DR-VIP 10.129.60.199
WEB1-Realserver 10.129.60.197
WEB2-Realserver 10.129.60.198
GateWay 10.129.60.129
三. 安装LVS和Keepalvied软件包
安装LVS
#yum install ipvsadm
安装keepalived
#wget
#ln -s /usr/src/kernels/2.6.18-92.el5-i686 /usr/src/linux
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#CFLAGS="-O3 -pipe" ./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/
#chkconfig --level 23456 keepalived on #做成系统启动服务方便管理.
四. 配置RealServer脚本
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek [url=][/url]
SNS_VIP=10.129.60.199
. /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
#chmod 755 /usr/local/sbin/realserver.sh
#echo '/usr/local/sbin/realserver.sh start' >> /etc/rc.local
五.利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
产生用于健康检测的url的digest值
#genhash -s 10.129.60.197 -p 80 -u /hb.html
MD5SUM = 348099c57ebe33689c82198672f86984
#genhash -s 10.129.60.198 -p 80 -u /hb.html
MD5SUM = 348099c57ebe33689c82198672f86984
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from
smtp_server 10.129.60.11
smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state BACKUP #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 99 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.129.60.199
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 10.129.60.199 80 {
delay_loop 5 #(每隔6秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
# persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
sorry_server 127.0.0.1 80 #realserver全部失败,vip指向本机80端口。在VIP维护的时候非常有用
real_server 10.129.60.197 80 {
weight 3 #(权重)
HTTP_GET { #通过检查指定url的校验和是否正确来判断RealServer的健康状态
url {
path /hb.html
digest 348099c57ebe33689c82198672f86984 #刚才genhash产生的md5值
}
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.129.60.198 80 {
weight 3
HTTP_GET {
url {
path /hb.html
digest 348099c57ebe33689c82198672f86984 #刚才genhash产生的md5值
}
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
3. 启动keepalived 服务
#/etc/init.d/keepalived start #keepalived就能利keepalived.conf 配置文件,实现负载均衡和高可用.
4. 启动RealServer
#/usr/local/sbin/realserver.sh start
#service httpd start
5. 测试lvs服务是否正常
5.1 通过浏览器访问是否正常
5.2 多次刷新浏览器,在主LVS上看连接数变化
#watch ipvsadm -l
5.3 停止主LVS上的keepalived 看看备份keepalived是否正常接管服务
六.相关参考
1.LVS 基础知识汇总
LVS的算法介绍
学习LVS的三种转发模式
LVS中的IP负载均衡技术
更多的请到 负载均衡版查看
2. keepalived使用gratuitous ARP技术实现VIP的漂移,无论在主lvs服务器还是备份lvs服务器上是看不到vip的。
gratuitous ARP的知识请参考:
阅读(3261) | 评论(6) | 转发(0) |