1背景:
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
2LVS+Keepalived 介绍
2.1LVS
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。
2.2Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
3网站负载均衡拓朴图
4IP及安装信息表
名称 IP 安装软件
LVS-DR-VIP 192.168.2.44
LVS-DR-Master 192.168.2.45 Ipvsadm + keepalived
LVS-DR-BACKUP 192.168.2.46 Ipvsadm + keepalived
WEB1-Realserver 192.168.2.47 Apache+Realserver脚本
WEB2-Realserver 192.168.2.48 Apache+Realserver脚本
GateWay 192.168.2.1
5安装LVS和Keepalvied软件包
5.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.15.tar.gz
5.2安装LVS和Keepalived
5.2.1安装LVS
#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
5.2.2安装Keepalived
#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 #做成系统启动服务方便管理.
6利用Keepalvied实现负载均衡和和高可用性
6.1配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
cnseek@gmail.com
# failover@firewall.loc
# sysadmin@firewall.loc
}
notification_email_from sns-lvs@gmail.com
smtp_server 127.0.0.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
# 20081013 written by :netseek
# VIP1
vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 100 # 备份服务上将100改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.44
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 192.168.2.44 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wrr #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.2.47 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.2.48 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
6.2BACKUP服务器配置
BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
7 查看lvs服务是否正常
#watch ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.44:80 wrr persistent 60
-> 192.168.2.48:80 Route 3 0 0
-> 192.168.2.47:80 Route 3 0 0
8Realserver配置
8.1配置Realserver脚本
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
SNS_VIP=192.168.2.44
. /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
8.2将此脚本添加到开机启动
chmod +x /usr/local/sbin/realserver.sh
vi /etc/rc.local
/usr/local/sbin/realserver.sh start
8.3查看Realserver的地址信息
此时会有lo:0 这个IP 为192.168.2.44 这个信息。
9测试
9.1LVS-DR的切换测试
在LVS-DR-Master中service keepalived stop 这时LVS-DR-BACKUP 会接管。在LVS-DR-BACKUP中tail -f /var/log/messages 会出现以下信息。
在这个过程ping 192.168.2.44 (VIP) 会出现一两个掉包。
在IE 中访问http://192.168.2.44 不会受到影响。
再启动LVS-DR-Master的keepalived (service keepalived start) 这时LVS-DR-Master 将再次接管服务器。
9.2测试LVS的负载均衡
查看WEB1-Realserver 或者WEB1-Realserver的apache的access_log 你会发现同一IP 60秒内访问http://192.168.2.44 (vip)的请求会转发给WEB1-Realserver或WEB1-Realserver。如果关闭WEB1-Realserver的apache 服务。所有的访问http://192.168.2.44 (vip)的请求会转发WEB2-Realserver 在日志里60秒内会出现多个IP的访问请求。
在此表明LVS的负载均衡功能的实现