Cluster

LMAP

  http,web object

  200, 50 dynamic

    Prefork, 2m

    10M

      500+150*2m

  200->1000

    800, 200

      1600+2000

Scale On: 向上扩展,

Scale Out: 向外扩展

Load Balancing: LB 负载均衡

Round Robin: 轮调

WRR: 加权轮调;

Cluster

  LB负责均衡集群: 提高服务的处理能力;

  HA:High AValiability 高可用集群: 提高服务在线能力;

    在线时间/(在线事件+故障处理时间)

    99%,99.9%,99.99%,99.999%

  HP(HPC): High Performance 高性能计算

    向量机

    并行处理集群

      分布式文件系统

      将大任务切割为小任务,分别进行处理的机制

Rync+inotify

hearbeat 心跳

health check: 健康检查

node: 节点

DC:事务协调员;

DAS: Direct Attached Storage 文件附加存储

NAS: Network Attached Storage 网络附加存储

split-brain: 脑裂

STONITH: Shoot The Other Node In The Head 爆头

fencing: 隔离

  节点级别: STONTIN

  资源级别:

3个节点,奇数;

分布式存储: 分布式文件系统

LB Cluster & LVS

负载均衡集群就是在多个提供了相同能力主机的前端提供一个分发起,接收用户请求,根据某种策略将用户请求给它分发至后端多台主机的集群类型,而且这种集成为了尽可能的工作正常,为了提供完善的工作能力还应该具备对于后端主机做健康状态检查功能,而它们实现将用户请求转发至后端主机的标准是取决于所谓的负载均衡器上调度算法,负责均衡器将处理所有的用户请求,因此真正对外提供服务的,用户所能够看到的地址也通常就是负载均衡器的外部地址,负载均衡器几乎决定了整个集群是否对外可用的,因此为了保证服务的可用性,一般还需要对负载均衡器提供高可用能力,问题是我们如何提供一个符合条件和复合需要的负载均衡器设备;

调度算法:

  rr

  wrr

Hardware

  F5, BIG IP

  Citrix, Netscaler

  A10

Software

  四层

    LVS 在四层实现,可以理解成四层交换机或四层路由设备,它能根据用户所请求的IP地址及端口号来实现将用户的请求分发至后端不同的主机;

  七层反向代理

    nginx 七层代理,只负责解析七层有限代理;

      http, smtp, pop3, imap

    haproxy

      http, tcp(mysql, smtp)

LVS(四层)

  Linux Virtual Server

  Cluster service: 172.16.100.1:80

director 调度器

realserver 真正服务器

LVS本身也是工作工作在内核的TCP/IP协议栈上的,它依然借鉴了netfilter的框架,当用户请求到达我们本机之后由于我们向外公布的地址,用户请求的就是我们本机地址,这些请求备从INPUT链到达本机了,如果我们本机的确有个服务监听这个端口就开始响应了,否则就拒绝,很显然我们LVS不能让它送到本机来,必须要能转发到其它主机上去,它如何才能转发,LVS工作在INPUT链上,当用户请求到达本机以后一旦发现请求的是本机地址又是本机端口,所以就立即转到INPUT链,而LVS就监控在INPUT链上,并且在这个地方设置有规则,一旦发现用户的是集群服务,它会强行修改这个数据报文的形成,本来过来INPUT链到内部去了,它不是这样子的它强行修改将报文送到FORWARD,并通过FORWARD到POSTROUTING转发至其它主机,所以LVS和iptables不能同时使用;

iptables/netfilter

LVS
  ipvsadm: 管理集群服务命令行工具

  ipvs: 内核

ipvs

ipvsadm

schedule method 调度方法

VIP: virtual IP

RIP: rs IP

DIP: director IP

CIP: Client IP

LVS IP Address Name Conventions

Virtual IP (VIP) address

  The IP address the Director uses to offer services to client computers

Real IP (RIP) address

  The IP address used on the cluster nodes

Director's IP (DIP) address

  The IP address the Director uses to connect to the D/RIP network

Client computer's IP (CIP) address

  The IP address assigned to a client computer that it uses as a source IP address for requests sent to the cluster

Types of LVS Clusters(LVS集群类型)

LVS clusters are usually described by the type of forwarding method the LVS Director uses to relay incoming requests to the nodes inside the cluster.

Three methods are currently available:

  Network address translation(LVS-NAT)(地址转换)

  Direct routing(LVS-DR)(直接路由)

  IP tunneling(LVS-TUN)(隧道)

LVS:

类型:

  NAT: 地砖转换

  DR: 直接路由

  TUN: 隧道

  NAT:

    集群节点跟director必须在同一个IP网络中;

    RIP通常是私有地址,仅用于各集群节点间的通信;

    director位于client和real server之间,并负责处理进出的所有通信;

    realserver必须将网关指向DIP;

    支持端口映射;

    realserver可以使用任意OS;

    较大规模应用场景中,director易成为系统瓶颈;

  DR: 直接路由

    集群节点跟director必须在同一物理网络中;

    RIP可以使用公网地址,实现便捷的远程管理和监控;

    director仅负责处理入站请求,响应报文则由realserver直接发往客户端;

    realserver不能将网关指向DIP;

    不支持端口映射;

  TUN: 隧道

    集群节点可以跨越Internet;

    RIP必须是公网地址;

    director仅负责处理入站请求,响应报文则由realserver直接发往客户端;

    realserver网关不能指向director

    只有支持隧道功能的OS才能用于realserver;

    不支持端口映射

NAT类型:

对于NAT类型来讲前端的director需要两个网络接口,一个网络接口面对互联网,让互联网用户能够访问到,而另外一个接口以内网的方式面向于各个realserver, 而用户请求到达的时候它是将用户请求的目标地址转换为realserver中的某一个地址方式来实现的,所说的NAT模型跟此前的DNAT的一样的,只不过不是目标而是多目标,因此能够实现负责均衡的功能,很显然它的工作机制也是跟DNAT一样,用户锁请求的报文到达这个位置的时候原地址是CIP,目标地址是VIP,但是VIP自身不提供任何服务,所有一旦发现用户请求的是个集群服务则通过地址转换以后挑选一个后端服务器向后转发,假如有三个服务器RIP1、RIP2、RIP3,于是这次如果director挑选了第二个服务器来响应,所以报文就送到RIP2上来了,这时候源IP是CIP,目标IP是RIP2,realserver2向后进行回应的时候,源地址是RIP2,目标地址是CIP,但是CIP没有访问RIP2,由此director还要负责将源地址RIP2转换成VIP,因此跟此前DNAT工作方式是一样的,只不过不需要写DNAT规则,而是要写IPVS规则;

DNAT

  多目标

Basic Properties of LVS-NAT(LVS-NAT遵循基本法则)

The cluster node need to be on the same network(VLAN or subnet) as the Director.(集群节点所有节点包括director和realserver必须要在同一个网络中)

The RIP addresses of the cluster nodes are normally private,non-routable IP addresses used only for intracluster communication.(RIP地址通常都是私有地址,而且仅能够用于跟DIP通信)

