LVS+keepalived


OS: Centos 6.4 Bit64

Lvs vip: 172.16.10.188

LvsKeepalived –master:172.16.10.160

Lvskeepalived –backup: 172.16.10.161

realserverWeb1:172.16.10.162

realserverWeb2:172.16.10.163

说明:http://blogimg.chinaunix.net/blog/upfile2/100507121554.jpg


软件:ipvsadm   keepalived

Lvs Keepalived –masteLvs keepalived –backup安装

一.ln -s/usr/src/kernels/2.6.32-358.6.2.el6.x86_64/ /usr/src/linux   和内核结合

rpm -ivh ipvsadm-1.25-9.el6.x86_64.rpm

/etc/rc.d/init.d/ipvsadmrestart

二.

三.wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz

四.tar zxvf  keepalived-1.2.6.tar.gz

五.cd keepalived-1.2.6

./configure --prefix=/usr/local/keepalived

六.make -j 2  && make install          二个CPU编译

七.

八.keepalived做成启动脚务,方便管理:

cp/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

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

cp/usr/local/keepalived/sbin/keepalived /usr/sbin/

mkdir /etc/keepalived

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

#service keepalived start|stop

开启负载服务器路由机制,分别在masterbackup上操作:

     vi/etc/sysctl.conf

     net.ipv4.ip_forward = 1

执行sysctl  -p,立即生效。

Keealived的配置:

分别在主从负载均衡服务器上配置/etc/keepalived/keepalived.conf,内容分别如下:

Lvs Keepalived –master配置.

global_defs {

  notification_email {

info@139.com#email通知

  }

  notification_email_from  info@139.com

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS1                 #设置lvsid,在一个网络内应该是唯一的

}


vrrp_instanceVI_1 {

   state MASTER             #lvs的状态,MASTERBACKUP两种,必须大写

   interface eth0              #设置对外服务的接口

   virtual_router_id 51         #设置虚拟路由表示

   priority 100                #设置优先级,数值越大,优先级越高

   advert_int 1               #设置同步时间间隔

   authentication {             #设置验证类型和密码

       auth_type PASS

       auth_pass 1111

   }

   virtual_ipaddress {           #设置lvs vip,如果有多个VIP,继续换行填写.


       172.16.10.188

   }

}

virtual_server  172.16.10.188 80 {

   delay_loop 6                 #健康检查时间间隔

   lb_algo rr                    #负载均衡调度算法,八种.

   lb_kind DR                   #负载均衡转发规则,DR(改写请求MAC,传到真实服务

                               器,并直接返回给客户,需同一网段)NAT(地址转换,地

                               址重写再返回给客户)

                               TUN(类似NAT,后端操作系统需支持"IP Tunneling"协议)

   persistence_timeout 50         #设置会话保持时间,对bbs等很有用

   protocol TCP                  #协议


   real_server 172.16.10.162 80 {

       weight 1                     #设置权重

       TCP_CHECK {

       connect_port 80

       connect_timeout 30            #(30秒无响应超时)

   }

   }


   real_server 172.16.10.163 80 {

       weight 1

       TCP_CHECK {

       connect_port 80

       connect_timeout 30

   }

   }

}


Lvs keepalived –backup配置。

global_defs {

  notification_email {

info@139.com#email通知

  }

  notification_email_from  info@139.com

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS2                 #设置lvsid,在一个网络内应该是唯一的

}


vrrp_instanceVI_1 {

   state BACKUP             #lvs的状态,MASTERBACKUP两种,必须大写

   interface eth0              #设置对外服务的接口

   virtual_router_id 51         #设置虚拟路由表示

   priority 99                #设置优先级,数值越大,优先级越高

   advert_int 1               #设置同步时间间隔

   authentication {             #设置验证类型和密码

       auth_type PASS

       auth_pass 1111

   }

   virtual_ipaddress {          #设置lvs vip,如果有多个VIP,继续换行填写.

       172.16.10.188

   }

}

virtual_server  172.16.10.188 80 {

   delay_loop 6                 #健康检查时间间隔

   lb_algo rr                   #负载均衡调度算法,rr,wrr,lc,wlc,lblc,lblcr,dh,sh

   lb_kind DR                   #负载均衡转发规则,DR(改写请求MAC,传到真实服务器,

                               并直接返回给客户,需同一网段)NAT(地址转换,地址重写

                               再返回给客户)TUN(同上)


   persistence_timeout 50           #设置会话保持时间,对bbs等很有用

   protocol TCP                     #协议


   real_server 172.16.10.162 80 {

       weight 1                      #设置权重

       TCP_CHECK {

       connect_port 80

       connect_timeout 30            #(30秒无响应超时)

   }

   }


   real_server 172.16.10.163 80 {

       weight 1

       TCP_CHECK {

       connect_port 80

       connect_timeout 30

   }

   }

}


Realserver的配置,两台web服务器增加如下脚本:

chmod +x /etc/rc.d/init.d/realserver.sh

/etc/rc.d/init.d/realserver.sh  start

echo /etc/rc.d/init.d/realserver.sh  start >> /etc/rc.local


cat /etc/rc.d/init.d/realserver.sh

#!/bin/bash

# description: Config realserver lo andapply noarp

SNS_VIP=172.16.10.188

. /etc/rc.d/init.d/functions

case "$1" in

start)

      ifconfiglo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP

      /sbin/routeadd -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)

      ifconfiglo:0 down

      routedel $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}"

      exit1

esac

exit 0


lo:0     Link encap:Local Loopback

        inet addr:172.16.10.188 Mask:255.255.255.255

         UPLOOPBACK RUNNING  MTU:16436  Metric:1

查看lvs服务是否正常:

1watch ipvsadm –ln

2tail–f /var/log/message     监听日志,测试LVS负载均衡及高可用性是否有效。

3Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。


参考:

http://network.51cto.com/art/201006/206831.htm

一个很方便管理LVS+Keepalived负载均衡平台的B/S软件。

https://code.google.com/p/tianyalvs