首页 > 其他 > 详细

LVS的NAT模式

时间:2017-01-12 16:29:31      阅读:249      评论:0      收藏:0      [点我收藏+]

NAT模式实践

nat模式是通过改变ip地址进行请求的转发,具体通过改变目的ip的IP地址进行转发请求,当最后返回给客户端的时候,改变原ip地址


原理

实质上用的是使用iptables规则,需要依赖防火墙。

技术分享

Director Server前端分发、调度服务器,需要有两块网卡(公网/私网)

Real Server属于后端的真正服务器,只有私网

用户首先请求DS,再有DS分发给RS,RS接受到请求后响应请求将数据给DS,由DS将数据给用户;

优缺点

优点:将所有web server隐藏,只需要暴露一台机器即可;

缺点:网络吞吐量很大,Director Server这台机器的网卡负载高,交换机也会成为瓶颈,还需要添加iptables规则,依赖于内核与防火墙;

配置:

环境准备:

    三台机器:

              DR两块网卡 (外网桥接模式   eth1【192.168.70.129】,内网NAT模式静态eth0【192.168.79.172】)

              RS1一块网卡(内网NAT模式静态eth0【192.168.79.173】)

              RS2一块网卡(内网NAT模式静态eth0【192.168.79.174】)

             :NAT模式静态的网关是192.168.79.2

1、修改hostname

[root@insist ~]# hostname dr

[root@insist ~]# bash

[root@insist ~]# hostname rs1

[root@insist ~]# bash

 

[root@insist ~]# hostname rs2

[root@insist ~]# bash

2、在dr上安装ipvsadm用来实现分发

[root@dr ~]# yum install -y ipvsadm

3、两台real server上都安装 nginx(前提安装epel扩展源yum install -y epel-release)

[root@rs1 ~]# yum install -y nginx

[root@rs2 ~]# yum install -y nginx

4、在Director 上编辑一下脚本

[root@dr ~]# vim /usr/local/sbin/lvs_nat.sh

#! /bin/bash

# director 服务器上开启路由转发功能

echo 1 > /proc/sys/net/ipv4/ip_forward

# 关闭icmp的重定向

echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects

echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects

echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects

echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects

 

# director设置nat防火墙

iptables -t nat -F

iptables -t nat -X

iptables -t nat -A POSTROUTING -s 192.168.79.0/24 -j MASQUERADE

# director设置ipvsadm

IPVSADM=‘/sbin/ipvsadm‘

$IPVSADM -C

$IPVSADM -A -t 192.168.70.129:80 -s rr

$IPVSADM -a -t 192.168.70.129:80 -r 192.168.79.173:80 -m       

$IPVSADM -a -t 192.168.70.129:80 -r 192.168.79.174:80 -m

解释说明:

iptables -t nat -F 清空防火墙规则

iptables -t nat -X 删除链

iptables -t nat -A 针对nat表重新制定规则,伪装来源ip

/sbin/ipvsadm

-C 清空规则

-A 添加一个Director

-a 添加一个Real Server

-t <VIP>:<PORT>

-r <RIP>:<PORT>

-s 分发算法

-p 长连接单位秒数内始终分配同一台机器

-m 使用nat(-j MASQUERADE)

-w 设置权重(0-100)

5、执行脚本

[root@dr ~]# sh /usr/local/sbin/lvs_nat.sh

 

6、查看iptables规则

[root@dr ~]# iptables -nvL -t nat

 

Chain PREROUTING (policy ACCEPT 6 packets, 599 bytes)

 pkts bytes target     prot opt in     out     source               destination         

 

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination         

    0     0 MASQUERADE  all  --  *      *       192.168.79.0/24      0.0.0.0/0           

 

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

 pkts bytes target     prot opt in     out     source               destination

7、查看ipvsadm的规则

[root@dr ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.70.129:80 rr

  -> 192.168.79.173:80            Masq    1      0          0         

  -> 192.168.79.174:80            Masq    1      0          0

8、配置网关(把dr内网ip作为两个rs的网关)

[root@rs1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

HWADDR=00:0C:29:F4:DE:E7

TYPE=Etherne

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.70.130

NETMASK=255.255.255.0

GATEWAY=192.168.79.172

[root@rs1 ~]# ifdown eth0 ;ifup eth0

 

[root@rs2~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

DEVICE=eth0

HWADDR=00:0C:29:D7:7A:0E

TYPE=Ethernet

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.70.195

NETMASK=255.255.255.0

GATEWAY=192.168.79.172

[root@rs2 ~]# ifdown eth0;ifup eth0

解释说明:

这里使用ifdown eth0 && ifup eth0实际上就是重启网卡,为了防止有多块网卡启动

9、查看网关

[root@rs1 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.70.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

192.168.79.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         192.168.79.172  0.0.0.0         UG    0      0        0 eth1

 

[root@rs2 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.70.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1

192.168.79.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         192.168.79.172  0.0.0.0         UG    0      0        0 eth1

10、把两个rs服务器的80端口打开

[root@rs1 ~]# /etc/init.d/nginx start

[root@rs2 ~]# /etc/init.d/nginx start

11、 为了方便区分,把两个rs的nginx的默认访问页修改一下

[root@rs1 ~]# echo "111111111111" > /usr/share/nginx/html/index.html

[root@rs1 ~]# curl localhost                                         

111111111111

 

[root@rs2 ~]# echo "222222222222222" > /usr/share/nginx/html/index.html

[root@rs2 ~]# curl localhost

222222222222222

12、测试

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

222222222222222

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

222222222222222

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

222222222222222

13、修改权重

技术分享

14、测试

[root@dr ~]# sh /usr/local/sbin/lvs_nat.sh

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

222222222222222

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

111111111111

[root@dr ~]# curl 192.168.70.129

222222222222222

解释说明:

2次”111111111111111”

1次”222222222222222”


本文出自 “linux” 博客,转载请与作者联系!

LVS的NAT模式

原文:http://warm51fun.blog.51cto.com/3884274/1891293

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!