The Director intercepts all communication (network packets going in either direction) between the client computers and the real servers.(director将负责处理所有的通讯,包括进来的和出去的,)

The cluster nodes use the Director's DIP as their default gateway for reply packets to the client computers.(集群节点将需要DIP作为它们默认网关)

The Director can remap network port numbers.That is, a request received on the Director's VIP on one port can be sent to a RIP inside the cluster on a different port.(director可以实现端口映射)

Any type of operating system can be used on the nodes inside the cluster.(任何类型的操作系统都可以用于realserver)

A single Director can become the bottleneck for the cluster.(一个单独的director很有可能称为集群的瓶颈)

DR类型:

前端是路由设备,而路由设备连接进来以后内部有个交换机,这个交换机上连接了三个realserver和一个director, director是单网卡的就可以了,realserver也只有一个网卡,当用户请求来的时候通过交换机到director, director知道自己有三个realserver, 根据某种挑选标准挑选出一个realserver以后将请求直接转发至对应的realserver, sealserver就直接创建回应报文直接响应给客户端,不再需要经过director,所以director这是时候只负责处理进来的请求,只转发请求调度请求,而realserver可以直接响应给客户端,用户请求的时候源IP是CIP,目标IP是VIP,转发到realserver以后,realserver响应的时候原地址是RIP,目标地址是CIP,问题是CIP没有请求RIP,使用RIP响应不会接受,应该源IP为VIP,于是在DR模型当中这四个主机都配置了VIP,在director上有两个地址,依然是VIP和DIP,只不过一个配置在网卡上一个配置在网卡别名上,而每一个realserver既有VIP又有RIP,每个realserver的RIP是不能一样的,但是它的VIP地址不拿来接收任何请求,VIP地址是配置在网卡别名上的并且是隐藏的,所以在整个网络上广播一下询问谁是VIP,它们是不会回答的,它们也不会认为自己有VIP地址,所以他们的VIP地址配置是隐藏起来的,不做跟任何人通信用,只有在响应客户端请求的时候把它作为源地址使用,虽然响应的时候作为源地址使用,真正出去的网卡设备仍然是RIP所在的网卡,所以它只管做源地址使用了,真正通信跟它没有任何关系,它仅仅是封装报文VIP仅用于当作源地址,在这种场景下用户的请求到达路由器以后,我们的路由设备,本地网络通信路由器是怎么把报文发给director所在的VIP网卡的,两个设备之间的真正通信靠的是物理层地址,但是我们发报文的时候源地址目标地址都是在两个报文之间发的,尤其是目标地址,源地址可能不是路由器的接口地址,是CIP地址,但是路由器必须要把这个请求通过网络设备发给VIP,而发往VIP的时候它必须要能够通过报文封装成帧,封装成帧的时候源MAC是路由器MAC,目标MAC是VIP所在的网卡的MAC地址,它怎么知道VIP的MAC是什么,有ARP地址解析,要通过ARP协议做地址解析,所以做解析,ARP广播的,它广播每一个realserver也能收到,realserver不会响应,realserver自己也有VIP,它要响应路由器就混乱了,所以这三个主机一定不能拿VIP通信,而后只有director拿着VIP地址响应,所以来自于CIP的请求最终都交给director了,director怎么发给各个realserver呢,客户端请求的时候目标地址是VIP,源地址是CIP,我们要改目标地址改成RIP,人家请求的是VIP,直接用RIP响应不合适,因此director在实现转发的时候是不会改目标地址的,在DR模型当中director转发报文到各realserver的时候不是通过跟NAT一样通过改变目标地址的行为来实现的,而是仅仅修改了MAC地址来实现的,directory有自己的DIP,realserver有自己的RIP,它们本身又在同一个物理网络上,所以DIP、RIP1、RIP2、RIP3它们彼此之间可以互相通信,而且能够通过ARP解析得到各自的MAC地址的,所以当用户请求到达director的时候,director发现这用户请求的是集群服务,所以它不会拆IP首部的,只是把MAC首部拆了,再重新封装,源MAC改为director自己的MAC,目标MAC改为它所挑选的realserver的MAC,所以直接把这个报文发给realserver, 假如它挑选的是第二个主机,第二个主机接收到以后发现目标MAC就是自己的,他认为就是到达自己主机的,拆掉帧封装以后,目标IP是VIP,自己主机上有VIP地址,所以就认为到达自己本机了,由此报文接收下来,发现自己的确有客户端请求的服务,于是服务响应以后就要封装响应报文了,响应报文响应的时候,人家请求的目标是VIP,所以用VIP响应,所以用VID封装,于是源地址是VIP,目标是CIP,所以它就直接响应出去了,也就意味着它就不用将网关指向director了,所以这样场景当中director仅负责入站请求,对于响应的报文不做任何处理,这样子director会大大的解脱,请求报文都很小,响应报文都比较大,如果我们不在负责处理响应报文,它的性能立马提高N倍,所以LVS的NAT模型能带动10个realserver,那么DR模型能带动100台server是没问题的,所以性能超强;

Basic Properties of LVS-DR(DR模型遵循的法则)

The cluster nodes must be on the same network segement as the Director.(各集群节点必须要director在同物理网络中)

The RIP addresses of the cluster nodes do not need to be private IP addresses.(RIP地址可以不用是私有IP)

The Director intercepts inbound(but not outbound) communication between the client and the real servers.(director仅负责处理入栈请求,响应报文直接由realserver发往客户端)

The cluster nodes (normally) do not use the Director as their default gateway for reply packets to the client computers.(集群节点一定不能使用director当作它们的默认网关)

The Director cannot remap network port numbers.(director无法实现端口映射)

Most operating systems can be used on the real servers inside the cluster.(大多数操作系统都可以用到realserver上,因为realserver必须要求隐藏VIP)

An LVS-DR Director can handle more real servers than an LVS-NAT Director.(DR director能够处理比NAT director处理多的多realserver)

TUN类型:

各realserver在不同地域,用户请求的时候通过DNS解析仍然到director的VIP上,它跟DR模式一样,所以每个realserver即有RIP也有VIP,RIP必须是公网IP,当用户请求到达director以后,director仍然选择一个realserver响应,但是director跟我们对应的各realserver并不在同一个网络上,该怎么转发,跟DR模型一样,最终各realserver在响应的时候是直接响应给客户端的,也就意味着我们realserver在响应的时候必须要以VIP作为源地址,所以三个realserver必须要有VIP地址,在互联网上我们还得确保用户请求的时候,请求VIP必须要到达director, 很显然这些VIP在互联网上是不可见的,所以转发的时候不可能是以VIP作为目的进行转发,当用户请求到达director的时候,源地址是CIP,目标地址是VIP,director收到以后必须要找一台主机转发,而转发它俩之间必须要通信,它在实现向外转发的时候比如挑选第二个realserver,向外进行转发的时候CIP和VIP不能动,不做任何修改,而是在它外部再加上IP首部,两个IP首部,外面的IP首部源地址是DIP,目标地址是RIP2,RIP2收到以后拆掉外层封装,发现还有层封装,不管怎么讲外层封装可以将报文送达到realserver,realserver拆掉第一层封装看第二层封装,源请求是CIP,目标是VIP,自己有VIP地址,于是创建响应报文,目标地址是CIP,源地址是VIP,这个报文通过网络路由以后直接到达client,不再需要经过director,在这种模型下它要求director和realserver必须要支持隧道机制才可以;

