您的位置:首页 > 其它

负载均衡之LVS-NAT LVS-DR模型详解

2016-05-31 12:04 861 查看
LVS是Linux Virtual Server的缩写,是一个虚拟的服务器集群系统,能够定义一个或多个后端服务器。工作在INPUT上的内核中 ipvs。

LVS的调度方法:10种
静态方法:仅根据算法本身进行调度
rr: Round Robin   轮询
wrr: Weighted RR  权重轮询
sh: source hashing   根据 ip   反均衡  支持session绑定
dh: destination hashing   根据防火墙 设置时候 用到不多
动态方法:根据算法及RS当前的负载状况
真处在数据链接的叫活动状态   数据链接结束但为断开叫非活动链接
lc: Least Connection  #最少连接
Overhead=Active*256+Inactive   #开始时候是轮询   结果中,最小者胜出;
wlc: Weighted LC
Overhead=(Active*256+Inactive)/weight    #结果中,最小者胜出;
sed: Shortest Expect Delay   #wlc的升级 最小期望延迟
Overhead=(Active+1)*256/weight
nq: Nerver Queue  #改进的sed 上来先轮一篇  不过是根据sed来轮询
lblc: Locality-based Least Connection  基于本地最少连接
dh+lc   用于session绑定  用的不多
lblcr: Replicated and Locality-based Least Connection  用的不多  后端是缓存服务器
lvs-nat




LVS NAT的特性:
1、RS应该使用私有地址;
2、RS的网关的必须指向DIP;
3、RIP和DIP必须在同一网段内;
4、请求和响应的报文都得经过Director;在高负载场景中,Director很可能成为系统性能瓶颈;
5、支持端口映射;
6、RS可以使用任意支持集群服务的OS;
安装ipvsadm
[root@marvin /]# yum install ipvsadm -y
vip:192.168.1.119 dip:192.168.243.132 虚拟网卡:主机模式rip1:192.168.243.129 虚拟网卡:主机模式 http服务 测试页面demo.html页面内容martinrip2:192.168.243.131 虚拟网卡:主机模式 http服务 测试页面demo.html页面内容luciahttp:这里不做安装配置说明添加vip:
[root@marvin /]# ifconfig eth1:0 192.168.1.119/24 up
配置路由转发:
[root@marvin nat]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

[root@marvin nat]# sysctl -p
real-server路由指向dip:
[root@martin /]# route add default gw 192.168.243.132
[root@lucia /]# route add default gw 192.168.243.132
添加集群服务:
#添加集群服务器
[root@marvin /]# ipvsadm -A -t 192.168.1.119:80 -s rr
#添加后端服务器
[root@marvin /]# ipvsadm -a -t 192.168.1.119:80 -r 192.168.243.129 -m
[root@marvin /]# ipvsadm -a -t 192.168.1.119:80 -r 192.168.243.131 -m
#数据查看
[root@marvin /]# ipvsadm -L -n               # --rate    --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.119:80 rr
-> 192.168.243.129:80           Masq    1      0          0
-> 192.168.243.131:80           Masq    1      0          0



测试结果1:1
[root@marvin /]# ipvsadm -L -n --rate --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
-> RemoteAddress:Port
TCP  192.168.1.119:80                  104     1090      507   154977   109894
-> 192.168.243.129:80                 52      649      304   101371    75352
-> 192.168.243.131:80                 52      441      203    53606    34542
修改:
[root@marvin /]# ipvsadm -E -t 192.168.1.119:80 -s wrr
[root@marvin /]# ipvsadm -e -t 192.168.1.119:80 -r 192.168.243.131 -m -w 3
清空计数器:

[root@marvin /]# ipvsadm -Z  -t 192.168.1.119:80
测试结果3:1
[root@marvin /]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.119:80 wrr
-> 192.168.243.129:80           Masq    1      0          5
-> 192.168.243.131:80           Masq    3      1          16
数据保存:
[root@marvin lvs-date]# ipvsadm -S > /mydata/lvs-date/nat/wrr.conf
数据集群服务:
[root@marvin nat]# ipvsadm -D -t 192.168.1.119:80
[root@marvin nat]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
还原集群服务:
[root@marvin nat]# ipvsadm -R </mydata/lvs-date/nat/wrr.conf
[root@marvin nat]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.119:80 wrr
-> 192.168.243.129:80           Masq    1      0          0
-> 192.168.243.131:80           Masq    3      0          0
查看映射表:
[root@marvin /]# ipvsadm -L -c
IPVS connection entries
pro expire state       source             virtual            destination
TCP 00:46  TIME_WAIT   192.168.1.102:56168 192.168.1.119:http 192.168.243.131:http
TCP 00:45  TIME_WAIT   192.168.1.102:56161 192.168.1.119:http 192.168.243.129:http
TCP 00:46  TIME_WAIT   192.168.1.102:56163 192.168.1.119:http 192.168.243.131:http
TCP 00:46  TIME_WAIT   192.168.1.102:56164 192.168.1.119:http 192.168.243.131:http
TCP 00:44  TIME_WAIT   192.168.1.102:56154 192.168.1.119:http 192.168.243.131:http
TCP 00:45  TIME_WAIT   192.168.1.102:56162 192.168.1.119:http 192.168.243.131:http
TCP 00:52  TIME_WAIT   192.168.1.102:56172 192.168.1.119:http 192.168.243.131:http
TCP 00:46  TIME_WAIT   192.168.1.102:56165 192.168.1.119:http 192.168.243.129:http
TCP 00:44  TIME_WAIT   192.168.1.102:56153 192.168.1.119:http 192.168.243.129:http
TCP 00:47  TIME_WAIT   192.168.1.102:56171 192.168.1.119:http 192.168.243.131:http
TCP 00:44  TIME_WAIT   192.168.1.102:56155 192.168.1.119:http 192.168.243.131:http
TCP 00:46  TIME_WAIT   192.168.1.102:56167 192.168.1.119:http 192.168.243.131:http
TCP 00:46  TIME_WAIT   192.168.1.102:56169 192.168.1.119:http 192.168.243.129:http
TCP 00:47  TIME_WAIT   192.168.1.102:56170 192.168.1.119:http 192.168.243.131:http
TCP 00:46  TIME_WAIT   192.168.1.102:56166 192.168.1.119:http 192.168.243.131:http
TCP 00:45  TIME_WAIT   192.168.1.102:56159 192.168.1.119:http 192.168.243.131:http
TCP 00:44  TIME_WAIT   192.168.1.102:56156 192.168.1.119:http 192.168.243.131:http
TCP 00:44  TIME_WAIT   192.168.1.102:56152 192.168.1.119:http 192.168.243.131:http
TCP 00:45  TIME_WAIT   192.168.1.102:56157 192.168.1.119:http 192.168.243.129:http
TCP 00:45  TIME_WAIT   192.168.1.102:56160 192.168.1.119:http 192.168.243.131:http
TCP 00:45  TIME_WAIT   192.168.1.102:56158 192.168.1.119:http 192.168.243.131:http
TCP 00:44  TIME_WAIT   192.168.1.102:56149 192.168.1.119:http 192.168.243.131:http

