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)。

Keepalvied
               Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现


  1. LVS-DR-Master        61.164.122.6        

  2. LVS-DR-BACKUP        61.164.122.7        

  3. LVS-DR-VIP        61.164.122.8        

  4. WEB1-Realserver        61.164.122.9        

  5. WEB2-Realserver        61.164.122.10        

  6. GateWay        61.164.122.1   

安装LVS和Keepalvied软件包
下载相关软件包
  1.      #mkdir /usr/local/src/lvs

  2.      #cd /usr/local/src/lvs

  3.      #wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

  4.      #wgethttp://www.keepalived.org/software/keepalived-1.1.15.tar.gz


 安装LVS和Keepalived

  1. #lsmod |grep ip_vs

  2.         #uname -r

  3.         2.6.18-53.el5PAE

  4.         #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/  /usr/src/linux


  5.         #tar zxvf ipvsadm-1.24.tar.gz

  6.         #cd ipvsadm-1.24

  7.         #make && make install

  8.         #find / -name ipvsadm  # 查看ipvsadm的位置


  9.         #tar zxvf keepalived-1.1.15.tar.gz

  10.         #cd keepalived-1.1.15

  11.         #./configure  && make && make install

  12.         #find / -name keepalived  # 查看keepalived位置               


  13.         #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

  14.         #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

  15.         #mkdir /etc/keepalived

  16.         #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

  17.         #cp /usr/local/sbin/keepalived /usr/sbin/

  18.         #service keepalived start|stop     #做成系统启动服务方便管理.


 配置LVS实现负载均衡
  vi /etc/sysctl.conf
  1.           net.ipv4.conf.lo.arp_ignore = 1

  2.           net.ipv4.conf.lo.arp_announce = 2

  3.           net.ipv4.conf.all.arp_ignore = 1

  4.           net.ipv4.conf.all.arp_announce = 2

  5.           sysctl   -p

       ip addr add 61.164.122.8/32 dev lo

利用Keepalvied实现负载均衡和和高可用性
  1.配置在主负载均衡服务器上配置keepalived.conf
  #vi /etc/keepalived/keepalived.conf

  1.   ! Configuration File for keepalived


  2. global_defs {

  3.    notification_email {

  4.          [email protected]

  5.    }

  6.    notification_email_from [email protected]

  7.    smtp_server 127.0.0.1

  8.   # smtp_connect_timeout 30

  9.    router_id LVS_DEVEL

  10. }


  11. # 20081013 written by :netseek

  12. # VIP1

  13. vrrp_instance VI_1 {

  14.     state MASTER             #备份服务器上将MASTER改为BACKUP   

  15.     interface eth0

  16.     virtual_router_id 51

  17.     priority 100    # 备份服务上将100改为99

  18.     advert_int 1

  19.     authentication {

  20.         auth_type PASS

  21.         auth_pass 1111

  22.     }

  23.     virtual_ipaddress {

  24.         61.164.122.8   

  25.         #(如果有多个VIP,继续换行填写.)

  26.     }

  27. }


  28. virtual_server 61.164.122.8 80 {

  29.     delay_loop 6                  #(每隔10秒查询realserver状态)

  30.     lb_algo wrr                  #(lvs 算法)

  31.     lb_kind DR                  #(Direct Route)

  32.     persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)

  33.     protocol TCP                #(用TCP协议检查realserver状态)


  34.     real_server 61.164.122.9 80 {

  35.         weight 3               #(权重)

  36.         TCP_CHECK {

  37.         connect_timeout 10       #(10秒无响应超时)

  38.         nb_get_retry 3

  39.         delay_before_retry 3

  40.         connect_port 80

  41.         }

  42.     }

  43.     real_server 61.164.122.10 80 {

  44.         weight 3

  45.         TCP_CHECK {

  46.         connect_timeout 10

  47.         nb_get_retry 3

  48.         delay_before_retry 3

  49.         connect_port 80

  50.         }

  51.      }

  52. }

#/etc/init.d/keepalived start   启动keepalived 服务,keepalived就能利用keepalived.conf 配置文件,实现负载均衡和高可用.
     整个LVS负均衡HA方案,由keepalived.conf一个文件即可搞定!


 

你可能感兴趣的:(linux,LVS,KeepAlive)