Basic Properties of LVS-TUN(TUN模型遵循的法则)

The cluster nodes do not need to be on the same physical network segment as the Director.(各集群节点没必须在同一个物理网络中)

The RIP addresses must not be private IP addresses.(realserver的RIP必须是公网地址)

The Director can normally only intercept inbound communication between the client and the cluster nodes.(director仅处理用户入站请求)

The return packets from the real server to the client must not go through the Director.(The default gateway can't be the DIP;it must be a router or another machine separate from the Director.)(响应报文一定不能通过director)

The Director cannot remap network port numbers;(不支持端口映射)

Only operating systems that support the IP tunneling protocol can be servers inside the cluster.(只有支持隧道功能的OS才能用于realserver)

知识恢复:

  LB: 负载均衡集群,提供更好的服务访问容量;

  HA: 高可用集群,服务可用性;

  HP: 高性能集群,解决复杂问题;

LB:

  Hardware

    F5 BIG-IP

    Citrix NetScaler

    A10

  Software

    四层
      LVS

    七层

      nginx

      haproxy

LVS: Linux Virtual Server

Type:

  NAT:

    类似DNAT

  DR:

    只处理请求报文,不处理响应报文;

  TUN:

    类似DR模型,可以让各realserver工作在互联网上,director将各请求转交给realserver时候将IP报文以IP隧道方式做了二次封装之后再发给各realserver;

director本身是通过某种调度算法计算以后挑选下一个真正响应用户请求的realserver,根据一定的调度算法来进行挑选realserver的;

固定调度

四种静态:因为它们在调度的时候是不考虑当前服务器是空闲还是繁忙的;

  rr: 轮叫,轮询

  wrr: Weight, 加权轮询,考虑服务器权重的意义,一旦加权,下次挑选他会每一次在实现轮询的时候给这样主机多轮一个,以权重之间的比例进行轮询,在轮调之前它会计算一下各服务器之间权重的比例,而后按照这样的比例实现在各主机之间进行调度;

  sh: source hash, 源地址hash,只要是来自同一个客户端的用户请求都发至同一个realserver;

  dh: 目标地址hash,以目标地址为标准进行挑选,将来自于同一个地址请求发送给同一个realserver,只不过二者的挑选标准在算法上有所不同;

动态调度方法:

  lc: 最少连接,它是通过计算当前后端每一个realserver的活动连接数和非活动连接数的总数并做比较以后,那个一个数目小,就挑选那一个;

    active*256+inactive

    谁的小,就挑谁(充分考虑当前的连接数目的状况,并挑选一个最空闲的服务器发给它)

  wlc: 加权最少连接,考虑权重;

    active*256+inactive/weight

  sed: 最短期望延迟

    (active+1)*256/weight

  nq: never queue 永不排队,基于sed,不考虑非活动连接状况

  LBLC: 基于本地最少连接;

    DH: LBLC动态DH;

  LBLCR: 基于本地的带复制功能的最少连接

默认方法: wlc(最理想的调度方法)

lvs:
  ipvsadm: 定义管理集群服务的命令行工具;

  ipvs: 内核模块;(内核中必须有这种功能,没有需要找别人编译好的符合内核的rpm包安装,或者手动重新编译内核)

ipvsadm:

  管理集群服务

    添加: -A -t|u|f service-address [-s scheduler](-A添加集群服务,service-address服务器地址,-s scheduler指定调度方法,默认wlc)

      -t: TCP协议的集群服务,指定realserver服务器协议,比如TCP的web服务;

      -u:UDP协议的集群服务,指定realserver服务器协议,比如UDP的DNS服务;

        service-address: IP:PORT

      -f: FirewallMark,FWM,防火墙标记;

        server-address: Mark Number(防火墙标记号)

    修改: -E -t|u|f service-address [-s scheduler](-E修改集群服务,service-address服务器地址,-s scheduler指定调度方法,默认wlc)

      -t: TCP协议的集群服务,指定realserver服务器协议,比如TCP的web服务;

      -u:UDP协议的集群服务,指定realserver服务器协议,比如UDP的DNS服务;

        service-address: IP:PORT

      -f: FirewallMark,FWM,防火墙标记;

        server-address: Mark Number(防火墙标记号)

    删除: -D -t|u|f service-address(-D删除集群服务)

    # ipvsadm -A -t 172.16.100.1:80 -s rr(将172.16.100.1:80服务添加为集群服务,-A添加集群服务,-t协议为TCP,-s指定调度算法)

  管理集群服务中的RS;

    添加: -a -t|u|f service-address -r server-address [-g|i|m] [-w weight] [-x upper] [-y lower](-a 向一个已经定义好的集群服务添加realserver)

      -t|-u|-f service-address: 事先定义好的某集群服务;

      -r server-address: 器RS的地址,在NAT模型中,可使用IP:PORT实现端口映射;

      [-g|i|m]: LVS类型

        -g: DR,默认模型;

        -i: TUN

        -m: NAT

      [-w weight]: 定义服务器权重

    修改: -e

    删除: -d -t|u|f service-address -r server-address(从那个集群删除把那个realserver删除)

    # ipvsadm -a -t 172.16.100.1:80 -r 192.168.10.8 -m

    # ipvsadm -a -t 172.16.100.1:80 -r 192.168.10.9 -m

  查看
    -L|-l [options]:

      -n: 数字显示IP地址和端口号;

      --stats: 显示统计信息;

      --rate: 速率

      --timeout: 显示tcp、tcpfin和udp的会话超时时长;

      -c: 显示当前ipvs连接状况;

  删除所有集群服务

    -C:清空ipvs规则

  保存规则

    -S

    # ipvsadm -S > /path/to/samefile(将ipvs规则保存到samefile文件)

    # service ipvsadm save

  载入此前的规则:

    -R

    # ipvsadm -R < /path/from/somefile

    # service ipvsadm restart

各节点之间的事件偏差不应该超出1秒钟:

NTP: Network Time Protocol 时间服务器;

sh:

前端有很多客户端,而其中它能够根据客户端的来源来判定这个请求者是谁,在一定的时间范围内只要这个请求到来的时候,只要这个请求的IP地址是同一个,它就一定会给它发往同一个realserver,比如1号客户端发起请求的时候,director发现此前没访问过于是根据轮询算法挑选一个服务器,假如发给的A号服务器,后来又来了第2号客户端,它来访问根据轮询方法2号客户端很有可能发给B服务器,过一会又来了第3个请求,1号客户端又发起了一个请求,这时候应该发给,按道理应该发给C服务器,但现在不是,sh指的是只要是来自同一个客户端,而且此前挑选过服务器,此后同一个客户端请求一定会被发往跟第一次相同的服务器上,sh它的意思叫源地址hash,它能够根据每一个用户的源地址把每一个客户端的源地址做一次hash计算,在director内部保持了一张hash表,其中某个hash结果挑选了那个服务器,又一个hash结果挑选了什么服务器,还有一个hash结果挑选了什么服务器,每一个客户端来访问的时候它首先会计算一下这个用户请求客户端的hash结果并比较在这里面是不是曾经挑选过服务器,只要有对应条目,下一次的访问一定会被转发到同一台主机上,在一定程度上破坏了公平调度的效果,但为什么我们要用到这种结果,比如我们是一个web服务器,就以电商网站为例,每一个用户在访问一个服务器的时候,它浏览商品以后,这个服务器为了追踪用户此前发过什么商品在购物车里面加入过什么商品,这些信息用户曾经浏览的商品以及加入到购物车中的商品一般都会通过一个session来保存的,它是结合所谓的cookie进行的,web协议是无状态的,每一次的用户请求都会被视作全新的请求,它无法识别这个客户端来源跟此前发起请求的来源有什么样的关系,并别提认证,但是现在访问很多的web服务器程序是能够让我们进行认证的,比如论坛登录进来以后可以不断的来连续的发帖,那它又是怎么实现的,web协议是无状态的,所以同一个客户端发起第一次请求,只要这个请求断开了,它再发一个请求,我们的服务器根本不知道这是同一个用户同一个客户端,所有就算我们用户登录了,等这个连接断开,用户再一次访问的时候,它还得让用户重新登录,因为服务器根本不知道是谁,如果我们发一个帖子就要认证一次,就会抓狂的,所以后来web服务器程序就引入了叫cookie的机制,cookie是用来实现让服务器端追踪客户端的一种机制,当我们的客户端第一次访问服务器的时候,服务器会返回一个身份标识信息给客户端,客户端会保存在浏览器的某个缓存目录里面,而后同一个客户端再访问网站,它在发起请求的时候会自动的把自己此前曾经拥有服务器端发来的身份标识信息再会送给服务器端,服务器端借此来追踪客户端身份,客户端第一次访问服务器的时候服务器会生成一个随机的标识给客户端,客户端会把保存到本地叫cookie的文件当中,而后我们客户端再一次访问这个服务器站点的时候它都会把这个cookie信息附加到请求里面,每发一个请求都会附加cookie信息的,所以每一次请求后续的我们服务器都能接收到客户端的cookie信息,进而它就能知道只要两次访问的cookie信息是一样的它就知道这是同一个客户端,所以它即要发标识信息给客户端同时还要记录下来此前客户端曾经访问的URL连接,以及用户的身份认证信息等等都有可能在cookie里面,早起的cookie我们的客户端是没法区分网站的,它向每一个网站发送请求的时候都会从cookie信息里面选一批发送给服务器端,这是相当危险的,我们的服务器借此就知道曾经此前这个客户端访问过那些网站,所以它可以借此可以收集客户的浏览行为,你曾经上过什么网站,访问过这个网站那些网页,你喜欢看什么东西,于是有目的的给你投放信息,还能搜集你的个人隐私,cookie有可能会被滥用的,所以为了避免这种情况现在都实行清空cookie,在每一个客户端它仅保存用户的标识信息,而不再保存用户曾经浏览的商品等各种信息了,这些信息保存在服务器端,服务器在它的内存当中为每一个身份标识关联一段内存区域,它在这个内存区域里面保存有用户曾经浏览过的商品信息,而这个内存区域当中所保存的信息的机制叫做session,session其实也是借助于cookie才能实现用户身份的,这只是其中一种机制,基于URL重写也能追踪的,cookie需要保存在客户端,cookie信息是由服务器端生成的一段随机码,客户端需要保存下来,而后每一次客户端再发起的请求都会附加到cookie,服务器端就借此cookie来标识每一个用户的身份,并且在本地内存内部为维持内存区域保存有此前用户曾经访问的商品浏览的URL等等信息,一旦用户过期了用户不再访问了,要给session设置一个过期时间,当超出以后这个session会被清除的,一个用户通常访问一个电子商务的网站它可能访问两个小时三个小时都有可能,这个session保存时间可能比较长一点,假如我们这里就是一个电商网站,用户第一次访问的时候我们把它定义到A上来了,它在购物车上加了一个商品,下次访问一刷新定义到B上来了,此前加的商品就没有了,加的商品是保存在session当中的,所以必须要把同一个客户端请求定义到同一个服务器上,所以它的主要功能用于实现session affinity(会话绑定),能够让用户此前建立的session信息继续使用的,在一定程度上它的确能够损害负载均衡效果,但是为了用户session, 为了具体应用这种机制也是非常必要的,如果说A主机损坏了,这时候A主机上保存的session会丢失,A用户浏览的商品加入到购物车上商品会丢失,当1号一访问那个网页,它告诉你A主机不在了,所以返回一个错误页面,这时候用户可能会刷新,一刷新就是新请求,我们服务器根据健康状态一检查发现A主机不在了,所以有可能给它定义到C上去了,C上没有它浏览的商品信息,所以这时候1号主机会迷茫的,又不得不重新加,所以为了避免其中一个服务器会损坏而导致我们的session丢失,如果能够将各主机的session永久保存就好了,比如在后面做一个共享存储,session是在内存当中的,我们即要在内存中保存一份,它还不时的同步到一个共享存储里面,其它主机彼此都保存,保存完以后还要去读,读的是本来不属于自己的,这时候A、B、C三个主机都有所有主机的session了,这样子在负载均衡到任何一个主机都没事,这是session共享的机制,所以在某些特殊场景下,可能需要做session sharing,如果把A、B、C做成session共享集群,每一个主机在内存session会自动通知给其它主机,让其它主机在内存中保存一份,它们彼此之间互相同步,这样会更好很多,在它们三个之间再建一个集群,建一个session共享集群,它们彼此之间都会将自己内存中所保存的session信息通过网络发送给其它主机,每一个主机就保留了当前网络内所有主机上的session,那由此1号主机发给谁都没问题,所以只要能session共享就不用sh,否则就只能使用sh了;

http stateless

cookie 小甜点

session affinity 会话绑定

session sharing 会话共享

Destination hashing(目标地址hash)

  This method always sends requests for the same IP address to the same server in the cluster.(将同一个请求发送给同一个server)

  Like the locality-based least-connection (LBLC) scheduling method,this method is useful when the servers inside the cluster are really cache or proxy servers.

Source hashing

  Can be used when the Director needs to be sure the reply packets are sent back to the same router or firewall that the requests came from.

  Director has more than one physical network connection, so that the Director knows which firewall or router to send the reply packet back through to reach the proper client computer.

dh:

一个director后面是两个缓存服务器,我们发起某一个请求,这个请求备轮到第一个缓存服务器上来,如果缓存服务器上没有,找原始服务器去获取,从原始服务器获取以后先要缓存到本地,而后再返回给客户端,如果第二次又有一个客户端请求了同一个内容,应该发往同一个服务器,不然缓存就没法命中了,由此对于同一个请求都给它发往同一个缓存服务器,以缓存服务器为目标进行挑选,只要是同一个请求,都给它发往同一个缓存服务器,这又是一种挑选标准;

rr在实现将用户的请求调度到后台realserver去的时候轮流,假如来了1000个请求,刚开始调度的时候,第一个500个,第二个也有500个,所以1000个请求它是均分的,过一段时间会发现发往第一个realserver的都没有下线,500个请求有400个还都在线,而发给rs2的500个请求只有10个在线,剩下490个统统都已经下线了,这时候再来新的发给rs1,过一段时间rs1忙死了,rs2很空闲,director不管这个的,因为此前派发是公平的每个人500个,所以它并不考虑当前服务器目前的连接状况的,所以叫静态方法,纯粹就根据算法本身进行调度,不考虑服务器已经建立起来的活动连接和非活动连接的状况,对于一个服务器来讲,活动连接,用户请求进来而且正在实现数据传输,这种称为活动连接,连接建立数据传输已经结束,但尚未断开,比如有些长连接,它此时不获取数据了,但是这个会话依然没有断开,仍然在线,这种连接称为非活动连接,活动连接有大量的数据交换,所以它可能占据了很多内存空间,而对于非活动连接它只需要维护当前会话状态就可以了,所以只需要很小很小的内存空间就足以,比如几个K,甚至来回都算上也就10几K就足够了,所以如果我们在调度的时候如果考虑服务器当前连接状况还应该区别对待活动和非活动的,甚至有时候可以无视非活动的,因为它占据的资源很少;

active

Least-connection(LC)(最少连接)

When a new request for a service running on one of the cluster nodes arrives at the director, the Director looks at the number of active and inactive connections to determine which cluster node should get the request.

  For each node in the cluster, the Director multiplies the number of active connections the cluster node is currently servicing by 256, and then it adds the number of inactive connections (recently used connections) to arrive at an overhead value for each node.

  The node with the lowest overhead value wins and is assigned the new incoming request for service.

  If the mathematical calculation results in the same overhead value for all of the cluster nodes, the first node found in the IPVS table of cluster nodes is selected.

wlc:

有两个realserver,第一个realserver的权重为20,第二个realserver的权重为5,很显然第一个是第二个的4倍,假如第一个realserver目前来讲活动连接数是60,非活动连接数是300,第二个realserver活动连接数是20,非活动连接数是100,下一个请求应该给第一个realserver,缺陷,无法在各realserver都没有连接情况下挑选性能好的realserver;

sed:

可以实现那个realserver的性能好先挑选谁,但是权重小的可能一直没有请求;

nq:

先不管权重,每个realserver先处理一个请求,再按照权重进行挑选realserver;

Locality-Based least-Connection (LBLC:DH)(基本本地最少连接)

The Director attempts to send all requests destined for a particular IP address (a particular web server) to the same transparent proxy server (cluster node).

  In other words,the first time a request comes in for a web server on the Internet, the Director will pick one proxy server to service this destination IP address using a slightly modified version of the WLC scheduling method, and all future requests for this same destination IP address will continue to go to the same proxy server.

  This method of load balancing, like the destination-hashing scheduling method described previously, is , therefore, a type of destination IP load balancing.

  In this method, the Director tries to associate only one proxy server to one destination IP address.

LBLC

尽管为了实现缓存命中率的提高,要保证将来自同一个用户请求发往同一个缓存服务器,但是也要保证考虑下一次轮询的时候该轮给谁,而不是从上而下挑选,而是挑选空闲的服务器,来一个新的连接它挑选的是一个空闲服务器,所以要考虑后端服务器的连接状况,但是这样以来它有可能会破坏命中率的,LBLC只不过是动态的DH,但它仍然尽可能保证对同一个资源请求发给同一个realserver,如果我们既能保证命中率又能尽可能保证负载均衡效果会更好;

LBLCR:(基于本地带复制功能最少连接)

Locality-Based Least-Connection with Replication Sheduling (LBLCR)

  The LBLCR scheduling method (which is also a form of destination IP load balancing) attempts to improve on the LBLC scheduling method by maintaining a set of proxy servers that can service each destination IP address.

  When a new connection request comes in, the Director will select the proxy server with the fewest number of active connections from this set of servers.

LBLCR和LBLC区域在于,LBLC尽可能保证,它就是一个DH,它并不考虑连接数状况的,但LBLCR不是如此,无论此前请求是谁发来的,只要是个新请求,一定会发给连接数比较少的realserver,这样会破坏命中率的,因为LBLCR带复制功能,两个缓存服务器之间可以通过内容交换协议或者内容分发协议实现缓存共享的,第一个主机的缓存内容可以共享给第二个主机,所以无论请求向那一台主机,这个主机上如果没有请求的内容,它不会到原始服务器上去找,而是到另外一个缓存那里,所以这种机制叫做缓存复制机制,这两台缓存服务器备称为兄弟服务器,缓存服务器在内容共享的时候彼此之间是可以共享缓存对象的,虽然缓存是共享的但并不是把所有内容都共享,当一个新的请求来了,这个缓存在A号服务器上,B服务器上没有,但是将请求发到B服务器,就意味着B服务器要到A服务器上去取,这中间有时间延迟,结果能命中,但是性能会差一点,如果它直接到A服务器取会快的多,A共享给B的并不是无论要不要都共享给你,你需要要什么到我这里来要,所以还是先发给A要好的多,所以尽可能保证同一个缓存请求到同一个服务器,只有对新请求才往新服务器重新挑选;

LVS_NAT模型案例:

环境介绍: 一个direcotr,外围地址是面向互联网,是一个公网地址,realserver使用私有地址,director有两块网卡,一个为向外提供服务器的地址,一个用于连接各RIP的,各RIP的网关指向DIP,两个realserver都是两个web服务器,director主机eth0网卡使用vmnet0桥接模式,eth1网卡使用vmnet1仅主机模式,RS1主机的eth0网卡使用vmnet1仅主机模式,RS2主机的eth0网卡使用vmnet1仅主机模式;

VIP: 172.16.100.1

DIP: 192.168.10.7

RIP1: 192.168.10.8

RIP2: 192.168.10.9

DR:

[root@localhost ~]# ifconfig(查看网卡接口信息)   
eth0      Link encap:Ethernet  HWaddr 00:0C:29:CC:FA:AE  
          inet addr:172.16.100.1  Bcast:172.16.100.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fecc:faae/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1046 errors:0 dropped:0 overruns:0 frame:0
          TX packets:442 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:91218 (89.0 KiB)  TX bytes:62936 (61.4 KiB)
          Interrupt:67 Base address:0x2000 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:CC:FA:B8  
          inet addr:192.168.10.7  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fecc:fab8/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:407 errors:0 dropped:0 overruns:0 frame:0
          TX packets:214 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:41483 (40.5 KiB)  TX bytes:18867 (18.4 KiB)
          Interrupt:83 Base address:0x2080 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:66 errors:0 dropped:0 overruns:0 frame:0
          TX packets:66 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:17446 (17.0 KiB)  TX bytes:17446 (17.0 KiB)

[root@localhost ~]# grep -i 'ipvs' /boot/config-2.6.18-308.el5(找出config-2.6.18文件包含ipvs字符的段,-i忽略大小写,)
# IPVS transport protocol load balancing support
# IPVS scheduler
# IPVS application helper
提示:/boot/config-2.6.18-308.el5是编译内核时参数配置文件;
[root@localhost ~]# grep -i 'vs' /boot/config-2.6.18-308.el5(找出config-2.6.18文件包含vs字符的段,-i忽略大小写) 
CONFIG_IP_VS=m(将ipvs定义为模块)
# CONFIG_IP_VS_DEBUG is not set
CONFIG_IP_VS_TAB_BITS=12
# IPVS transport protocol load balancing support(支持那些负载)
CONFIG_IP_VS_PROTO_TCP=y(TPC定义集群服务)
CONFIG_IP_VS_PROTO_UDP=y(UDP定义集群服务)
CONFIG_IP_VS_PROTO_ESP=y(ipsec相关协议功能)
CONFIG_IP_VS_PROTO_AH=y(ipsec相关协议功能)
# IPVS scheduler(ipvs调度算法)
CONFIG_IP_VS_RR=m
CONFIG_IP_VS_WRR=m
CONFIG_IP_VS_LC=m
CONFIG_IP_VS_WLC=m
CONFIG_IP_VS_LBLC=m
CONFIG_IP_VS_LBLCR=m
CONFIG_IP_VS_DH=m
CONFIG_IP_VS_SH=m
CONFIG_IP_VS_SED=m
CONFIG_IP_VS_NQ=m
# IPVS application helper
CONFIG_IP_VS_FTP=m(代理ftp)
CONFIG_SCSI_MVSAS=m
CONFIG_MOUSE_VSXXXAA=m
CONFIG_MAX_RAW_DEVS=8192
提示:由此可见内核已经有这样功能了,我们不需要添加任何东西了,所以我们只要安装上ipvsadm就可以了;
[root@localhost ~]# ls /etc/yum.repos.d/(查看/etc/yum.repos.d目录文件及子目录)
rhel-debuginfo.repo 
[root@localhost ~]# wget ftp://172.16.0.1/pub/gls/server.repo -O /etc/yum.repos.d/server.repo(通过互联网下载server.repo文件保存到/etc/yum.rep
os.d目录叫server.repo,-O更改保存目录)
[root@localhost ~]# vim /etc/yum.repos.d/server.repo(编辑server.repo)