lvs-dr:



LVS DR类型:
1、让前端路由将请求发往VIP时,只能是Dirctor上的VIP;
解决方案:
(1) 静态地址绑定;
未必有路由器的配置权限;
Director调用时静态地址绑定将难以适用;
(2) arptables
(3) 修改Linux内核参数,将RS上的VIP配置为lo接口的别名,限制Linux仅对对应接口的ARP请求做响应;
LVS DR类型的特性:
1、RS可以使用私有地址;但也可以使用公网地址,此时可以直接通过互联网连入RS以实现配置、监控等;
2、RS的网关一定不能指向DIP;
3、RS跟Dirctory要在同一物理网络内(不能由路由器分隔);
4、请求报文经过Directory,但响应报文一定不经过Director
5、不支持端口映射;
6、RS可以使用大多数的操作系统;
vip:192.168.1.119 #dip: 虚拟网卡:主机模式rip1:192.168.1.122 虚拟网卡:主机模式 http服务 测试页面demo.html页面内容martinrip2:192.168.1.123 虚拟网卡:主机模式 http服务 测试页面demo.html页面内容luciahttp:这里不做安装配置说明简单说明:这里的dip=vip vip是一般都是公网 假如要设置不同网段的dip,即dip可以是私网 只要rip与dip能通信 rip的网络能流向cip即可。 real-server:
[root@martin ~]# echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@martin ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@martin ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@martin ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

[root@lucia ~]#  echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
[root@lucia ~]# echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
[root@lucia ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@lucia ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

arp_ignore    被动模式   # 是否响应ARP地址请求,默认0。
0     # 回复本机的所有ip地址(不管地址在哪里)。
1     # 仅在请求的目标地址配置请求到达的接口上的时候,才给予响应
arp_announce  # 限制本机如何通告本地地址,默认0。 通告级别:主动模式
0     # 通高所有本机ip地址。
1     #试图仅向目标网络通告与其网络匹配的地址
2     # 仅对相同网络段地址进行通告。 通告rip  mac  隐藏real-server上的vip  mac
添加vip:(广播域设为本机)
[root@martin ~]# ip addr add dev lo 192.168.1.119/32 broadcast 192.168.1.119 label lo:0

[root@lucia ~]# ip addr add dev lo 192.168.1.119/32 broadcast 192.168.1.119 label lo:0
添加集群服务

[root@marvin nat]# ipvsadm -A -t 192.168.1.119:80 -s rr   #时间[-p 300]
[root@marvin nat]# ipvsadm -a -t 192.168.1.119:80 -r 192.168.1.222 -g
[root@marvin nat]# ipvsadm -a -t 192.168.1.119:80 -r 192.168.1.223 -g



[root@marvin nat]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.119:80 rr
-> 192.168.1.222:80             Route   1      0          5
-> 192.168.1.223:80             Route   1      0          5
[root@marvin nat]# ipvsadm -E -t 192.168.1.119:80 -s wrr
[root@marvin nat]# ipvsadm -e -t 192.168.1.119:80 -r 192.168.1.223 -g -w 4
[root@marvin nat]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.119:80 wrr
-> 192.168.1.222:80             Route   1      0          6
-> 192.168.1.223:80             Route   4      0          24
抓包:数据从lo上出去
[root@lucia ~]# tcpdump -i any  -nn -XX tcp src port 80 and ip src 192.168.1.119

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
19:14:03.630672 IP 192.168.1.119.80 > 192.168.1.102.58761: Flags [S.], seq 4229184048, ack 1320608146, win 14480, options [mss 1460,sackOK,TS val 418789887 ecr 177960826,nop,wscale 6], length 0
0x0000:  0004 0001 0006 000c 2960 a9fd 0000 0800  ........)`......
0x0010:  4500 003c 0000 4000 4006 b68e c0a8 0177  E..<..@.@......w
0x0020:  c0a8 0166 0050 e589 fc14 3a30 4eb6 e192  ...f.P....:0N...
0x0030:  a012 3890 69c0 0000 0204 05b4 0402 080a  ..8.i...........
0x0040:  18f6 39ff 0a9b 777a 0103 0306            ..9...wz....
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: