LVS + Keepalived 实现负载均衡

#mkdir /usr/local/src/lvs
    #cd /usr/local/src/lvs
    #wget
    #wget
2. 安装LVS和Keepalived

#lsmod |grep ip_vs
         #uname -r
         2.6.18-53.el5PAE
需要安装 :
yum install -y kernel-devel
         #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
需要安装 :

yum -y install openssl-devel

#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 -p /etc/keepalived
         #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
         #cp /usr/local/sbin/keepalived /usr/sbin/
         #service keepalived start|stop     #做成系统启动服务方便管理.
3. 配置Realserver脚本.
   #vi /usr/local/sbin/realserver.sh
         #!/bin/bash
         # description: Config realserver lo and apply noarp
         #Written by :NetSeek
        
         SNS_VIP=192.168.1.163
         
         . /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
4. 配置在主负载均衡服务器上配置keepalived.conf
  vi /etc/keepalived/keepalived.conf
 
! Configuration File for keepalived
global_defs {
    notification_email {
          cnseek@gmail.com
    }
    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
         }
      }
}
       

5. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,
只需将标示的部分改一下即可.

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/pxxfz.html