前言部分来源于网络

背景:
        随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。
1、LVS+Keepalived 介绍
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
十种调度算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
2、网站负载均衡拓扑图
CentOS6.2下搭建LVS(DR)+Keepalived实现高性能高可用负载均衡服务_第1张图片
3、在2台Director Server(LVS_Master与)上分别配置LVS+Keepalived
实验环境是CentOS6.2,并且配置YUM
keepalived:http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
ipvsadm: http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz 注意:(for kernel 2.6.28-rc3 or later) 
popt-static:http://mirror.centos.org/centos/6/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm
首先下载popt-static:具体原因可以查看http://crazylinux.blog.51cto.com/259244/811591
[root@host1 ~]# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm  
[root@host1 ~]# yum -y install kernel-devel make gcc openssl-devel  libnl* popt*
[root@host1 ~]# ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux

[root@host1 ~]# tar -zxvf ipvsadm-1.26.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/ipvsadm-1.26/
[root@host1 ipvsadm-1.26]# make
[root@host1 ipvsadm-1.26]# make install
[root@host1 ~]# tar -zxvf keepalived-1.2.2.tar.gz -C /usr/src/
[root@host1 ~]# cd /usr/src/keepalived-1.2.2/
[root@host1 keepalived-1.2.2]# ./configure
[root@host1 keepalived-1.2.2]# make
[root@host1 keepalived-1.2.2]# make install
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@host1 keepalived-1.2.2]# mkdir /etc/keepalived
[root@host1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@host1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
4、在2台Director Server(LVS_Master与)上分别配置Keepalived

   
   
   
   
  1. [root@host1 ~]# less /etc/keepalived/keepalived.conf   
  2. ! Configuration File for keepalived  
  3. global_defs {  
  4.    router_id LVS_MASTER #网上资料说这个值也需要修改,具体不详,之前我们线上的主备就一直是一样的 ^ ^还是修改一下吧!  
  5. }  
  6. vrrp_instance VI_1 {  
  7.     state MASTER #LVS_Backup上修改成BACKUP  
  8.     interface eth0  
  9.     virtual_router_id 51  
  10.     priority 100 #LVS_Backup上修改成80 
  11.     advert_int 1  
  12.     authentication {  
  13.         auth_type PASS  
  14.         auth_pass 1111  
  15.     }  
  16.     virtual_ipaddress {  
  17.         192.168.10.100  
  18.     }  
  19. }  
  20. virtual_server 192.168.10.100 80 {  
  21.     delay_loop 6  
  22.     lb_algo rr  
  23.     lb_kind DR  
  24.     nat_mask 255.255.255.0  
  25.     persistence_timeout 50  
  26.     protocol TCP  
  27.  
  28.     real_server 192.168.10.3 80 {  
  29.         weight 1  
  30.         TCP_CHECK {  
  31.         connect_timeout 10  
  32.         nb_get_retry 3  
  33.         connect_port 80  
  34.         }  
  35.     }  
  36.     real_server 192.168.10.4 80 {  
  37.         weight 1  
  38.         TCP_CHECK {  
  39.         connect_timeout 10  
  40.         nb_get_retry 3  
  41.         connect_port 80  
  42.         }  
  43.     }  

BACKUP服务器同上配置,先安装lvs再按装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可。
 5、分别在2台Real Server上面编写脚本并启动

   
   
   
   
  1. [root@host3 init.d]# cat /etc/init.d/realserver.sh      
  2. #!/bin/bash  
  3. SNS_VIP=192.168.10.100           
  4. . /etc/rc.d/init.d/functions           
  5. case "$1" in 
  6. start)  
  7.         ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP  
  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. stop)  
  17.         ifconfig lo:0 down  
  18.         route del $SNS_VIP >/dev/null 2>&1  
  19.         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
  20.         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce  
  21.         echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore  
  22.         echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce  
  23.         echo "RealServer Stoped" 
  24.         ;;  
  25.         *)  
  26.         echo "Usage: $0 {start|stop}" 
  27.         exit 1  
  28. esac           
  29. exit 0      

6、在2台RealServer中分别配置HTTP并启动realserver.sh
[root@host3 ~]# yum -y install httpd
[root@host3 ~]# cd /var/www/html/
[root@host3 html]# cat index.html 

RealServer 192.168.10.3


[root@host3 html]# /etc/init.d/httpd start
[root@host3 ~]# /etc/init.d/realserver.sh start

另一台机器配置一样,过程略、、、
7、测试:
分别启动Keepalived
[root@host1 ~]# chkconfig keepalived on
[root@host1 ~]# /etc/init.d/keepalived restart
[root@host1 ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.10.100:http rr persistent 50
  -> 192.168.10.3:http            Route   1      0          0         
  -> 192.168.10.4:http            Route   1      0          0   
先关闭LVS_Master的Keepalived,观察LVS_Backup的日志
[root@host2 ~]# tailf /var/log/messages
Mar 21 07:22:28 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 21 07:22:29 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.100 added
Mar 21 07:22:29 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.10.100
在测试机上访问:
CentOS6.2下搭建LVS(DR)+Keepalived实现高性能高可用负载均衡服务_第2张图片

启动LVS_Master,在查看LVS_Backup日志
[root@host2 ~]# tailf /var/log/messages
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Mar 21 07:26:20 host2 Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Mar 21 07:26:20 host2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.10.100 removed
一切访问正常~!祝各位好运!~