RHEL6.5----LVS(NAT)

主机名 IP  所需软件 
master

192.168.30.130(Nat)

192.168.17.130(VMnet4)

ipvsadm 
node-1 192.168.17.131  httpd
node-2 192.168.17.132  httpd 

在master上安装ipvsadm

[root@master ~]# yum install -y ipvsadm #配置好本地YUM源即可
启动ipvsadm
[root@master ~]# /etc/init.d/ipvsadm start
ipvsadm: Clearing the current IPVS table:                  [  OK  ]
ipvsadm: Applying IPVS configuration:                      [  OK  ]

修sysctl.conf文件,实现路由转发

[root@master ~]# vim /etc/sysctl.conf 
..........
net.ipv4.ip_forward = 1
..........
使立即生效
[root@master ~]# sysctl -p
net.ipv4.ip_forward = 1
........

使用ipvsadm添加规则

[root@master ~]# ipvsadm -A -t 192.168.30.130:80 -s rr
参数说明
    -A 添加
    -t 表示 TCP 的服务   VIP:PORT
    -s 指定调度算法   rr 表示 round-robin 轮循
[root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.17.131 -m 
[root@master ~]# ipvsadm -a -t 192.168.30.130:80 -r 192.168.17.132 -m 
保存规则
[root@master ~]# /etc/init.d/ipvsadm save
查看规则
[root@master ~]# service ipvsadm status
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.30.130:80 rr
  -> 192.168.17.131:80            Masq    1      0          0         
  -> 192.168.17.132:80            Masq    1      0          0         

[root@master ~]# 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.30.130:80 rr
  -> 192.168.17.131:80            Masq    1      0          0         
  -> 192.168.17.132:80            Masq    1      0          0   

在node-1和node-2上

修改网关,创建测试页面并启动httpd服务

node-1和node-2上,IP设为192.168.17.131和192.168.17.132,GATEWAY设置为192.168.17.130。
安装httpd服务并创建测试页
[root@node-1 ~]# yum install -y httpd
[root@node-1 ~]# echo "<h1>This is the node-1 Web-Server:192.168.17.131</h1>" > /var/www/html/index.html
[root@node-2 ~]# echo "<h1>This is the node-2 Web-Server:192.168.17.132</h1>" > /var/www/html/index.html
[root@node-1 ~]# service httpd restart 
Stopping httpd:                                            [FAILED]
Starting httpd:                                            [  OK  ]

测试

在linux终端

[root@master ~]# elinks 192.168.30.130:80 --dump
                  This is the node-2 Web-Server:192.168.17.132
[root@master ~]# elinks 192.168.30.130:80 --dump
                  This is the node-1 Web-Server:192.168.17.131
[root@master ~]# elinks 192.168.30.130:80 --dump
                  This is the node-2 Web-Server:192.168.17.132
[root@master ~]# elinks 192.168.30.130:80 --dump
                  This is the node-1 Web-Server:192.168.17.131

物理机浏览器

查看详细信息

[root@master ~]# ipvsadm -L -n -c #查看客户端连接realserver的情况
IPVS connection entries
pro expire state       source             virtual            destination
TCP 01:56  TIME_WAIT   192.168.30.130:38229 192.168.30.130:80  192.168.17.131:80
TCP 01:50  TIME_WAIT   192.168.30.130:38223 192.168.30.130:80  192.168.17.131:80
TCP 01:47  TIME_WAIT   192.168.30.130:38222 192.168.30.130:80  192.168.17.132:80
TCP 01:53  TIME_WAIT   192.168.30.130:38225 192.168.30.130:80  192.168.17.131:80
TCP 01:55  TIME_WAIT   192.168.30.130:38228 192.168.30.130:80  192.168.17.132:80
TCP 01:54  TIME_WAIT   192.168.30.130:38226 192.168.30.130:80  192.168.17.132:80
TCP 01:52  TIME_WAIT   192.168.30.130:38224 192.168.30.130:80  192.168.17.132:80
TCP 01:54  TIME_WAIT   192.168.30.130:38227 192.168.30.130:80  192.168.17.131:80
[root@master ~]# ipvsadm -L -n --rate #查看链接速率
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port                 CPS    InPPS   OutPPS    InBPS   OutBPS
  -> RemoteAddress:Port
TCP  192.168.30.130:80                   0        0        0       22       31
  -> 192.168.17.131:80                   0        0        0       11       16
  -> 192.168.17.132:80                   0        0        0       10       15
清空内核虚拟服务表中所有记录
[root@master ~]# ipvsadm -C 
[root@master ~]# ipvsadm -L -n --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes
  -> RemoteAddress:Port
原文地址:https://www.cnblogs.com/zd520pyx1314/p/9116886.html