说到大型网站的架构,就必然要谈到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地址;

LVS入门的更多相关文章

  1. LVS入门篇(五)之LVS+Keepalived实战

    一.实验架构和环境说明 (1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台负载均衡服务器(一台为主机,另一台为备机), ...

  2. LVS入门篇(四)之LVS实战

    一.LVS的NAT模式实战 1.环境说明: HOST OS role remask 192.168.56.12 Centos 7.4 LVS调度器(1.2.7) VIP:192.168.0.104 1 ...

  3. LVS入门篇(三)之LVS的工作模式和调度算法

    1.NAT模型 (1)原理图: ①.客户端(200.10.10.1)将请求发往前端的负载均衡器(114.100.80.10),请求报文源地址是CIP(客户端IP),后面统称为CIP),目标地址为VIP ...

  4. LVS入门篇(二)之LVS基础

    1. LVS介绍 LVS是Linux虚拟服务器(LinuxVirtualServers),使用负载均衡技术将多台服务器组成一个虚拟服务器.它为适应快速增长的网络访问需求提供了一个负载能力易于扩展,而价 ...

  5. LVS入门篇(一)之ARP协议

    1.概念 地址解析协议,即ARP(AddressResolutionProtocol),是根据IP地址获取物理MAC地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上 ...

  6. 使用LVS实现负载均衡原理及安装配置详解

    负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...

  7. Iptables与LVS——从入门到放弃

    防火墙什么是防火墙?防火墙其实就是一个隔离的工具,工作于主机或者网络的边缘,对于进出本主机或者网络的报文根据事先定义好的网络规则做匹配监测.防火墙可以简单地划分为两大类:主机防火墙 网络防火墙     ...

  8. Linux运维入门到高级全套常用要点

    Linux运维入门到高级全套常用要点 目 录 1. Linux 入门篇................................................................. ...

  9. Linux运维工程师入门的10大实用工具

    说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具. 我就大概列出这几方面,这样入门就基本没问题了. 工具如下: 1.Linux系统基础 这个不用说了,是基础中的基础,连这 ...

随机推荐

  1. redis在centOS的安装

    1.安装tcl支持 yum install tcl 2.安装redis我们以最新的2.8.9为例 $ wget http://download.redis.io/releases/redis-2.8. ...

  2. UWP开发入门(二十)——键盘弹起时变更界面布局

    UWP APP在键盘弹起或隐藏时,并不会自动处理界面布局.有时会出现键盘遮挡了下一个需要填写的文本框,或是下一步按钮的情况.本篇我们以登录界面做例子,用一种巧妙简单的方式在键盘弹起和隐藏时更改界面的布 ...

  3. R提高篇(二): 图形初阶

    目录: 图形示例 图形参数 符号.线条 颜色 文本属性 尺寸与边界 自定义标题 自定义坐标轴 图例 文本标注 图形组合 图形示例 如下代码描述病人对两种药物五个剂量水平上的响应情况 > myda ...

  4. sprint 1 2 3 贡献分

    本学期我们做的项目是商品和员工管理系统(茗仕茶业管理平台) 团队Github地址:https://github.com/LinWenFeng团队博客地址:http://www.cnblogs.com/ ...

  5. c++转C#

    //c++:HANDLE(void   *)          ----    c#:System.IntPtr        //c++:Byte(unsigned   char)     ---- ...

  6. Linq专题之集合初始化器

    集合初始化器用来初始化一个集合,和对象初始化器有点类似,都是用一对{}来初始化. using System; using System.Collections.Generic; using Syste ...

  7. c# tcp备忘及networkstream.length此流不支持查找解决

    服务端 bool isRunning = true;  MouseKeyBoard mk = new MouseKeyBoard(); void InitTcpServer(int port) { T ...

  8. Aspose.Words.Tables.Row类操作word表格行

    http://www.aspose.com/docs/display/wordsnet/Aspose.Words.Tables.Row+Class Retrieves the index of a r ...

  9. 吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展

    bug所在位置:DotNet.Business\Utilities\BaseManager.GetDataTableByPage.cs的函数 public virtual DataTable GetD ...

  10. IIS配置ASP.NET和服务器错误页

    以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...