LVS+Keepalived实现负载均衡及双机热备

一、环境介绍
VIP 192.168.183.100
lvs-master 192.168.183.128
lvs-backup 192.168.183.129
realserver1 192.168.183.130
realserver2 192.168.183.131

我是用虚机做的测试,四台虚机全部最小化安装CentOS 5.5 64bit,网络模式为NAT。

本次测试LVS为DR模式。

二、部署LVS+Keepalived

1.安装ipvsadm
#yum -y install ipvsadm 
2.安装keepalived
#yum -y install kernel-devel openssl-devel
现在最新稳定版本keepalived,本次测试使用版本为keepalived-1.2.2
先建立内核目录软连接,否则./configure会报错。
ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
解压keepalived-1.2.2.tar.gz
进入源码目录,./configure 
完成之后显示如下信息则configure成功。
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
然后make && make install
我在这里遇到了一个错误,有人说是版本的问题,是如下处理的:
vi keepalived/libipvs-2.6/ip_vs.h
找到下面一行:
#include <linux/types.h>        /* For __beXX types in userland */
把他放到下面一行的下面:
#include <sys/types.h>
这样便可以编译过去了。

三、配置lvs+Keepalived

1.拷贝配置文件及命令文件
# 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/
# chkconfig �Cadd keepalived
# chkconfig �Clevel 2345 keepalived on

2.撰写keepalived.conf
# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
     notification_email {
                [email protected]
     }
     notification_email_from localhost
     smtp_server 192.168.1.99
     smtp_connect_timeout 30
     router_id LVS_MASTER
}

vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 51
        priority 101
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.183.100
        }
}
virtual_server 192.168.183.100 80 {
        delay_loop 6
        lb_algo wrr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 50
        protocol TCP

        real_server 192.168.183.130 80 {
                weight 3
                TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
                }
        }
        real_server 192.168.183.131 80 {
                weight 1
                TCP_CHECK {
                connect_timeout 10
                nb_get_retry 3
                delay_before_retry 3
                connect_port 80
                }
        }
}
这个是MASTER主机的配置,在BACKUP上基本相同,仅红色标记部分做相应更改即可,其他完全与MASTER相同。

四、RealServer配置
RealServer请自行安装相关WEB服务。
RealServer上面只需要执行以下脚本即可。
#! /bin/bash
SNS_VIP=192.168.183.100
/etc/rc.d/init.d/functions

case "$1" in
start)
         /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
         /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
         syscty -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
启动后检查是否监听虚拟IP,如果有监听虚拟IP,说明启动成功。否则,请检查。
至此所有的配置完毕。
五、测试
在主备机器上都启动keepalived,在RealServer上启动Web服务和监听虚拟IP。
测试在停止其中之一的WEB服务时,停止LVS-MASTER keepalived服务时。访问虚拟IP,看是否可以返回结果,并且观察RealServer WEB服务的访问日志。
简单的测试就到此结束。以后会继续关注这方面的问题。

你可能感兴趣的:(keepalived,LVS,职场,休闲,realserver)