实现LVS-NAT

来源:互联网 发布:《怪物》 韩国 知乎 编辑:程序博客网 时间:2024/06/06 16:38

author:JevonWei
版权声明:原创作品


实现NAT模式的轮询rr模式

网络拓扑图如下
image
网络环境

RS1 192.168.198.138RS2 192.168.198.120LVS:    DIP 192.168.198.128    vip 172.16.253.105路由R1:    172.16.253.166    192.168.80.128Client 192.168.80.129RS1,RS2的网关指向192.168.198.128,client的网关指向R1

VS

添加路由转发选项[root@VS ~]# vim /etc/sysctl.d/99-sysctl.conf      net.ipv4.ip_forward=1[root@VS ~]# sysctl -p /etc/sysctl.d/99-sysctl.conf \\刷新生效net.ipv4.ip_forward = 1 [root@VS ~]# route add  -net 192.168.80.0/24 gw 172.16.253.166[root@VS ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface0.0.0.0         192.168.198.1   0.0.0.0         UG    100    0        0 ens340.0.0.0         192.16.0.1      0.0.0.0         UG    101    0        0 ens33172.16.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33192.16.0.1      0.0.0.0         255.255.255.255 UH    100    0        0 ens33192.168.80.0    172.16.253.166  255.255.255.0   UG    0      0        0 ens33192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0192.168.198.0   0.0.0.0         255.255.255.0   U     100    0        0 ens34配置LVS的调度算法为rr轮询[root@VS ~]# yum -y install ipvsadm [root@VS ~]# ipvsadm -A -t 172.16.253.105:80 -s rr \\-t指定TCP协议,-s指定调度算法为轮询[root@VS ~]# ipvsadm -a -t 172.16.253.105:80 -r 192.168.198.138 -m \\添加192.168.198.138 RS1服务器到LVS调度,-m 为nat类型[root@VS ~]# ipvsadm -a -t 172.16.253.105:80 -r 192.168.198.120 -m [root@VS ~]# ipvsadm -Ln \\查看LVS调度信息IP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  172.16.253.105:80 rr  -> 192.168.198.120:80           Masq    1      0          0           -> 192.168.198.138:80           Masq    1      0          0[root@VS ~]# curl 192.168.198.120welcome to RS2[root@VS ~]# curl 192.168.198.138welcome to RS1修改LVS的调度模式为wrr[root@VS ~]# ipvsadm -E -t 172.16.253.105:80 -s wrr[root@VS ~]# ipvsadm -LnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  172.16.253.105:80 wrr  -> 192.168.198.120:80           Masq    1      0          0           -> 192.168.198.138:80           Masq    1      0          0[root@VS ~]# ipvsadm -e -t 172.16.253.105:80 -r 192.168.198.138 -m -w 3 \\修改192.168.198.138 RS1主机的权重为3,-w 指定权重,-m为nat算法,192.168.198.120权重仍为1[root@VS ~]# ipvsadm -LnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  172.16.253.105:80 wrr  -> 192.168.198.120:80           Masq    1      0          0           -> 192.168.198.138:80           Masq    3      0          0 

RS1

[root@RS1 ~]# yum -y install httpd[root@RS1 ~]# vim /var/www/html/index.html     welcome to RS1[root@RS1 ~]# service httpd start

RS2

[root@RS2 ~]# yum -y install httpd[root@RS2 ~]# vim /var/www/html/index.html     welcome to RS2[root@RS2 ~]# service httpd start

路由器R1

[root@R1 ~]# vim /etc/sysctl.d/99-sysctl.conf      net.ipv4.ip_forward=1[root@R1 ~]# sysctl -p /etc/sysctl.d/99-sysctl.conf net.ipv4.ip_forward = 1[root@R1 ~]# route add -net 192.168.80.0/24 gw 192.168.80.129[root@R1 ~]# route add -net 172.16.0.0/16 gw 172.16.253.105 [root@R1 ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface192.168.80.0    192.168.80.129  255.255.255.0   UG    0      0        0 eth1192.168.80.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1172.16.0.0      172.16.253.105  255.255.0.0     UG    0      0        0 eth0172.16.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth00.0.0.0         172.16.0.1      0.0.0.0         UG    0      0        0 eth0

client

[root@client ~]# route add -net 172.16.0.0/16 gw 192.168.80.128访问rr轮询算法[root@client ~]# for i in {1..10};do curl 172.16.253.105;sleep 1;done welcome to RS2welcome to RS1welcome to RS2welcome to RS1welcome to RS2访问wrr权重算法[root@client ~]# for i in {1..10};do curl 172.16.253.105;sleep 1;donewelcome to RS2welcome to RS1welcome to RS1welcome to RS1welcome to RS2welcome to RS1welcome to RS1welcome to RS1welcome to RS2welcome to RS1

查看LVS的信息

[root@VS ~]# ipvsadm -Ln --statsIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port               Conns   InPkts  OutPkts  InBytes OutBytes  -> RemoteAddress:PortTCP  172.16.253.105:80                  29      158      139    10710    15609  -> 192.168.198.120:80                 15       82       69     5554     7923  -> 192.168.198.138:80                 14       76       70     5156     7686[root@VS ~]# ipvsadm -Ln --connection \\查看网络连接数[root@VS ~]# cut -d " " -f1 /var/log/httpd/access_log | sort -nr | uniq -c| sort -n \\查看网络连接
原创粉丝点击