CENTOS5下采用LVS +KEEPALIVED实现负载均衡

 一、环境
OS: CENTOS5.3
 
VIP:192.168.0.181  (外网访问IP地址)
 
LVS_SRV: 192.168.0.180 (LVS主服务器)
 
R1:192.168.0.185
 
R2:192.168.0.186
 
二、LVS安装
 
1.软件安装(安装至LVS_SRV)
 
yum install ipvsadm modcluster system-confi-cluster
 
也可以选择手工下载源码编译安装,这里不再叙述。
 
2.配置(lvs.sh)
 
1) lvs_srv配置
 
    #开启IP转发,仅当DR模式下
 
echo "1" >/proc/sys/net/ipv4/ip_forward
 
2)       Rn配置(rs.sh)
 
#!/bin/sh
 
# ghb in 20120212
 
# description: Config realserver tunl port and apply arp patch
 
VIP=192.168.0.181
 
. /etc/rc.d/init.d/functions
 
case $1 in
 
    start)
 
    echo "Tunl port starting"
 
    ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
 
    /sbin/route add -host $VIP dev lo:0
 
    echo "1" > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
 
    echo "2" > /proc/sys/net/ipv4/conf/tunl0/arp_announce
 
    echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
 
    echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
 
    sysctl -p
 
    ;;
 
    stop)
 
    echo "Tunl port closing"
 
    ifconfig lo:0 down
 
    echo "1" > /proc/sys/net/ipv4/ip_forward
 
    echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
 
    ;;
 
    *)
 
    echo "Usage: $0 {start|stop}"
 
    exit 1
 
esac
 
3) Rn配置
 
Rn下运行
 
Rs.sh start
 
三、KEEPALIVED
 
这里是用keepalived来进行lvs的建立和服务器和vip的健康检查,其实keepalived就像一个shell,他自动的给你做lvs+vrrp。
 
keepalived本身来做真实服务器的健康检查,vrrp来做vip的冗余。
 
 
1.软件安装
 
# tar -zxvf keepalived-1.1.15.tar.gz
 
# cd keepalived-1.1.15
 
# ./configure –-enable-lvs-syncd –-enable-lvs –-with-kernel-dir= /usr/src/kernels/2.6.18-128.e15-i686
 
#(注意:–-with-kernel-dir参数必需加载,否则无法支持KEEPALIVED调用IPVS完成LVS应用,内核版本根据实际机器情况填写,当然,不要告诉我你安装系统的时候没有选择安装系统开发包工具)
 
# make && make install
 
cp /usr/local/etc/rc.d/init.d/keepalived  /etc/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/
 
2.  网卡设置lvs-init.sh
 
#!/bin/sh
 
VIP=192.168.0.181
 
# set the Virtual IP Address
 
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
 
/sbin/route add -host $VIP dev eth0:0
 
#开启IP转发,仅当DR模式下
 
echo "1" >/proc/sys/net/ipv4/ip_forward
 
或者
 
Cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-script/ifcfg-eth0:0
 
修改内容:
 
DEVICE=eth0:0
 
BOOTPROTO=static
 
IPADDR=192.168.0.181
 
NETMASK=255.255.255.224
 
ONBOOT=yes
 
  3.配置文件keepalived.conf
 
! Configuration File for keepalived
 
global_defs {
    notification_email {
          #[email protected]
    }
    #notification_email_from [email protected]
    #smtp_server 127.0.0.1
   # smtp_connect_timeout 30
    router_id LVS_DEVEL
}
 
# 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 {
         192.168.0.181   
         #(如果有多个VIP,继续换行填写.)
     }
}
 
virtual_server 192.168.0.181 80 {
     delay_loop 6                  #(每隔10秒查询realserver状态)
     lb_algo wrr                  #(lvs 算法,rr,wrr-轮询,lc,wlc-最少连接)
     lb_kind DR                  #(Direct Route,NAT,TUN)
     persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
     protocol TCP                #(用TCP协议检查realserver状态)
 
     real_server 192.168.0.185 80 {
         weight 3               #(权重)
         TCP_CHECK {
              connect_timeout 10       #(10秒无响应超时)
              nb_get_retry 3
              delay_before_retry 3
              connect_port 80
         }
     }
     real_server 192.168.0.186 80 {
         weight 3
         TCP_CHECK {
             connect_timeout 10
              nb_get_retry 3
              delay_before_retry 3
              connect_port 80
         }
      }
}
 
 
四、运行
 
1.  Rn服务器运行Rs.sh start
 
2.  LVS服务器安装后重启service keepalived start
 
运行watch ipvsadm –Ln,查看服务状态
 
(本方案真实安装服务器并在生产中运行,没有任何问题,当然真实项目中还有相关的防火墙设置的其他操作,希望可以帮助有需要的各位)

 

本文出自 “linux进阶屋” 博客,谢绝转载!

你可能感兴趣的:(LVS,centos,KeepAlive,休闲,centos5)