说到大型网站的架构,就必然要谈到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. 五分钟,运用cocoaui库,搭建主流iOS app中我的界面

    本项目基于天天团购项目,在上一篇中有说到! 首先介绍一些cocoaui,是国内的一名程序员做的开源的开源系统,目的是为了简化ios布局!官网地址:www.cocoaui.com,github地址:ht ...

  2. 理解SQL Server的查询内存授予(译)

    此文描述查询内存授予(query memory grant)在SQL Server上是如何工作的,适用于SQL 2005 到2008. 查询内存授予(下文缩写为QMG)是用于存储当数据进行排序和连接时 ...

  3. Winform开发框架的业务对象统一调用方式

    在这个纷繁的社会里面,统一性的特点能够带来很多高效的产出.牢固的记忆,这种特征无论对于企业.个人的开发工作,知识的传承都有着非常重要的作用,Winfrom框架本身就是基于这个理念而生,从统一的数据库设 ...

  4. windows的host文件的位置和作用

    在Window系统中有个Hosts文件(没有后缀名),在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\ ...

  5. sql语句创建新登录名和设置权限

    use DBName go --新增用户 exec sp_addlogin '用户名','密码','默认数据库名' --添加登录 exec sp_grantdbaccess N'test' --使其成 ...

  6. css中important的用处

    今天看代码时遇到一段不理解的地方. #note_content { line-height: 22px; border: #DEDEDE 1px solid; background: #FAFAFA; ...

  7. MyKTV项目总结

    今天和大伙分享一下我的KTV系统,我想大家都有自己独特的魅力,都有自己的风采,都有自己骄傲的一部分. 在这里我就抛砖引玉,聊聊我的KTV项目,希望大家能给出自己的建议.. 首先,我们先了解一下:当我们 ...

  8. postgresql 9.6 rc1发布

    postgresql 9.6 rc1发布了,意味着postgresql 9.6正式版将会越来越近了. 对于dss来说,postgresql远优于mysql,尤其是9.6新引入的并行执行,将大大提高性能 ...

  9. 使用WebMatrix发布网站

    使用WebMatrix发布网站 WebMatrix 简介: Microsoft WebMatrix 是微软最新的 Web 开发工具,它包含了构建网站所需要的一切元素.您可以从开源 Web 项目或者内置 ...

  10. UniversalApp启动页面设置

    在新建的一个UniversalApp中,我在Shared项目下添加了一个页面,新建的页面名称为InitPage.xaml,现在我想把InitPage.xaml作为起始页,但是在配置文件中未找到设置启动 ...