LVS NAT模型演示
这里我们刻意将两个RS的web页面文件,提供为不同的内容(实际应用中,每个RS页面的内容要完全相同)
基本配置
[root@web1 ~]# route add default gw 192.168.110.130 --> web1服务器中,默认网关要指向DIP [root@web2 ~]# route add default gw 192.168.110.130 --> web2服务器中,默认网关要指向DIP [root@Directory ~]# vim /etc/sysctl.conf net.ipv4.ip_forward = 1 //开启路由转发功能 [root@Directory ~]# sysctl -p //以上修改立即生效
directory配置
[root@Directory ~]# ipvsadm -A -t 10.1.249.43:80:80 -s rr -->添加一个集群服务,调度算法为rr [root@Directory ~]# ipvsadm -L -n -->查看添加的集群服务 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 rr --> 成功添加 调度算法为rr [root@Directory ~]# ipvsadm -a -t 10.1.249.43:80 -r 192.168.110.129 -m -->向集群中添加RS [root@Directory ~]# ipvsadm -a -t 10.1.249.43:80 -r 192.168.110.128 -m -->将web1和web2添加到集群中 [root@Directory ~]# ipvsadm -L -n IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 rr -> 192.168.110.128:80 Masq 1 0 0 -->成功添加 -> 192.168.110.129:80 Masq 1 0 0 --> 权重默认为1 masq-->masquerade-->地址伪装
测试:浏览器内输入10.1.249.43(VIP的地址)
刷新
默认每刷新一次,会出现不同的页面
因为我们此时的算法为rr 即在web1和web2之间轮询
命令查看状态
[root@Directory ~]# ipvsadm -L -nc TCP 00:17 TIME_WAIT 10.1.250.160:55181 10.1.249.43:80 192.168.110.128:80 -->明显看出在轮询 TCP 01:55 TIME_WAIT 10.1.250.160:55253 10.1.249.43:80 192.168.110.129:80 TCP 01:55 TIME_WAIT 10.1.250.160:55254 10.1.249.43:80 192.168.110.128:80 TCP 01:55 TIME_WAIT 10.1.250.160:55251 10.1.249.43:80 192.168.110.129:80 ... [root@Directory ~]# ipvsadm -L -n --stats IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 18 88 87 12095 8353 -> 192.168.110.128:80 9 43 42 5768 4048 --> 匹配到的报文数几乎一样 -> 192.168.110.129:80 9 45 45 6327 4305
修改调度算法为wrr测试
[root@Directory ~]# ipvsadm -Z 统计数据清零 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 0 0 0 0 0 --> 全部清零 -> 192.168.110.128:80 0 0 0 0 0 -> 192.168.110.129:80 0 0 0 0 0 [root@Directory ~]# ipvsadm -E -t 10.1.249.43:80 -s wrr --> 修改调度算法 [root@Directory ~]# ipvsadm -e -t 10.1.249.43:80 -r 192.168.110.129 -m -w 3 --> 将web1的权重修改为3 [root@Directory ~]# ipvsadm -L -n -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.1.249.43:80 wrr -> 192.168.110.128:80 Masq 1 0 1 -> 192.168.110.129:80 Masq 3 0 0 --> 此时web1的权重为3
测试访问 一定要访问VIP的地址 此时,平均出现3次web1会出现1次web2
统计数据查看
[root@Directory ~]# ipvsadm -L -n --stats IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 10.1.249.43:80 24 120 117 16393 9859 -> 192.168.110.128:80 6 30 30 4218 2958 -> 192.168.110.129:80 18 90 87 12175 6901 --> 数据几乎为上面的3倍
查看访问日志
此时查看web1或者web2中httpd的访问日志时,一定是CIP [root@web2 ~]# tail /var/log/httpd/access_log --> 查看web2的httpd的访问日志 10.1.250.160 - - [03/Sep/2016:15:23:43 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" 10.1.250.160 - - [03/Sep/2016:15:23:43 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" 10.1.250.160 - - [03/Sep/2016:15:23:44 +0800] "GET / HTTP/1.1" 200 14 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36" === 10.1.250.160为我们测试访问的客户端地址 ===
原创文章,作者:sixijie,如若转载,请注明出处:http://www.178linux.com/55735