[Server]
name=Server
baseurl=file:///media/Server
enabled=1
gpgcheck=0
[VT]
name=VT
baseurl=file:///media/VT
enabled=1
gpgcheck=0
[Cluster]
name=Cluster
baseurl=file:///media/Cluster
enabled=1
gpgcheck=0
[ClusterStorage]
name=ClusterStorage
baseurl=file:///media/ClusterStorage
enabled=1
gpgcheck=0

提示:确保一定要配置Cluster项,因为ipvsadm软件包在Cluster当中,因为它本来就是集群;
[root@localhost ~]# yum -y install ipvsadm(通过yum源安装ipvsadm软件,-y所有询问回答yes)
[root@localhost ~]# man ipvsadm(查看ipvsadm的man帮助)

       ipvsadm - Linux Virtual Server administration

       ipvsadm -A|E -t|u|f service-address [-s scheduler]
               [-p [timeout]] [-O] [-M netmask]
       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
               [-g|i|m] [-w weight] [-x upper] [-y lower]
       ipvsadm -d -t|u|f service-address -r server-address
       ipvsadm -L|l [options](查看ipvs规则)
       ipvsadm -Z [-t|u|f service-address](清空计数器)
       ipvsadm --set tcp tcpfin udp
       ipvsadm --start-daemon state [--mcast-interface interface]
               [--syncid syncid]
       ipvsadm --stop-daemon state
       ipvsadm -h

