LVS + keepalived(DR) 实战

一、LVS体系结构

使用LVS架设的服务器集群系统有三个部分组成:最前端的负载均衡层,用Load Balancer表示,中间的服务器群组层,用Server Array表示,最底端的数据共享存储层,用Shared Storage表示,在用户看来,所有的内部应用都是透明的,用户只是在使用一个虚拟服务器提供的高性能服务。

LVS体系结构如图1所示:

                                                                              

二、LVS 3种负载均衡技术对比

 

负载均衡技术有很多实现方案,有基于DNS域名轮流解析的方法、有基于客户端调度访问的方法、有基于应用层系统负载的调度方法,还有基于IP地址的调度方法,在这些负载调度算法中,执行效率最高的是IP负载均衡技术。
LVS 的IP负载均衡技术是通过IPVS模块来实现的,IPVS是LVS集群系统的核心软件,它的主要作用是:安装在Director Server上,同时在Director Server上虚拟出一个IP地址,用户必须通过这个虚拟的IP地址访问服务。这个虚拟IP一般称为LVS的VIP,即Virtual IP。访问的请求首先经过VIP到达负载调度器,然后由负载调度器从Real Server列表中选取一个服务节点响应用户的请求。
当用户的请求到达负载调度器后,调度器如何将请求发送到提供服务的Real Server节点,而Real Server节点如何返回数据给用户,是IPVS实现的重点技术。


VS/NAT: 即(Virtual Server via Network Address Translation)
也 就是网络地址翻译技术实现虚拟服务器,当用户请求到达调度器时,调度器将请求报文的目标地址(即虚拟IP地址)改写成选定的Real Server地址,同时报文的目标端口也改成选定的Real Server的相应端口,最后将报文请求发送到选定的Real Server。在服务器端得到数据后,Real Server返回数据给用户时,需要再次经过负载调度器将报文的源地址和源端口改成虚拟IP地址和相应端口,然后把数据发送给用户,完成整个负载调度过 程。
可以看出,在NAT方式下,用户请求和响应报文都必须经过Director Server地址重写,当用户请求越来越多时,调度器的处理能力将称为瓶颈。


VS/TUN :即(Virtual Server via IP Tunneling)
也 就是IP隧道技术实现虚拟服务器。它的连接调度和管理与VS/NAT方式一样,只是它的报文转发方法不同,VS/TUN方式中,调度器采用IP隧道技术将 用户请求转发到某个Real Server,而这个Real Server将直接响应用户的请求,不再经过前端调度器,此外,对Real Server的地域位置没有要求,可以和Director Server位于同一个网段,也可以是独立的一个网络。因此,在TUN方式中,调度器将只处理用户的报文请求,集群系统的吞吐量大大提高。
VS/DR: 即(Virtual Server via Direct Routing)
也 就是用直接路由技术实现虚拟服务器。它的连接调度和管理与VS/NAT和VS/TUN中的一样,但它的报文转发方法又有不同,VS/DR通过改写请求报文 的MAC地址,将请求发送到Real Server,而Real Server将响应直接返回给客户,免去了VS/TUN中的IP隧道开销。这种方式是三种负载调度机制中性能最高最好的,但是必须要求Director Server与Real Server都有一块网卡连在同一物理网段上。

 

下面通过几个图示了解一下:

 

                                                                      

 

 

                                                                    

 

                                                                        

 

 

三、如何选择合适的负载模式根据自己的需要,下面讲解一下常用的DR负载的搭建

1、准备:

    二台DirectorServer 192.168.15.5(主) 192.168.15.6(备)   虚拟IP  192.168.15.100

    二台RealServer   192.168.15.8  192.168.15.9

2、DirectorServer LVS安装

现在LVS已经是 Linux标准内核的一部分,在Linux2.4内核以前,使用LVS时必须要重新编译内核以支持LVS功能模块,但是从Linux2.4内核以后,已经完全内置了LVS的各个功能模块,无需给内核打任何补丁,可以直接使用LVS提供的各种功能。

检查内核是否支持lvs的ipvs模块
[root@dev-test ~]# modprobe -l|grep ipvs
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ftp.ko
可以看到内核默认支持ipvs模块

3、DirectorServer keepAlived 安装

这里我使用的是自动安装的keepAlived,当然也可以找对应的版本下载后安装,这里我不做详细介绍,直接命令行:

root@test-api:/home/clonencheng# apt-get install keepalived
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-3.13.0-52 linux-headers-3.13.0-52-generic
  linux-headers-3.13.0-83 linux-headers-3.13.0-83-generic
  linux-headers-3.13.0-85 linux-headers-3.13.0-85-generic
  linux-headers-3.13.0-87 linux-headers-3.13.0-87-generic
  linux-headers-3.13.0-88 linux-headers-3.13.0-88-generic
  linux-headers-3.13.0-91 linux-headers-3.13.0-91-generic
  linux-headers-3.13.0-92 linux-headers-3.13.0-92-generic
  linux-headers-3.13.0-93 linux-headers-3.13.0-93-generic
  linux-image-3.13.0-52-generic linux-image-3.13.0-83-generic
  linux-image-3.13.0-85-generic linux-image-3.13.0-87-generic
  linux-image-3.13.0-88-generic linux-image-3.13.0-91-generic
  linux-image-3.13.0-92-generic linux-image-3.13.0-93-generic
  linux-image-extra-3.13.0-52-generic linux-image-extra-3.13.0-83-generic
  linux-image-extra-3.13.0-85-generic linux-image-extra-3.13.0-87-generic
  linux-image-extra-3.13.0-88-generic linux-image-extra-3.13.0-91-generic
  linux-image-extra-3.13.0-92-generic linux-image-extra-3.13.0-93-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  iproute ipvsadm libperl5.18 libsensors4 libsnmp-base libsnmp30
