負載均衡--LVS+Keepalived

利用LVS+Keepalived 實現高性能高可用負載均衡php


 

   背景:html

        隨着你的網站業務量的增加你網站的服務器壓力愈來愈大?須要負載均衡方案!商業的硬件如F5又太貴,大家又是創業型互聯公司如何有效節約成本,節省沒必要要的浪費?同時實現商業硬件同樣的高性能高可用的功能?有什麼好的負載均衡可伸張可擴展的方案嗎?答案是確定的!有!咱們利用LVS+Keepalived基於完整開源軟件的架構能夠爲你提供一個負載均衡及高可用的服務器。linux

 

1、LVS+Keepalived 介紹web

     1. LVS算法

LVSLinux Virtual Server的簡寫,意即Linux虛擬服務器,是一個虛擬的服務器集羣系統。本項目在1998年5月由章文嵩博士成立,是中國國內最先出現的自由軟件項目之一。目前有三種IP負載均衡技術(VS/NAT、VS/TUN和VS/DR);bash

八種調度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。服務器

 

     2. Keepalvied架構

       Keepalived在這裏主要用做RealServer的健康狀態檢查以及LoadBalance主機和BackUP主機之間failover的實現app

二. 網站負載均衡拓樸圖負載均衡

           

.

   IP信息列表:

名稱

IP

 LVS-DR-Master

61.164.122.6

 LVS-DR-BACKUP

61.164.122.7

 LVS-DR-VIP

61.164.122.8

 WEB1-Realserver

61.164.122.9

 WEB2-Realserver

61.164.122.10

 GateWay

61.164.122.1

 

 

三. 安裝LVS和Keepalvied軟件包

   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

   2. 安裝LVS和Keepalived

     #lsmod |grep ip_vs

     #uname -r

      2.6.18-53.el5PAE

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

 

     #tar zxvf ipvsadm-1.24.tar.gz

     #cd ipvsadm-1.24

  #make && make install

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

 

  #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     #作成系統啓動服務方便管理.

 

 

四. 配置LVS實現負載均衡

  1. LVS-DR,配置LVS腳本實現負載均衡

      #vi /usr/local/sbin/lvs-dr.sh

      #!/bin/bash

  # description: start LVS of DirectorServer

  #Written by :NetSeek http://www.linuxtone.org

 

  GW=61.164.122.1

  # website director vip.

  SNS_VIP=61.164.122.8

  SNS_RIP1=61.164.122.9

  SNS_RIP2=61.164.122.10

 

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

 

  logger $0 called with $1

 

  case "$1" in

 

  start)

           # set squid vip

           /sbin/ipvsadm --set 30 5 60

           /sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255

  broadcast $SNS_VIP up

           /sbin/route add -host $SNS_VIP dev eth0:0

           /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3

           /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1

           /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1

           touch /var/lock/subsys/ipvsadm >/dev/null 2>&1

 

       ;;

  stop)

           /sbin/ipvsadm -C

           /sbin/ipvsadm -Z

           ifconfig eth0:0 down

           ifconfig eth0:1 down

           route del $SNS_VIP

           route del $SS_VIP

           rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1

           echo "ipvsadm stoped"

           ;;

 

  status)

 

           if [ ! -e /var/lock/subsys/ipvsadm ];then

                echo "ipvsadm stoped"

                exit 1

           else

                echo "ipvsadm OK"

           fi

           ;;

 

  *)

          echo "Usage: $0 {start|stop|status}"

           exit 1

  esac

 

  exit 0

     

  2. 配置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=61.164.122.8

 

  . /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

    或者採用secondary ip address方式配置

    # vi /etc/sysctl.conf

net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

  #sysctl –p

  #ip addr add 61.164.122.8/32 dev lo

  #ip add list 查看是否綁定

  3. 啓動lvs-dr腳本和realserver啓本,在DR上能夠查看LVS當前狀態:

#watch ipvsadm –ln

 

五.利用Keepalvied實現負載均衡和和高可用性

  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 {

        61.164.122.8  

        #(若是有多個VIP,繼續換行填寫.)

    }

}

 

virtual_server 61.164.122.8 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 61.164.122.9 80 {

        weight 3               #(權重)

        TCP_CHECK {

        connect_timeout 10       #(10秒無響應超時)

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

    }

    real_server 61.164.122.10 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

        }

     }

}

 

2. BACKUP服務器同上配置,先安裝lvs再按裝keepalived,仍後配置/etc/keepalived/keepalived.conf,只需將紅色標示的部分改一下便可.

 

3. vi /etc/rc.local

   #/usr/local/sbin/lvs-dr.sh  將lvs-dr.sh這個腳本註釋掉。步

   #/usr/local/sbin/lvs-dr.sh stop 中止lvs-dr腳本

   #/etc/init.d/keepalived start  啓動keepalived 服務,keepalived就能利用keepalived.conf 配

   置文件,實現負載均衡和高可用.

 

4. 查看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  61.164.122.8:80 wrr persistent 60

  -> 61.164.122.10:80            Route   3      0          0

  -> 61.164.122.9:80             Route   3      0          0

 

 #tail –f /var/log/message  監聽日誌,查看狀態。

 

5.停Master服務器的keepalived服務,查看BAKCUP服務器是否能正常接管.

 

 

四.相關參考

  1.LVS 基礎知識彙總

更多的請到http://www.linuxtone.org 負載均衡版查看

 

    1. Keepalived 相關參考資料。

   http://www.keepalived.org/documentation.html

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息