LVS_NAT 实验
1、安装内核源代码(ipvsadm时需要)# cd /mnt/cdrom/Packages/
# rpm -ivh kernel-devel-2.6.32-279.el6.x86_64.rpm
需要给源代码做一个软连接,不然编译ipvsadm的时候报错
# ln -s /usr/src/kernels/2.6.32-279.el6.x86_64/ /usr/src/linux
2、安装ipvsadm
# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
安装的时候有很多报错:
http://pmghong.blog.运维网.com/attachment/201305/183301644.png
解决办法:
# yum install popt popt-devel
# yum install libnl libnl-devel
# rpm -ivh popt-static-1.13-7.el6.x86_64.rpm
安装完重新编译安装,便可顺利装上
安装完成后运行一下ipvsadm 命令,然后执行lsmod|grep ip_vs 命令,若能显示如下,说明内核能支持:
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
# lsmod|grep ip_vs
ip_vs 1154900
libcrc32c 12461 ip_vs
ipv6 322541149 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
3、IP地址的分配
客户机 192.168.30.1
Director 192.168.30.105 (eth0)
10.0.0.101 (eth1)VIP
Real1 192.168.30.113
Real2 192.168.30.114
4、为Real server 配置主页服务
为每台real server安装配置好apache,并在每台服务器的/var/www/html 下简历index.html 文件。
为了看出效果,最好让每台服务器的Index.html文件都不一样,下面以Real1上的index.html 配置为例:
# service iptables stop
# setenforce 0
# yum install httpd
# vim /var/www/html/index.html
I'm Real Server 1.
IP:192.168.30.113
# service httpd start
Starting httpd:
在Director 上最好也建立个apache 服务,写个与Real Server 不一样的index.html ,例如:
http://pmghong.blog.运维网.com/attachment/201305/183333598.png
5、Director 上的配置
(1)开启转发
# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
# sysctl -p
(2)添加虚拟服务
# ipvsadm -A -t 10.0.0.101:80 -s rr
(3)添加真实服务器
# ipvsadm -a -t 10.0.0.101:80 -r 192.168.30.113 -m
# ipvsadm -a -t 10.0.0.101:80 -r 192.168.30.114 -m
以NAT的方式,添加指向各真实服务器
6、在Real Server 上指定网关地址
【Node1】
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.30.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 192.168.30.1050.0.0.0 UG 0 0 0 eth0
【Node2】
# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.30.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 192.168.30.1050.0.0.0 UG 0 0 0 eth0
注意:Real Server 必须要将网关地址指定到Director 上
RealServer 上面不能有Director 上的公网IP ,在本例中为10.0.0.0网段地址,否则访问不到页面。
7、测试
打开浏览器,访问虚拟IP,按F5 不断刷新,可以看到不同Real Server 的内容:
http://pmghong.blog.运维网.com/attachment/201305/183349861.png
http://pmghong.blog.运维网.com/attachment/201305/183403856.png
也可以通过命令来查看调度的情况:
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP10.0.0.101:80 rr
-> 192.168.30.113:80 Masq 1 0 49
-> 192.168.30.114:80 Masq 1 0 49
从上面结果可以看到rr 模式是1:1 调度的
>>下面再来试试wrr(加权轮询算法)
上面的轮询算法,每个real server 服务器被调用的机会都是均等的,如果Real 1 和 Real 2 服务器的处理性能要远比Real 3 和 Real 4 都强,用rr算法就不是很合理了,因为rr算法不好考虑权重(也就是优先级)。这样可能会造成一个问题:
对于同样的请求过来,R1,R2性能较好,能很好的处理这些请求;而对于性能较差的R3、R4 来说, 同样的请求可能会造成机器相应不过来,不能很好的分配好请求。
相比于轮询算法,加权轮询算法会设置一个权重值,这个权重值决定了在负载均衡的时候被轮询到的几率大小。在rr 算法下,即使设置了weight值,也不会起作用。
由于这个实验是接着上面继续做的,所以有2种做法:
1. 通过ipvsadm -C 清除所有配置,重新配置
2. 通过ipvsadm -E 修改原先的配置
# ipvsadm -E -t 10.0.0.101:80 -s wrr // 将rr 算法修改为wrr 算法
# ipvsadm -e -t 10.0.0.101:80 -r 192.168.30.114 -m -w 5 //加大real server2的权重
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP10.0.0.101:80 wrr
-> 192.168.30.113:80 Masq 1 0 0
-> 192.168.30.114:80 Masq 5 0 0
权重值范围为0-65535 之间,默认为1,值越高,优先级就越高。如果为0,表示永远不会被选中(在处理真实服务器故障或维护时很有用),如果值为65535 表示永远只选择这台服务器
改完后,在客户端通过F5 刷新浏览器,在Director 上可以看到,R2 被选中的几率是R1 的5倍。
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP10.0.0.101:80 wrr
-> 192.168.30.113:80 Masq 1 0 7
-> 192.168.30.114:80 Masq 5 0 35
注意:当用rr算法的时候,即使设置了权重值也没有作用,只有用到了wrr算法时,权重值才会发挥作用。
ipvsadm -a 命令加入真实条目的先后顺序,并不决定real server的优先级
>>下面再来试试lc和wlc(最少连接算法和加权最少连接算法)
lc(最少连接算法)会检查哪台Real Server 的连接请求最少,然后优先选择它。所以当服务器池中的服务器硬件配置相同时,用Lc算法,是个不错的选择。
wlc 是加权最少连接算法,其原理跟lc相似,只是多了权重值这个条件。
# ipvsadm -E -t 10.0.0.101:80 -s wlc
# ipvsadm -e -t 10.0.0.101:80 -r 192.168.30.113 -m -w 1
# ipvsadm -e -t 10.0.0.101:80 -r 192.168.30.114 -m -w 5
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP10.0.0.101:80 wlc
-> 192.168.30.113:80 Masq 1 0 0
-> 192.168.30.114:80 Masq 5 0 0
测试:
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP10.0.0.101:80 wlc
-> 192.168.30.113:80 Masq 1 0 12
-> 192.168.30.114:80 Masq 5 0 60
小结
NAT方式的原理比较好理解,配置也比较简单
Real Server 达到20台以上时,Director 将会成为瓶颈
所需软件下载:http://down.运维网.com/data/776254
页:
[1]