[root@localhost ~]# ipvsadm -L(查看集群服务)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@localhost ~]# ipvsadm -L -n(查看集群服务,-n以数字显示)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@localhost ~]# man ipvsadm(查看ipvsadm的man帮助)

       ipvsadm - Linux Virtual Server administration

       ipvsadm -A|E -t|u|f service-address [-s scheduler](-A和-E使用格式一样)
               [-p [timeout]] [-O] [-M netmask]
       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(-a和-e使用格式一样)
               [-g|i|m] [-w weight] [-x upper] [-y lower]
       ipvsadm -d -t|u|f service-address -r server-address
       ipvsadm -L|l [options](查看ipvs规则)
       ipvsadm -Z [-t|u|f service-address]
       ipvsadm --set tcp tcpfin udp
       ipvsadm --start-daemon state [--mcast-interface interface]
               [--syncid syncid]
       ipvsadm --stop-daemon state
       ipvsadm -h

       -L, -l, --list(查看Ipvs规则)
              List the virtual server table if no argument is specified. If a service-address is selected, list this service only. If the 
-c option is selected, then  dis-
              play the connection table. The exact output is affected by the other arguments given.

       -n, --numeric(数字格式显示IP地址和端口号)
              Numeric  output.   IP  addresses  and  port  numbers  will  be printed in numeric format rather than as as host names and se
