Lvs+keepalived负载均衡安装配置
环境:
Centos 5.5
lvs-master:192.168.18.30
lvs-backup:192.168.18.40
vip:192.168.18.31
web1:192.168.18.60
web2:192.168.18.70
netmask:255.255.255.0
gateway:192.168.18.1 
为了好区分先把主机名临时修改一下:
# hostname master
# hostname backup
# hostname web_1
# hostname web_2
安装过程
1、MASTER安装配置:
(1)下载所需要的包
[root@master ~]# cd /usr/local/src
[root@master ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz 
(2)安装依赖包
[root@master ~]# yum install kernel-devel 
[root@master ~]# ln -sv /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux
[root@master ~]# yum -y install openssl-devel 
(3)解压安装ipvsadm
[root@master ~]# tar -zxvf ipvsadm-1.24.tar.gz
[root@master ~]# cd ipvsadm-1.24
[root@master ~]# make && make install
[root@master ~]# cd ..
(4)解压安装keepalived
[root@master ~]# tar -zxvf keepalived-1.1.17.tar.gz
[root@master ~]# cd keepalived-1.1.17
[root@master ~]# ./configure 
[root@master ~]# make && make install 
(5)配置keepalived 
[root@master ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@master ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived
[root@master ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@master ~]# vi /etc/keepalived/keepalived.conf 
 
! Configuration File for keepalived
global_defs {
   notification_email {
      dongzhi1109@163.com
   }
   notification_email_from dongzhi1109@163.com
   smtp_server smtp.163.com
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
# VIP1
vrrp_instance VI_1 {
    state MASTER             #备份服务器上将MASTER改为BACKUP   
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 51
    priority 100    # 备份服务上将100改为90
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.18.31
        #(如果有多个VIP,继续换行填写.)
    }
}
 
virtual_server 192.168.18.31 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wlc                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)
 
    real_server 192.168.18.70 80 {
        weight 100               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.18.90 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}
(6)启动并加入开机启动
[root@master ~]#  service keepalived start|stop 
[root@master ~]# chkconfig –level 2345 keepalived on 
2、BACKUP安装配置: 
(1)下载所需要的包
[root@backup ~]# cd /usr/local/src
[root@backup ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
[root@backup ~]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz 
(2)安装依赖包
[root@backup ~]# yum -y install kernel-devel 
[root@backup ~]# uname -r
[root@backup ~]# ln -sv /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux
[root@backup ~]# yum -y install openssl-devel 
(3)解压安装ipvsadm
[root@backup ~]# tar -zxvf ipvsadm-1.24.tar.gz
[root@backup ~]# cd ipvsadm-1.24
[root@backup ~]# make && make install
[root@backup ~]# cd ..
(4)解压安装keepalived
[root@backup ~]# tar -zxvf keepalived-1.1.17.tar.gz
[root@backup ~]# cd keepalived-1.1.17
[root@backup ~]# ./configure 
[root@backup ~]# make && make install 
(5)配置keepalived 
[root@backup ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@backup ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@backup ~]# mkdir /etc/keepalived
[root@backup ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@backup ~]# vi /etc/keepalived/keepalived.conf 
 
! Configuration File for keepalived
global_defs {
   notification_email {
      dongzhi1109@163.com
   }
   notification_email_from dongzhi1109@163.com
   smtp_server smtp.163.com
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
# VIP1
vrrp_instance VI_1 {
    state  BACKUP   
    interface eth0
    lvs_sync_daemon_inteface eth0
    virtual_router_id 51
    priority  90
    advert_int 5
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.18.31
        #(如果有多个VIP,继续换行填写.)
    }
}
 
virtual_server 192.168.18.31 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wlc                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)
 
    real_server 192.168.18.60 80 {
        weight 100               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.18.70 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}
(6)启动并加入开机启动
[root@backup ~]# service keepalived start|stop 
[root@backup ~]# chkconfig --level 2345 keepalived on 
配置realserver 
1、配置web_1服务
(1)添加lvs_real.sh文件
[root@web_1 ~]# vi /root/lvs_real.sh 
#!/bin/bash
SNS_VIP=192.168.18.31
/etc/rc.d/init.d/functions
case "$1" in
start)
       /sbin/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)
       /sbin/ifconfig lo:0 down
       /sbin/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
(2)给文件权限并启动
[root@web_1 ~]# chmod +x /root/lvs_real.sh
[root@web_1 ~]# /root/lvs_real.sh start 
(3)加入开机启动
[root@web_1 ~]# echo “/root/lvs_real.sh start” >> /etc/rc.local 
2、配置 web_2
(1)添加lvs_real.sh文件
[root@web_2 ~]# vi /root/lvs_real.sh 
#!/bin/bash
SNS_VIP=192.168.18.31
/etc/rc.d/init.d/functions
case "$1" in
start)
       /sbin/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)
       /sbin/ifconfig lo:0 down
       /sbin/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
(2)给文件权限并启动
[root@web_2 ~]# chmod +x /root/lvs_real.sh
[root@web_2 ~]# /root/lvs_real.sh start 
(3)加入开机启动
[root@web_2 ~]# echo “/root/lvs_real.sh start” >> /etc/rc.local 
如果出现这种问题:

解决方法:
关闭关闭avahi-daemon守护进程
service avahi-daemon  stop
 
测试LVS+keepalived 

日志信息:

1、高可用性测试:
将LVS-MASTER上的keepalived服务停掉,然后观察LVS-BACKUP上的日志;
从日志中可看出,LVS-MASTER服务停掉以后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源;

将LVS-MASTER 上的keepalived服务开启后,LVS-BACKUP的日志状态;
从日志可看出,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色;

2、故障切换测试:
故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行 ;
将192.168.18.60这台主机节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下: 

以上可以看出,Keepalived监控模块检测到192.168.18.60这台主机出现故障后,将些节点2从集群系统中剔除掉了;
重新启动realserver-web_1节点的服务,日志信息如下:

以上可以看出,Keepalived监控模块检测到192.168.18.60这台主机恢复正常后,又将此节点加入集群系统中;
测试正常情况下网页显示
为了测试在这里只是简单的写了两个html页面;
以下为访问一次和强制刷新一次以后所显示的两个页面;

以上可以看出测试是成功的,到这里我们的lvs+keepalived已经完成;