LVS NAT模型演示

LVS NAT模型演示

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的地址)

刷新

LVS NAT模型演示

默认每刷新一次,会出现不同的页面

因为我们此时的算法为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

(0)
sixijiesixijie
上一篇 2016-10-29 21:58
下一篇 2016-10-29 22:59

相关推荐

  • bash 基础特性1

    bash 基础特性 命令模式 systemctl set-default multi-user.target 图形模式 systemctl set-default graphical.target   # rmdir [OPTION]…DIRECTORY…-p:删除某录后,如果其父目录为空,则一并删除之-v:显示过程# mk…

    Linux干货 2017-06-01
  • bash中的变量

        对任何一门编程语言来说,最基础的部分就是变量。那什么事变量呢?变量就是把一个已知的可以变动的值,赋给一个固定名字的,用固定的名字代表这个可变动的值。在bash中也不例外,跟大多数编程语言一样,它也有一些自己的语法和规则 bash变量:     1.规则设置:变量和变量的值中间用=连接,=的两…

    Linux干货 2016-08-15
  • Linux下history命令的介绍

    Linux下history命令的介绍 一、用处 对于Linux而言,命令是人机交互的重要方式,而查看命令历史可以帮助我们了解系统的使用状态、增强安全性也可以调用历史达到快捷操作的目的。 二、命令行历史   执行过的命令会被存入缓存,当正常退出shell时,缓存中的命令会被写入文件中并保存在用户下的隐藏文件文件.bash_history中。当用户登陆…

    Linux干货 2016-07-29
  • 源码包编译安装MariaDB-10.1.22

    源码包编译安装MariaDB-10.1.22 1、获取源码包        mariadb-10.1.22.tar.gz 2、编译环境及依赖关系     yum groupinstall -y Development Tools yum -y install …

    Linux干货 2017-03-16
  • Shell脚本编程中作业

    一、作业: 1、1、编写服务脚本/root/bin/testsrv.sh,完成如下要求 (1) 脚本可接受参数:start, stop, restart, status (2) 如果参数非此四者之一,提示使用格式后报错退出 (3) 如是start:则创建/var/lock/subsys/SCRIPT_NAME, 并显示“启动成功” 考虑:如果事先已经启动过一…

    Linux干货 2016-08-24
  • 三剑客之一sed命令

    sed sed ‘2p’ /etc/passwd  打印paswwd的第二行 这样执行结果看能看出第二行显示两次 sed -n ‘2p’ /etc/passwd -n 执行后 会取出当前行显示. n是关闭打印  例如[root@cent7 ~]#ifconfig ens33|sed -n &…

    Linux干货 2017-08-13