天天看点

服务器架构之性能扩展-第二章(3) 第二章:Lvs基础:

首先了解一下OSI模型:

<a target="_blank" href="http://blog.51cto.com/attachment/201208/115947257.png"></a>

物理层定义了线缆的标准,数据链路层完成了数据包的加封,它加了一个源MAC地址和目的MAC地址,网络层是加入了源IP地址和目的IP地址,传输层加入了源端口和目的端口,会话层表示层和应用层是应用程序,会话层定义了通信状态,表示层是通信的编码状态。加封和解封过程,完成一个数据的通信。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120016447.png"></a>

数据11-》44,首先给信息给交换机,交换机收到记录源地址,发送广播,等待44的回应。44回应,交换机会加下目的地址,下次直接发送就可以了。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120035628.png"></a>

路由器是连接不同网段的设备。口较少。目的MAC地址是数据发送之前,广播通知而得到的数据。而此时arp就是MAC和IP对应的表,交换机也可以返回一个MAC地址给广播,一步步的传输。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120052818.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120205926.png"></a>

负载均衡架设有基于网络设备的负载均和和基于操作系统的负载均衡,基于网络设备如Cisco路由或F5负载均衡器,基于操作系统的有windows的NBL和linux的LVS。

LVS-nat模式:(virtual server via network address translation)

通过网络地址转换,调度器将重写请求报文的目标地址,根据预设的调度算法,将请求分派给后端的真是服务器;真是服务器响应报文通过调度器时,报文的源地址将被重写,返回给客户,完成整个负载调度。

优点:节省IP资源,屏蔽内网ip,内外分离。

缺点:访问量大,容易造成VIP主机瓶颈(一般不会)。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120242962.png"></a>

IP tun模式:(virtual server via ip tunneling)

为了解决nat技术的调度器负担问题,调度器吧请求报文通过ip隧道转发给真实服务器,而真实服务器将相应直接返回给客户,所以调度器中处理请求报文。由于一般请求报文比应答报文大许多,此技术可以使集群系统的吞吐量提高10倍。

缺点:比较浪费公网ip。有点:由于下行速度大于上行速度,此技术速度快。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120302492.png"></a>

LVS-dr模式:(virtual server via direct routing)

此模式通过改写请求报文的MAC地址,将请求发到真实服务器,真实服务器直接传给客户。这种没有ip隧道模式的开销,具有更广泛的应用场合。但要求调度服务器和真实服务器都有一块网卡在同一网段。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120316870.png"></a>

轮叫:(round robin):

调度器通过“轮叫”调度算法,将外部请求按顺序轮流分配到集群中的每一台服务器。

加权轮叫:(weighted round robin)

加权轮叫算法,会根据真实服务器的不同处理能力来调度访问请求。这样可以保证处理能力强的服务器承受更多的访问流量,并可以动态调整权值。

最小连接:(least connections)

最少连接调度器可以动态的将网络请求调度到链接最少的服务器上,如果真实服务器的性能相近,看可以达到较好的目的。

加权最少链接:(weighted least connections)

在集群计算机性能差异较大时,采用加权最少链接可以让权值较高的服务器承受较大比例的请求,并且动态调整权值。

实验环境:

负载均衡服务器:真实IP 8.8.8.8      内网IP:192.168.10.1

真实服务器:server1:192.168.10.2

真实服务器:server2:192.168.10.3

 1、先做两个快照:

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120330364.png"></a>

负载均衡调度的服务器需要两块网卡,一块内网一块外网的,内网的网卡要和其他的两台真实服务器的网卡在同一个交换机上。如我们将内网都在VM2交换机上,将外网用vm1交换机。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120343695.png"></a>

2、配置IP

LVS服务器:我们使用临时ip测试

Ipconfig eth0 8.8.8.8 netmask 255.255.255.0

Ipconfig eth1 192.168.10.1 netmask 255.255.255.0

真实服务器: ipconfig eth0 192.168.10.2 netmask 255.255.255.0

和ipconfig eth0 192.168.10.3 netmask 255.255.255.0

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120402791.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120415912.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120445658.png"></a>

为了使我们配置的公网可以访问,我们要配置我们的本地ip和8.8.8.1在同一个网段,我们配置本地ip为8.8.8.1,掩码为255.255.255.0这样它们在同一个子网8.8.8.0了,这里我们选择的VMware1的网卡。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120500979.png"></a>

为了不影响测试,我们这里将防火墙和selinux停掉。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120513142.png"></a>

为了ping通网络,我们通过命令“echto 1&gt;/proc/sys/net/ipv4/ip_forward”开启路由转发功能,对于真实服务器我们还需要增加默认网关,通过命令“route add default gw 192.168.10.1”实现。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120529809.png"></a>

