Centos5.5lvs+keepalived集群
环境:4centos5.5(32bit)
目标:实现负载均衡集群,保证业务的高可靠性
撰写人:hx10
日期:2010/6/2  hangzhou
blog:http://hi.baidu.com/hx10
 
第一步、环境准备
lvs-master:10.80.11.245
lvs-backup:10.80.11.246
vip:10.80.11.240
web1:10.80.11.243
web2:10.80.11.244
netmask:255.255.255.0
gateway:10.80.11.1
 
接下来关掉一些不必要的系统服务
chkconfig bluetooth off
chkconfig firstboot off
chkconfig cups off
chkconfig ip6tables off
chkconfig iptables off
chkconfig isdn off
chkconfig kudzu off
chkconfig sendmail off
chkconfig smartd off
chkconfig autofs off
service bluetooth stop
service firstboot stop
service cups stop
service ip6tables stop
service iptables stop
service isdn stop
service kudzu stop
service sendmail stop
service smartd stop
service autofs stop
服务器时间校对
# crontab -e
加入一行:
0 3 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1
第二步、安装软件
#cd /usr/local/src
#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
#ln -s /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux
#tar -zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#cd ..
#tar -zxvf keepalived-1.1.15.tar.gz
# cd keepalived-1.1.15
#./configure
configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.
解决办法:
# yum -y install openssl-devel
# ./configure && make && make install
编译的时候出现这个提示,说明keepalived和内核结合了,如果不是这样的,需要加上这个参数./configure --with-kernel-dir=/kernel/path
Keepalived configuration
------------------------
Keepalived version       : 1.1.15
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No
 
下面设置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
第三步、配置keepalived
#vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
    notification_email {
          yhl5555@126.com
    }
    notification_email_from yhl5555@126.com
    smtp_server 127.0.0.1
   # smtp_connect_timeout 30
    router_id LVS_DEVEL
}
 
# VIP1
vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 51
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass 1111
     }
     virtual_ipaddress {
         10.80.11.240  
     }
}
 
virtual_server 10.80.11.240 80 {
     delay_loop 6
     lb_algo wrr
     lb_kind DR
     #persistence_timeout 5
     protocol TCP
 
     real_server 10.80.11.243 80 {
         weight 3             
         TCP_CHECK {
         connect_timeout 10      
         nb_get_retry 3
         delay_before_retry 3
         connect_port 80
         }
     }
     real_server 10.80.11.244 80 {
         weight 3
         TCP_CHECK {
         connect_timeout 10
         nb_get_retry 3
         delay_before_retry 3
         connect_port 80
         }
      }
}
 
lvs-master的优先级设置为100,状态为MASTER,lvs-backup上优先级为99,状态为BACKUP,这是2LVS的唯一区别
persistence_timeout 5这是在5S内连接到同一台web服务器,注释掉就轮训
至此,lvs-master配置已经完成,lvs-backup对照lvs-backup安装相应软件,注意keepalive.conf有细微区别
第四步、web服务器的配置
2web服务器上添加一下脚本
#vim /etc/init.d/realserver.sh
#!/bin/bash
SNS_VIP=10.80.11.240
. /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
红色部分为VIP地址
#chmod 755 /etc/init.d/realserver.sh
#sh /etc/init.d/realserver.sh start
同时加入自启动
#vim /etc/rc.local
/etc/init.d/realserver.sh start
两台web服务器都需要这样设置
第五步、测试
分别启动2lvs的服务
#service keepalived start
然后在客户机上输入http://10.80.11.240进行访问
停掉lvs-master,然后测试
停掉web1,然后测试
如果需要把lvs加入开机自启动,执行以下命令
#chkconfig --add keepalived
#chkconfig keepalived on
至此,LVS+KEEPALIVED高可靠性配置已经完成。