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

xixingzhe 2012-04-28

一、环境介绍
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 –add keepalived
# chkconfig –level 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相同。

相关推荐