一、为什么要使用负载均衡技术?
1、系统高可用性
2、系统可扩展性
3、负载均衡能力
LVS+keepalived能很好的实现以上的要求,LVS提 供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后 很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的 配置文件,并能实现无缝配置变更!
二、LVS+Keepalived介绍
1、 LVS
LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡。LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器。目前有三种IP负 载均衡技术(VS/NAT、VS/TUN和VS/DR);八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。
2、 keepalived
Keepalived 是运行在lvs 之上,它的主要功能是实现真实机的故障隔离及负载均衡器间的失败 切换,提高系统的可用性
三、LVS+keepalived负载均衡架构
![](http://blogimg.chinaunix.net/blog/upfile2/100507121554.jpg)
四、LVS+keepalived的安装和配置
1、配置环境
System OS:CentOS release 5.4
Software:ipvsadm-1.24.tar.gz, keepalived-1.1.19.tar.gz
2、信息列表
名称 | IP |
LVS-Master | 192.168.5.21 |
LVS-BACKUP | 192.168.5.22 |
LVS-VIP | 192.168.5.100 |
Realserver1 | 192.168.5.31 |
Realserver2 | 192.168.5.32 |
Realserver3 | 192.168.5.33 |
3、安装lvs
分别在backup lvs和master lvs上安装
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz 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
4、安装keepalived
分别在backup lvs和master lvs上安装
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz tar zxvf keepalived-1.1.19.tar.gz cd keepalived-1.1.19 ./configure --prefix=/usr/local/keepalived make make install cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ mkdir /etc/keepalived
5、配置keepadlived
LVS-Master的配置文件如下,LVS-Backup的配置文件需要按照注释更改数值。
[root@linux ~]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc #smtp_server 192.168.200.1 #smtp_connect_timeout 30 router_id LVS_DEVEL_MASTER #备份服务器改为 LVS_DEVEL_BACKUP } vrrp_sync_group test { #设置vrrp组 group { loadbalance } } vrrp_instance VI_1 { state MASTER #备份服务器改为 BACKUP interface eth2 virtual_router_id 51 priority 150 #备份服务器改为 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.100 #可以为多个VIP,各占一行 } } virtual_server 192.168.5.100 80 { delay_loop 6 lb_algo rr #负载均衡算法 rr|wrr|lc|wlc|sh|dh|lblc lb_kind DR #可以为 NAT|DR|TUN nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP #TCP|UDP real_server 192.168.5.31 80 { weight 100 #权重,越大就越容易被非配 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 #端口号 } } real_server 192.168.5.32 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.5.33 80 { weight 100 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } } virtual_server 192.168.5.100 5060 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol UDP real_server 192.168.5.41 5060 { weight 100 MISC_CHECK { misc_path "/etc/keepalived/UDP_CHECK.sh 192.168.5.41 5060" #UDP检测脚本 misc_timeout 10 } } real_server 192.168.5.42 5060 { weight 100 MISC_CHECK { misc_path "/etc/keepalived/UDP_CHECK.sh 192.168.5.42 5060" misc_timeout 10 } } } }
UDP_CHECK.sh的脚本文件如下
#!/bin/bash /usr/bin/nc -uz -w1 $1 $2 | grep succeeded >/dev/null exit $?
6、Realserver的配置
三台客户端的脚本都一样!
cat /etc/rc.d/init.d/realserver.sh#!/bin/bash #description : Start Real Server VIP=192.168.5.100 #Virtual Server IP ./etc/rc.d/init.d/functions case "$1" in start) echo " Start LVS of Real Server" /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up #注意子网掩码 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 -arp 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 ;; stop) /sbin/ifconfig lo:0 down echo "close LVS of Real Server" 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 sysctl -p ;; *) echo "Usage: $0 {start|stop}" exit 1 esac
7、 测试
1) 首先测试各个readserver,确定各个readserver都能正常访问,测试realserver.sh脚本,看启动后 能不能绑定lvs vip,停止后能不能去除绑定的vip
2) 测试lvs能否进行负载均衡转发,利用ipvsadm命令查看,如果想只当当前的 请求转发到哪个服务器去了,可以用ipvsadm加选项来查看,其完整形式为:ipvsadm –lcn | grep 192.168.5.31
3) 停掉主lvs看lvs backup是否接管!
4) 更详细信息请查看日志,tail -f /var/log/messages