这样我们服务器之间便可以ping通了,只有环境正确才能达到目的。我们可以通过本地ping通lvs服务器,我们可以通过lvs服务器ping通内外网,因为它是内外网的使者。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120544580.png"></a>

3.安装ipvsadm插件,首先执行“ipvsadm”命令,看能否识别命令,如不识别用yum安装即可。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120604427.png"></a>

4、编写测试脚本,在LVS服务器上编写如下脚本,该脚本首先使用-C请求之前的脚本,然后定义提供服务器的ip,并使用rr轮训方式,然后指定真实服务器的ip,-m指使用nat模式。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120619649.png"></a>

测试脚本:OK

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120637906.png"></a>

5、定义服务:

真实服务器1和真实服务器2分别开启apache功能,并且分别定义主页。如果源码包安装的apache,可以通过/usr/local/apache/bin/apachectl start 开启,然后到/usr/local/apache/htdocs目录下新建index.html文件内容写“&lt;h1&gt;192.168.10.2,it works!&lt;h1&gt;”.

6、测试nat模式:

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120658748.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120710158.png"></a>

负载均衡服务器:隧道IP 200.168.10.10     内部IP:200.168.10.1

真实服务器:server1:200.168.10.2

真实服务器:server2:200.168.10.3

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120725801.png"></a>

1、配置ip

首先LVS服务器我们需要一块网卡就行了,我们使用eth0即可,听到eth1.其次我们需要增加我们的隧道ip,通过命令“ifconfig tunl0 eth0 200.168.10.10 netmask 255.255.255.255 up”实现,我们还需要将隧道ip加入路由,通过“route add -host 200.168.10.10 dev eth0”实现。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120741885.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120758929.png"></a>

对于真实ip我们也一样配置一个真实ip和一个隧道ip然后将隧道ip加入路由。这里我们要求我们的三台服务器使用同一个交换机vm1.

下图是增加了隧道ip的网卡配置和route效果。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120813640.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120826519.png"></a>

2.Ping测试,我们都是公网,我们本地ip也改为200.168.10.4子网掩码255.255.255.0这样都在一个网段,可以ping通所有的这网段的ip。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120838837.png"></a>

3.建立配置文件。我们首先个LVS服务器建立配置文件,我们这一次使用-i指定为隧道模式。然后增加执行权限,查看结果。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120852555.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/120906399.png"></a>

4.对于真实服务器我们需要建立如下脚本,然后执行。这是为了使如果请求直接访问了真实服务器,真实服务器会忽略请求。这样就保证了,每次请求都是LVS服务器发过来的。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122348756.png"></a>

5.测试:我们分别修改真实服务器的主文件,修改ip为现在的ip,然后浏览测试。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122421242.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122605695.png"></a>

测试之后,我们还可以返回LVS服务器查看轮询的情况和权值等信息。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122621989.png"></a>

负载均衡服务器:虚拟IP 200.168.10.10     内部IP:200.168.10.1

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122744566.png"></a>

LVS服务器,我们需要一快网卡,然后虚拟一个ip即可,我们可以使用命令“ifconfig eth0 200.168.10.10 netmask 255.255.255.255 up”。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122804419.png"></a>

对于真实服务器,我们要通过回环ip绑定这个ip,通过命令“ifconfig lo:0 200.168.10.10 netmask 255.255.255.255 up”实现,然后将该ip分别加入路由“route add -host 200.168.101.10 dev lo:0”实现。

2.配置配置文件,对于配置文件我们这次指定-g即dr模式来实现。然后赋予执行权限,执行。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122824129.png"></a>

对于真实服务器,我们同样要进行配置,让它忽略对于VIP的请求。

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122842802.png"></a>

3、测试:我们进行测试

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122902930.png"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201208/122955157.png"></a>

可以通过真实服务器执行“ipvsadm -L -n”进行查看轮训效果

<a target="_blank" href="http://blog.51cto.com/attachment/201208/123010431.png"></a>

 注:本章是进行LVS的基本使用在生产环境中:

1、ip的配置最好使用setup的图形界面或vi /etc/sysconfig/network-scripts/ifcfg-eth0.

2、对于作为提供真实服务的主机,要保证访问的网页一致。可以通过架设rsync同步服务器,或drbd镜像服务功能或共享磁盘(有DAS/NAS/SAN 等方式)的方式来进行同步真实服务器的数据,保证结果的一致性,这个作为下一步的研究目标。

本文转自zsaisai 51CTO博客,原文链接:http://blog.51cto.com/3402313/964038