rvices respectively, which is the
              default.

       --stats(统计信息,和-L一起使用)
              Output of statistics information. The list command with this option will display the statistics information of services and 
their servers.

       --rate Output  of rate information. The list command with this option will display the
              rate information (such as connections/second, bytes/second and  packets/second)
              of services and their servers.(用来输出速度信息,每秒连接数,每秒字节数,每秒包个数)

       --timeout(用于输出tcp超时时间限定,)
              Timeout  output.  The  list  command with this option will display the  timeout
              values (in seconds) for TCP  sessions,  TCP  sessions  after  receiving  a  FIN
              packet, and UDP packets.

       --daemon(显示进程状态以及多播端口的)
              Daemon  information  output. The list command with this option will display the
              daemon status and its multicast interface.

       --sort Sort the list of virtual services and real servers. The virtual service entries
              are  sorted  in  ascending  order by <protocol, address, port>. The real server
              entries are sorted in ascending order by <address, port>.(对列出来的ipvs集群服务以及每个集群服务中的realserver做排序的,默认是升序,根
据协议地址端口做升序排序)

       -c, --connection(显示当前连接状况)
              Connection output. The list command with this option  will  list  current  IPVS
              connections.


[root@localhost ~]# ipvsadm -L -n(显示ipvs中的规则,-L列出规则,-n以数字显示IP地址和端口号)      
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@localhost ~]# ipvsadm -L -n --stats(显示ipvs中的规则,-L列出规则,-n以数字显示IP地址和端口号,--stats统计信息)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts(入栈报文个数)  OutPkts(出栈报文个数)  InBytes(入栈字节数) OutBytes(出栈字节数)
  -> RemoteAddress:Port
