首页 > 技术文章 > [LVS] 用keepalived实现LVS NAT模式高可用性

bonjov1 2015-05-22 18:47 原文



默认前提是LVS已经可以正常工作了。

因为是NAT模式,RS的路由要指向LVS的接口地址,所以需要一个统一的后台浮动地址,使得RS都指向这个浮动IP.
否则在切换时,会导致RS回包到DOWN掉的LVS上,使得请求超时失败。

1. 下载相关软件包

#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz


2. 安装Keepalived

        #tar zxvf keepalived-1.1.15.tar.gz
        #cd keepalived-1.1.15
        #./configure --with-kernel-dir=/kernel/path  && make && make install
        #find / -name keepalived  # 查看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的话最好加上--with-kernel-dir=/kernel/path参数,如果keepalived找不到内核源码,在编译时会不编译lvs支持。
仔细看./configure 最后的提示:
找不到内核源码提示如下:

Keepalived configuration
------------------------
Keepalived version       : 1.1.15
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : No
IPVS sync daemon support : No
Use VRRP Framework       : Yes
Use LinkWatch            : No
Use Debug flags          : No
找到内核源码提示如下:
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


        
3. 再两台LVS上配置keepalived.conf:

[root@A ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER             #备份服务器上将MASTER改为BACKUP   
    interface eth11
    virtual_router_id 51
    priority 100              # 备份服务上将100改为99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        11.11.11.15
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth12
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        22.22.22.2
    }
}

virtual_server 11.11.11.15 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo rr                     #(lvs 算法)
    lb_kind NAT
    nat_mask 255.255.255.0
    persistence_timeout 50         #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                 #(用TCP协议检查realserver状态)

    real_server 22.22.22.23 80 {
        weight 1                 #(权重)
        TCP_CHECK {    
        connect_timeout 3         #(3秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        }
    }

    real_server 22.22.22.24 80 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
    }

}


4. 查看lvs服务是否正常

a) 首先在两台LVS上开启keepalived,执行 "ip addr", 观察Master上的浮动IP是否存在。
b) 关闭Master的keepalived 服务,在BACKUP上执行“ip addr”,观察浮动IP是否切换过来了。
c) 从Client端,访问LVS vIP,切换两台LVS的主备状态,观察请求是否可以切换。




5. 拓扑:


          外网              外网浮动VIP        内网                内网浮动IP
Client    11.11.11.11            
                
LVS1    11.11.11.12        11.11.11.15       22.22.22.22        22.22.22.2
LVS1    11.11.11.13                           22.22.22.25    
                
Server1                                         22.22.22.23    
Server2                                         22.22.22.24    




推荐阅读