Suggested packages:
  heartbeat ldirectord lm-sensors snmp-mibs-downloader
The following NEW packages will be installed:
  iproute ipvsadm keepalived libperl5.18 libsensors4 libsnmp-base libsnmp30
0 upgraded, 7 newly installed, 0 to remove and 181 not upgraded.
Need to get 1,223 kB of archives.
After this operation, 4,729 kB of additional disk space will be used.
Do you want to continue? [Y/n]

回车后自动安装完成。

4、配置keepalvied

自动安装的keepalvied配置文件位于:/etc/keepalived/keepalived.conf,下面分别贴出主备的配置,注意:主备virtual_router_id必须一致,且priority的值主>备。

master keepalived配置如下:

global_defs {
   notification_email {
     #coffee.lee@yhouse.com
   }
   notification_email_from ubuntu@192.168.15.5
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100                     # 100 on master, 90 on backup
    quiescent=no                    # 当realserver被移除时,丢弃跟踪记录和连接持续模板
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.15.100 dev eth0
    }

    virtual_server 192.168.15.100 8038 {  #注意IP地址与端口号之间用空格隔开
        delay_loop 2        #设置健康检查时间,单位是秒
        lb_algo rr          #设置负载调度算法,默认为rr,即轮询算法,最优秀是wlc算法
        lb_kind DR          #设置LVS实现LB机制,有NAT、TUNN和DR三个模式可选
        nat_mask 255.255.0.0
       # persistence_timeout 50  #会话保持时间,单位为秒
        protocol TCP        #指定转发协议类型,有TCP和UDP两种
        real_server 192.168.15.8 8038 {
            weight 1          #配置节点权值,数字越大权值越高
            TCP_CHECK {
                connect_timeout 5     #表示3秒无响应,则超时
                nb_get_retry 2        #表示重试次数
                delay_before_retry 2  #表示重试间隔
            }
        }
        real_server 192.168.15.9 8038 {  #配置服务器节点,即Real Server2的public IP
            weight 1            #配置节点权值,数字越大权值越高
            TCP_CHECK {
                connect_timeout 5       #表示3秒无响应,则超时
                nb_get_retry 2          #表示重试次数
                delay_before_retry 2    #表示重试间隔
            }
        }    
    }    

}


backup keepalived配置如下:


global_defs {
   notification_email {
     #coffee.lee@yhouse.com
   }
   notification_email_from ubuntu@192.168.15.5
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90                     # 100 on master, 90 on backup
    quiescent=no                    # 当realserver被移除时,丢弃跟踪记录和连接持续模板
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.15.100 dev eth0 #dev.api.yhouse.com
    }

    virtual_server 192.168.15.100 8038 {  #注意IP地址与端口号之间用空格隔开
        delay_loop 2        #设置健康检查时间,单位是秒
        lb_algo rr          #设置负载调度算法,默认为rr,即轮询算法,最优秀是wlc算法
        lb_kind DR          #设置LVS实现LB机制,有NAT、TUNN和DR三个模式可选
        nat_mask 255.255.0.0
       # persistence_timeout 50  #会话保持时间,单位为秒
        protocol TCP        #指定转发协议类型,有TCP和UDP两种
        real_server 192.168.15.8 8038 {
            weight 1          #配置节点权值,数字越大权值越高
            TCP_CHECK {
                connect_timeout 5     #表示3秒无响应,则超时
                nb_get_retry 2        #表示重试次数
                delay_before_retry 2  #表示重试间隔
            }
        }
        real_server 192.168.15.9 8038 {  #配置服务器节点,即Real Server2的public IP
            weight 1            #配置节点权值,数字越大权值越高
            TCP_CHECK {
                connect_timeout 5       #表示3秒无响应,则超时
                nb_get_retry 2          #表示重试次数
                delay_before_retry 2    #表示重试间隔
            }
        }
    }

}

5、配置RealServer

二台RealServer服务器上,我们分别启动二个测试服务,这里我用的是tomcat,下面只包含一个index.jsp文件,为了区分请求转发到哪台服务器,内容做如下区分:

             

在二台RealServer下分别执行以下脚本realserver.sh (为了使realsever能够接受来自虚拟服务器的请求):

#!/bin/bash
VIP=192.168.15.100
case "$1" in
start)
       ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $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
       echo "RealServer Start OK"
       ;;
stop)
       ifconfig lo:0 down
       route del $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

6、至此,LVS+keepalived(DR)已经基本搭建完,下面我们来测试下:

我们先输入地址刷新一下:

再刷新一下,则变成了:

说明我们搭建没有问题,请求能够自动在二台realserver之间切换,lvs有8种负载策略,这里我们用的是rr即轮询,所以看到了以上的结果,刷新一下换一下,其他策略这里不做详细介绍,有兴趣可以测试一下,比如加权轮询,最少连接等等。

上面的dev.corp.yhouse.com/dev,我使用的是nginx转发的,nginx配置如下:

server {
        charset utf-8;
        client_max_body_size 4M;
        listen 80;

        index index.html index.htm index.jsp;
        server_name dev.corp.yhouse.com;

        location / {
                deny  all;
        }

        location /dev/ {
                proxy_pass  http://192.168.15.100:8038/test/;
                proxy_set_header   Host             $host;
        }
}

从配置可以看出,从nginx访问直接转发至虚拟IP(LVS层),而不关心最终服务地址。

 

posted on 2016-08-31 18:03  虾米&老黄牛  阅读(329)  评论(0编辑  收藏  举报

导航