[root@localhost ~]# ipvsadm -L -n --rate(显示ipvs中的规则,-L列出规则,-n以数字显示IP地址和端口号,--rate速率)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port                 CPS(每秒连接数)    InPPS(每秒入栈报文个数)   OutPPS(出栈包个数)    InBPS(入栈字节个数)   OutBPS(出栈
字节个数)
  -> RemoteAddress:Port
[root@localhost ~]# ipvsadm -L -n --timeout(显示ipvs中的规则,-L列出规则,-n以数字显示,--timeout显示tcp会话的超时时间限定)
Timeout (tcp tcpfin udp): 900 120 300
提示:当前ipvs的tcp协议、tcpfin、udp协议超时时间长度等等,可以修改默认时间长度;
[root@localhost ~]# ipvsadm -L -n --daemon(显示ipvs中的规则,-L列出规则,-n以数字显示,--daemon显示进程状态以及多播端口的)
提示:当前没有启动,所以不会显示进程;
[root@localhost ~]# ipvsadm -L -n -c(显示ipvs中的规则,-L列出规则,-n以数字显示IP地址和端口号,-c显示当前连接状况)
IPVS connection entries
pro expire state       source             virtual            destination
[root@localhost ~]# man ntpdate(查看ntpdate客户端工具的man帮助)

       ntpdate - set the date and time via NTP(根据ntp调整日志和时间)

       ntpdate [ -46bBdqsuv ](ip协议版本) [ -a key ] [ -e authdelay ] [ -k keyfile ] [ -o version ] [  -p
       samples ] [ -t timeout ] [ -U user_name ] server [ ... ]

[root@localhost ~]# date(查看当前系统事件)
Sat Nov 22 06:49:03 CST 2014
[root@localhost ~]# ntpdate 172.16.0.1(根据ntp服务器调整时间)
[root@localhost ~]# service ntpd start(启动ntpd服务)
Starting ntpd:                                             [  OK  ]
[root@localhost ~]# vim /etc/ntp.conf(编辑ntp服务配置文件ntp.conf)

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org

#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 key 42  # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

RS1:

[root@localhost ~]# ifconfig eth0(查看eth0网卡接口信息)
eth0      Link encap:Ethernet  HWaddr 00:0C:29:B8:44:39  
          inet addr:192.168.10.8  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feb8:4439/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:74 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13563 (13.2 KiB)  TX bytes:13127 (12.8 KiB)
          Interrupt:67 Base address:0x2000 
[root@localhost ~]# route -n(显示路由表,-n以数字显示)
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.100.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         192.168.10.7    0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# wget ftp://192.168.0.254/pub/gls/server.repo -O /etc/yum.repos.d/server.repo(通过互联网ftp下载server.repo保存到/etc/yum.re
pos.d目录叫server.repo)
[root@localhost ~]# yum -y install httpd telnet-server(通过yum源安装httpd和telent服务,-y所有询问回答yes)

RS2:

[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D9:86:95  
          inet addr:192.168.10.9  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fed9:8695/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:116 errors:0 dropped:0 overruns:0 frame:0
          TX packets:77 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:14423 (14.0 KiB)  TX bytes:13211 (12.9 KiB)
          Interrupt:67 Base address:0x2000 
[root@localhost ~]# route -n(显示路由表,-n以数字显示)
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.100.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     0      0        0 eth1
0.0.0.0         192.168.10.7    0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# wget ftp://192.168.0.254/pub/gls/server.repo -O /etc/yum.repos.d/server.repo(通过互联网ftp下载server.repo保存到/etc/yum.
repos.d目录叫server.repo)
[root@localhost ~]# yum -y install httpd telnet-server(通过yum源安装httpd和telent服务,-y所有询问回答yes)

RS1:

[root@localhost ~]# ntpdate 192.168.10.7(通过ntp服务器同步时间)
13 Mar 15:55:10 ntpdate[4768]: step time server 192.168.10.7 offset -28801.308936 sec
[root@localhost ~]# echo "RS1.magedu.com" > /var/www/html/index.html(显示RS1.magedu.com输出到index.html文件)
[root@localhost ~]# setenforce 0(关闭selinux)
[root@localhost ~]# service httpd start(启动httpd服务)
启动 httpd:                                               [确定]

RS2:

[root@localhost ~]# ntpdate 192.168.10.7(通过ntp服务器同步时间)
[root@localhost ~]# echo "RS2.magedu.com" > /var/www/html/index.html(显示RS1.magedu.com输出到index.html文件)
[root@localhost ~]# setenforce 0(关闭selinux)
[root@localhost ~]# service httpd start(启动httpd服务)
Starting httpd:                                            [  OK  ]

RS1:

[root@localhost ~]# curl http://localhost(访问本机httpd服务)
RS1.magedu.com
[root@localhost ~]# curl http://localhost
RS1.magedu.com
[root@localhost ~]# curl -I http://localhost(获取httpd://localhost页面的http协议首部)
HTTP/1.1 200 OK
Date: Sun, 13 Mar 2016 08:08:33 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Sun, 13 Mar 2016 07:58:34 GMT
ETag: "2e3b3f-f-87e33e80"
Accept-Ranges: bytes
Content-Length: 15
Connection: close
Content-Type: text/html; charset=UTF-8

RS2:

[root@localhost www]# curl localhost(访问本机httpd服务)
RS2.magedu.com
[root@localhost www]# curl http://192.168.10.8(访问192.168.10.8的httpd服务)
RS1.magedu.com

DR:

[root@localhost ~]# ping 192.168.10.8(ping测试)
PING 192.168.10.8 (192.168.10.8) 56(84) bytes of data.
64 bytes from 192.168.10.8: icmp_seq=1 ttl=64 time=2.14 ms
64 bytes from 192.168.10.8: icmp_seq=2 ttl=64 time=0.302 ms

--- 192.168.10.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.302/1.221/2.140/0.919 ms
[root@localhost ~]# ping 192.168.10.9(ping测试)
PING 192.168.10.9 (192.168.10.9) 56(84) bytes of data.
64 bytes from 192.168.10.9: icmp_seq=1 ttl=64 time=2.53 ms
64 bytes from 192.168.10.9: icmp_seq=2 ttl=64 time=0.296 ms

--- 192.168.10.9 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.296/1.415/2.534/1.119 ms
提示:DR到达两天RS主机通信都正常;
[root@localhost ~]# curl http://192.168.10.8(访问192.168.10.8的httpd服务)
RS1.magedu.com
[root@localhost ~]# curl http://192.168.10.9(访问192.168.10.9的httpd服务)
RS2.magedu.com
[root@localhost ~]# ipvsadm -A -t 172.16.100.1:80 -s rr(添加集群服务,-A添加集群服务,-t协议tcp,-s指定调度方法)
[root@localhost ~]# ipvsadm -a -t 172.16.100.1:80 -r 192.168.10.8 -m(向172.16.100.1:80集群服务添加realserver192.168.10.8,-a添加realserver,
-t事先定义好的集群服务,-r指定realserver地址,-m指定为NAT模型) 
[root@localhost ~]# ipvsadm -a -t 172.16.100.1:80 -r 192.168.10.9 -m(向172.16.100.1:80集群服务添加realserver192.168.10.9,-a添加realserver,
-t事先定义好的集群服务,-r指定realserver地址,-m指定为NAT模型)
[root@localhost ~]# ipvsadm -L -n(显示集群服务,-n以数字显示)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.100.1:80 rr
  -> 192.168.10.9:80              Masq    1      0          0         
  -> 192.168.10.8:80              Masq    1      0          0    
