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地址;
LVS入门的更多相关文章
- LVS入门篇(五)之LVS+Keepalived实战
一.实验架构和环境说明 (1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台负载均衡服务器(一台为主机,另一台为备机), ...
- 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 ...
- LVS入门篇(三)之LVS的工作模式和调度算法
1.NAT模型 (1)原理图: ①.客户端(200.10.10.1)将请求发往前端的负载均衡器(114.100.80.10),请求报文源地址是CIP(客户端IP),后面统称为CIP),目标地址为VIP ...
- LVS入门篇(二)之LVS基础
1. LVS介绍 LVS是Linux虚拟服务器(LinuxVirtualServers),使用负载均衡技术将多台服务器组成一个虚拟服务器.它为适应快速增长的网络访问需求提供了一个负载能力易于扩展,而价 ...
- LVS入门篇(一)之ARP协议
1.概念 地址解析协议,即ARP(AddressResolutionProtocol),是根据IP地址获取物理MAC地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求广播到网络上 ...
- 使用LVS实现负载均衡原理及安装配置详解
负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...
- Iptables与LVS——从入门到放弃
防火墙什么是防火墙?防火墙其实就是一个隔离的工具,工作于主机或者网络的边缘,对于进出本主机或者网络的报文根据事先定义好的网络规则做匹配监测.防火墙可以简单地划分为两大类:主机防火墙 网络防火墙 ...
- Linux运维入门到高级全套常用要点
Linux运维入门到高级全套常用要点 目 录 1. Linux 入门篇................................................................. ...
- Linux运维工程师入门的10大实用工具
说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具. 我就大概列出这几方面,这样入门就基本没问题了. 工具如下: 1.Linux系统基础 这个不用说了,是基础中的基础,连这 ...
随机推荐
- 优化C/C++代码的小技巧
说明: 无意看到一篇小短文,猜测作者应该是一个图形学领域的程序员或专家,介绍了在光线(射线)追踪程序中是如何优化C/C++代码的.倒也有一些参考意义,当然有的地方我并不赞同或者说我也不完全理解,原文在 ...
- sublime text3 安装package
在sublime text2中安装package control插件的时候是执行python: import urllib2,os; pf='Package Control.sublime-packa ...
- ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法
方法一:通过Request.Form [HttpPost] public ActionResult Test() { string id=Reques ...
- 点餐APP 冲刺一总结
在冲刺一的过程中,我们小组每个人的任务相对来说都比较轻松,完成的进度也比较快, 主要的原因是每个人分配的任务都比较明确,大家也很积极地去完成.当然啦,我们在实现 项目的过程中也遇到了一些麻烦,主要是最 ...
- Winform开发框架之权限管理系统的改进
权限管理系统,一直是很多Mis系统和一些常见的管理系统所需要的,所以一般可以作为独立的模块进行开发,需要的时候进行整合即可,不需要每次从头开发,除非特殊的系统需求.我在Winform开发框架介绍中的随 ...
- C#实用杂记-EF全性能优化技巧
原文链接:http://www.makmong.com/947.html#comment-31 EntityFramework 优化建议 2016年1月15日 下午4:54 LEILINKANG ...
- Azure开发者任务之六:使用WCF Service Web Role
在本文中,我们将会在local development fabric上创建一个WCF服务角色,然后在一个控制台应用程序中使用它. WCF服务角色可以让我们创建一个WCF服务,并且把它托管在Window ...
- 【循序渐进学Python】10.模块和包
1.导入模块 任何Python程序都可以作为模块导入,只要Python解释器能找到我们定义的模块所在位置即可,一般来讲,在一个模块被导入时,Python解释器会按照下面的步骤进行搜索: 在当前所在目录 ...
- DataSet导出到Excel,并生成文件(C#实现,可合并行和列)
using System; using System.IO; using System.Data; using System.Reflection; using System.Diagnostics; ...
- wcf 双工
服务器 [ServiceContract(Namespace="http://www.artech.com/", CallbackContract = typeof(ICallba ...