zoukankan      html  css  js  c++  java
  • LVS入门

    说到大型网站的架构,就必然要谈到LVS。LVS即:Linux Virtual Server,是由国人章文嵩博士所创立的,已经被加入到了Linux 2.6的内核模块中了。官方网址:

    http://www.linuxvirtualserver.org/

    The Linux Virtual Server is a highly scalable and highly available server built on a cluster of real servers, with the load balancer running on the Linux operating system. The architecture of the server cluster is fully transparent to end users, and the users interact as if it were a single high-performance virtual server.

    The Linux Virtual Server as an advanced load balancing solution can be used to build highly scalable and highly available network services, such as scalable web, cache, mail, ftp, media and VoIP services.

    LVS是一个极好的负载均衡解决方案,LVS(Linux虚拟服务器),他实现了将一个由多台real server组成的集群虚拟成一台Linux服务器来对外提供服务(通过一个虚拟ip-VIP),同时在这多台real server之间实现了负载的均衡。配合keepalived,同时又提供了LVS的高可用,所以LVS + keepalived是一个极好的高可用的负载均衡解决方案

    中文官网的架构图如下(http://zh.linuxvirtualserver.org/node/95):

    整个集群系统通过一个VIP对外提供服务。在集群系统的最前面是两台实现高可用的Director server,当master director server挂掉时,自动fail over由backup director server变成新的master director server接管服务。

    Director server(DS):即分发服务器,功能是将每一个到达VIP的请求分发到后面的real server集群中的某一台服务器上;

    Real server(RS):即真正对外提供服务的服务器。

    Director server的高可用一般由keepalived来实现。同时keepalived也实现对real server的健康检查,如果发现挂掉的real server,会调用LVS的管理命令ipvsadm自动将改real server踢出集群;如果real server又重新恢复服务,就重新加入到集群中。

    LVS的三种模式:

    LVS的DS工作模式分为三种

    1> NAT:网络地址转换模式, 进站/出站的数据流量经过分发器;

    2> DR:直接路由模式,只有进站的数据流量经过分发器;

    3> TUN:隧道模式,只有进站的数据流量经过分发器;

    显然DR模式性能最好,一般情况我们使用DR模式,DR模式要求DS和RS都有一块网卡连在同一个物理网段上(也即位于同一个局域网中)。而TUN模式没有这个要求。

    LVS分发请求的十种算法:

    LVS的DS在向后面的RS分发请求实现负载均衡时,有10中不同的算法,供不同的场景来选中使用。

    1>轮叫(Round Robin):rr 在real server之间轮询的分配请求;

    2>加权轮叫(Weighted Round Robin):wrr 基于权重的轮叫

    3>最少链接(Least Connections):lc 将请求发送给链接数最少的real server

    4>加权最少链接(Weighted Least Connections):wlc 基于权重的最少链接

    5>基于局部性的最少链接(Locality-Based Least Connections): lblc 调度算法是针对目标IP地址的负载均衡,目前主要用于Cache集群系统。该算法根据请求的目标IP地址找出该目标IP地址最近使用的服务器,若该服务器是可用的且没有超载,将请求发送到该服务器;若服务器不存在,或者该服务器超载且有服务器处于一半的工作负载,则用“最少链接” 的原则选出一个可用的服务器,将请求发送到该服务器。

    6>带复制的基于局部性最少链接(Locality-Based Least Connections with Replication): lblcr 调度算法也是针对目标IP地址的负载均衡,目前主要用于Cache集群系统。它与LBLC算法的不同之处是它要维护从一个目标 IP地址到一组服务器的映射,而LBLC算法维护从一个目标IP地址到一台服务器的映射。该算法根据请求的目标IP地址找出该目标IP地址对应的服务器 组,按“最小连接”原则从服务器组中选出一台服务器,若服务器没有超载,将请求发送到该服务器;若服务器超载,则按“最小连接”原则从这个集群中选出一台 服务器,将该服务器加入到服务器组中,将请求发送到该服务器。同时,当该服务器组有一段时间没有被修改,将最忙的服务器从服务器组中删除,以降低复制的程 度。

    7>目标地址散列(Destination Hashing ):dh 调度算法根据请求的目标IP地址,作为散列键(Hash Key)从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,否则返回空。

    8>源地址散列(Source Hashing):sh 调度算法根据请求的源IP地址,作为散列键(Hash Key)从静态分配的散列表找出对应的服务器,若该服务器是可用的且未超载,将请求发送到该服务器,否则返回空。

    9>最短期望延迟(Shortest Expected Delay):sed 略

    10>无须队列等待(Never Queue):nq 略

    LVS分发策略的指定——ipvsadm命令:

    LVS的十种分发算法/策略由LVS的命令行工具来设定。ipvsadm在keepalived配置文件中要使用到,所以必须掌握它:

    直接yum 即可安装ipvsadm

    [root@localhost ~]# yum install ipvsadm

    查看ipvsadm帮助:

    [root@localhost ~]# ipvsadm --help
    ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)
    Usage:
      ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine]
      ipvsadm -D -t|u|f service-address
      ipvsadm -C
      ipvsadm -R
      ipvsadm -S [-n]
      ipvsadm -a|e -t|u|f service-address -r server-address [options]
      ipvsadm -d -t|u|f service-address -r server-address
      ipvsadm -L|l [options]
      ipvsadm -Z [-t|u|f service-address]
      ipvsadm --set tcp tcpfin udp
      ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
      ipvsadm --stop-daemon state
      ipvsadm -h
    
    Commands:
    Either long or short options are allowed.
      --add-service     -A        add virtual service with options
      --edit-service    -E        edit virtual service with options
      --delete-service  -D        delete virtual service
      --clear           -C        clear the whole table
      --restore         -R        restore rules from stdin
      --save            -S        save rules to stdout
      --add-server      -a        add real server with options
      --edit-server     -e        edit real server with options
      --delete-server   -d        delete real server
      --list            -L|-l     list the table
      --zero            -Z        zero counters in a service or all services
      --set tcp tcpfin udp        set connection timeout values
      --start-daemon              start connection sync daemon
      --stop-daemon               stop connection sync daemon
      --help            -h        display this help message
    
    Options:
      --tcp-service  -t service-address   service-address is host[:port]
      --udp-service  -u service-address   service-address is host[:port]
      --fwmark-service  -f fwmark         fwmark is an integer greater than zero
      --ipv6         -6                   fwmark entry uses IPv6
      --scheduler    -s scheduler         one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
                                          the default scheduler is wlc.
      --pe            engine              alternate persistence engine may be sip,
                                          not set by default.
      --persistent   -p [timeout]         persistent service
      --netmask      -M netmask           persistent granularity mask
      --real-server  -r server-address    server-address is host (and port)
      --gatewaying   -g                   gatewaying (direct routing) (default)
      --ipip         -i                   ipip encapsulation (tunneling)
      --masquerading -m                   masquerading (NAT)
      --weight       -w weight            capacity of real server
      --u-threshold  -x uthreshold        upper threshold of connections
      --l-threshold  -y lthreshold        lower threshold of connections
      --mcast-interface interface         multicast interface for connection sync
      --syncid sid                        syncid for connection sync (default=255)
      --connection   -c                   output of current IPVS connections
      --timeout                           output of timeout (tcp tcpfin udp)
      --daemon                            output of daemon information
      --stats                             output of statistics information
      --rate                              output of rate information
      --exact                             expand numbers (display exact values)
      --thresholds                        output of thresholds information
      --persistent-conn                   output of persistent connection info
      --nosort                            disable sorting output of service/server entries
      --sort                              does nothing, for backwards compatibility
      --ops          -o                   one-packet scheduling
      --numeric      -n                   numeric output of addresses and ports
    

     比如:ipvsadm -A -t 192.168.137.135:80 -s rr -p 600

    表示增加一个虚拟服务器,也即LVS,采用TCP协议,VIP为:192.168.137.135,端口为:80,采用 rr 负载均衡调度算法;

    ipvsadm -a -t 192.168.137.135:80 -r 192.168.137.150:80 -g

    ipvsadm -a -t 192.168.137.135:80 -r 192.168.137.151:80 -g

    表示在刚才新增的虚拟服务器192.168.137.135中添加两台real server,其ip地址分别为192.168.137.150和192.168.137.151,采用DR模式。

    ipvsadm -L 表示列出当前系统中的 real server 列表信息。

    验证内核已经安装了LVS模块:

    [root@localhost ~]# modprobe -l |grep ip_vs
    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
    kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
    

    LVS涉及到的三种IP地址: 

    1> VIP:整个LVS集群对外提供服务的ip地址;

    2> DIP: director server ip,分发服务器的IP,一般有master/backup两台DS服务器,所以有两个DIP;

    3> RIP: real server ip,真正处理请求的RS服务器的IP地址,每一台real server有一个ip地址;

  • 相关阅读:
    【转】简单地用nfs实现linux间文件共享
    CentOS 6.1上部署SVN时遇到的问题及解决方法
    ubuntu下部署SVN服务
    LINUX GBK>UTF8文件编码批量转换脚本[转]
    Thinkpad E420 3YC(双显卡)安装64位Ubuntu 11.10笔记
    拦截器详解
    快排的算法
    冒泡排序的算法
    struts2的输入校验
    ssh整合开发
  • 原文地址:https://www.cnblogs.com/digdeep/p/4243002.html
Copyright © 2011-2022 走看看