提示:越先添加的realserver越在下面,如果权重一样它会自上而下进行负载;

测试:通过Windows的IE浏览器访问http://172.16.100.1的web服务,访问不到;

DR:

[root@localhost ~]# iptables -L -n(查看iptables中规则,-L列出规则,-n以数字显示)
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination    
提示:iptables和ipvs不能一起使用;
[root@localhost ~]# cat /proc/sys/net/ipv4/ip_forward(查看ip_forward文件内容)
0
[root@localhost ~]# echo 1 > !$(显示1输出到ip_forward文件)
echo 1 > /proc/sys/net/ipv4/ip_forward
提示:打开ipv4协议的网卡之间转发功能;

测试:通过Windows的IE浏览器访问http://172.16.100.1的web服务,访问的是RS1的页面;

 

刷新Windows的IE浏览器,访问的是RS2的页面;

DR:

[root@localhost ~]# ipvsadm -L -n(查看集群服务,-n以数字显示)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.100.1:80 rr
  -> 192.168.10.9:80              Masq    1      0          3         
  -> 192.168.10.8:80              Masq    1      0          4  
[root@localhost ~]# ipvsadm -L -n --stats(查看集群服务,-n以数字显示,--stats显示统计信息)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  172.16.100.1:80                    16       63       34     5672     2921
  -> 192.168.10.9:80                     8       30       15     2753     1524
  -> 192.168.10.8:80                     8       33       19     2919     1397
[root@localhost ~]# ipvsadm -E -t 172.16.100.1:80 -s wrr(修改集群服务172.16.100.1:80的调度方法为wrr,-E修改集群服务,-t执行集群服务协议为tcp,-s指
定调度方法)
[root@localhost ~]# ipvsadm -e -t 172.16.100.1:80 -r 192.168.10.8 -m -w 3(修改集群服务中的realserver,-e修改,-t指定集群服务,-r指定realserver,
-m指定为NAT模型,-w指定权重)
[root@localhost ~]# ipvsadm -e -t 172.16.100.1:80 -r 192.168.10.9 -m -w 1(修改集群服务中的realserver,-e修改,-t指定集群服务,-r指定realserver,
-m指定为NAT模型,-w指定权重)

测试:通过其它linux主机安装httpd服务后,通过ab命令压力测试172.16.100.1:80/index.html服务;

[root@localhost ~]# ab -c 100 -n 10000 http://172.16.100.1/index.html(httpd服务压力测试,-c并发量,-n总共发的请求)
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.16.100.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests


Server Software:        Apache/2.2.3
Server Hostname:        172.16.100.1
Server Port:            80

Document Path:          /index.html
Document Length:        15 bytes

Concurrency Level:      100
Time taken for tests:   4.165913 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      2770000 bytes
HTML transferred:       150000 bytes
Requests per second:    2400.43 [#/sec] (mean)
Time per request:       41.659 [ms] (mean)
Time per request:       0.417 [ms] (mean, across all concurrent requests)
Transfer rate:          649.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   29 294.3      0    3005
Processing:     0   11  12.8      2      35
Waiting:        0   10  12.8      1      34
Total:          0   40 294.6      2    3027

Percentage of the requests served within a certain time (ms)
  50%      2
  66%     27
  75%     28
  80%     29
  90%     30
  95%     31
  98%     33
  99%     36
 100%   3027 (longest request)

DR:

[root@localhost ~]# ipvsadm -L -n --stats(查看集群服务,-n以数字显示,--stats显示统计信息)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
TCP  172.16.100.1:80                 10018    50073    50042  3656402  5453907
  -> 192.168.10.9:80                  2508    12530    12515   915253  1364024
  -> 192.168.10.8:80                  7510    37543    37527  2741149  4089883
提示:大致192.168.10.8的realserver是192.168.10.9的realserver处理请求的3倍;
[root@localhost ~]# service ipvsadm save(保存集群服务配置)
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:      [  OK  ]
提示:ipvs虽然有脚步,但它不是服务,保存到/etc/sysconfig/ipvsadm;
[root@localhost ~]# ipvsadm -S > /etc/sysconfig/ipvsadm.web(保存集群服务规则)
[root@localhost ~]# cat !$(查看ipvsadm.web文件内容)
cat /etc/sysconfig/ipvsadm.web
-A -t 172.16.100.1:http -s wrr
-a -t 172.16.100.1:http -r 192.168.10.9:http -m -w 1
-a -t 172.16.100.1:http -r 192.168.10.8:http -m -w 3
[root@localhost ~]# ipvsadm -C(清空ipvs规则)
[root@localhost ~]# ipvsadm -L -n(查看集群服务,-n以数字显示)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
[root@localhost ~]# ipvsadm -R < /etc/sysconfig/ipvsadm.web(将ipvsadm.web中的规则输入到ipvs服务)
[root@localhost ~]# ipvsadm -L -n(查看集群服务,-n以数字显示)
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.16.100.1:80 wrr
  -> 192.168.10.8:80              Masq    3      0          0         
  -> 192.168.10.9:80              Masq    1      0          0   
[root@localhost ~]# ipvsadm -L -c(查看集群服务,-c显示ipvs连接状况)
TCP 01:46  TIME_WAIT   172.16.100.8:54358 172.16.100.1:http  192.168.10.8:http
TCP 01:45  TIME_WAIT   172.16.100.8:52910 172.16.100.1:http  192.168.10.8:http
TCP 01:47  TIME_WAIT   172.16.100.8:55309 172.16.100.1:http  192.168.10.8:http
TCP 01:46  TIME_WAIT   172.16.100.8:54166 172.16.100.1:http  192.168.10.8:http
TCP 01:46  TIME_WAIT   172.16.100.8:53464 172.16.100.1:http  192.168.10.8:http
TCP 01:44  TIME_WAIT   172.16.100.8:51194 172.16.100.1:http  192.168.10.8:http
TCP 01:47  TIME_WAIT   172.16.100.8:55993 172.16.100.1:http  192.168.10.8:http
TCP 01:47  TIME_WAIT   172.16.100.8:54776 172.16.100.1:http  192.168.10.8:http
TCP 01:45  TIME_WAIT   172.16.100.8:53006 172.16.100.1:http  192.168.10.8:http
TCP 01:47  TIME_WAIT   172.16.100.8:56756 172.16.100.1:http  192.168.10.8:http
TCP 01:46  TIME_WAIT   172.16.100.8:53256 172.16.100.1:http  192.168.10.8:http
提示:当前尚未断开了,位于服务器模版当中正在处于计时有那个客户端访问的服务器;