LVS NAT模型实践
# LVS NAT模型概念
通过修改请求报文的目标IP
地址,而后根据调度算法挑选出一台RS
节点进行转发。(请求进入负载均衡器LVS
时做DNAT
,后端返回数据出负载均衡时做SNAT
)
# NAT 基础图解
# NAT 底层实现(New IP)
- 客户端:
10.0.0.1(外网)
- DS:
172.16.1.100(VIP)
、172.16.1.3(DIP)
- RS:
172.16.1.7
、172.16.1.8
、Gateway:172.16.1.3
# NAT 访问原理
- 当用户请求到达
DS
,此时请求的数据报文会先到内核空间的PREROUTING
链。此时报文的源IP为CIP
,目标IP为VIP
; PREROUTING
检查发现数据包的目标IP
是本机,将数据包送至INPUT
链;IPVS
比对数据包请求的服务是否为集群服务,若是,通过调度算法挑选一台后端RS
服务器,并修改数据包的目标IP
为RS的IP
,然后将数据包发至POSTROUTING
链。此时报文的源IP为CIP
,目标IP为RIP
;POSTROUTING
链通过选路,将数据包通过DS
的DIP
发送给RS
;RS
发现目标为自己的IP
,则交给应用程序处理,然后构建响应报文发回给DS
。此时报文的源IP为RIP
,目标IP为CIP
;DS
在响应客户端前,会将源IP
地址修改为VIP
地址,然后响应给客户端。此时报文的源IP为VIP
,目标IP为CIP
;
# NAT 特性
RS
必须使用私有地址,并需要将网关指向DS
;RIP
和DIP
必须为同一网段内;NAT
模型支持端口映射;RS
可以使用任意操作系统。例如Linux、Windows
等;- 请求和响应报文都要经过
DS
,高负载场景中,DS
易称为瓶颈;
# LVS NAT模型实战
# NAT 架构规划
# NAT Route配置
将
Linux
服务器配置为路由器,先配置其IP
地址;eth0
配置信息如下
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=10.0.0.200 PREFIX=24 GATEWAY=10.0.0.2 # 指向能出公网的IP DNS1=223.5.5.5
1
2
3
4
5
6
7
8
9
10
11eth1
配置信息如下
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth1 DEVICE=eth1 ONBOOT=yes IPADDR=172.16.1.200 PREFIX=24
1
2
3
4
5
6
7
8
9在
Route
节点启用FORWARD
转发功能,实现路由功能;[root@route ~]# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf [root@route ~]# sysctl -p
1
2
# NAT RS配置
配置
RS
节点eth1
网卡为LAN
模式,然后将网关统一指向DS
服务器;(所有RS
节点都需要操作)[root@rs01 ~]# ifdown eth0 # 关闭eth0网关,真实生产环境也仅有一块网卡 [root@rs01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth1 DEVICE=eth1 ONBOOT=yes IPADDR=172.16.1.7 # 不同的 RS 节点地址不一样 GATEWAY=172.16.1.3 # 所有的 RS 节点网关都指向 DS 节点的 DIP PREFIX=24 DNS=223.5.5.5
1
2
3
4
5
6
7
8
9
10
11
12重启
eth1
网卡,使其生效;[root@rs01 ~]# ifdown eth1 8& ifup eth1
1检查
RS
节点路由信息;[root@rs01 ~]# route -n Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.16.1.3 0.0.0.0 UG 100 0 0 eth1 172.16.1.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1
1
2
3
4配置后端所有
RS
的web
服务,注意RS1
和RS2
页面不一样,方便验证效果;rs1
节点配置
[root@rs01 ~]# yum install nginx -y [root@rs01 ~]# cat /etc/nginx/conf.d/lvs.birenchong.cn.conf server { listen 80; server_name lvs.birenchong.cn; root /opt; location / { index index.html; } } [root@rs01 ~]# echo "Web Page RS-Node1" > /opt/index.html [root@rs01 ~]# systemctl start nginx # 本机测试访问 [root@rs01 ~]# curl -HHost:lvs.birenchong.cn http://172.16.1.7 Web Page RS-Node1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17rs2
节点配置
[root@rs02 ~]# yum install nginx -y [root@rs02 ~]# cat /etc/nginx/conf.d/lvs.birenchong.cn.conf server { listen 80; server_name lvs.birenchong.cn; root /opt; location / { index index.html; } } [root@rs02 ~]# echo "Web Page RS-Node2" /opt/index.html [root@rs02 ~]# systemctl start nginx # 本机测试访问 [root@rs02 ~]# curl -HHost:lvs.birenchong.cn http://172.16.1.8 Web Page RS-Node2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NAT DS配置
编辑网卡配置,将
DS
节点的eth1
网关指向路由节点;[root@lb01 ~]# ifdown eth0 [root@lb01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth1 DEVICE=eth1 ONBOOT=yes IPADDR=172.16.1.3 PREFIX=24 GATEWAY=172.16.1.200 DNS1=223.5.5.5
1
2
3
4
5
6
7
8
9
10
11
12新增
VIP
地址的网卡配置,将VIP
绑定到eth1:1
网卡上;[root@lb01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1:1 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth1:1 DEVICE=eth1:1 ONBOOT=yes IPADDR=172.16.1.100 PREFIX=24
1
2
3
4
5
6
7
8
9重启
DS
节点eth1、eth1:1
的网卡;[root@lb01 ~]# ifdown eth1 && ifup eth1 [root@lb01 ~]# ifdown eth1:1 && ifup eth1:1
1
2开启
DS
节点的内核转发功能,不然RS
节点发送的数据包会被丢弃;[root@lb01 ~]# echo "net.ipv4.ip_forward = 1" >>/etc/sysctl.conf [root@lb01 ~]# sysctl -p
1
2DS
节点负载均衡配置;# 定义LVS集群 [root@lb01 ~]# ipvsadm -A -t 172.16.1.100:80 -s rr # 添加RS1、RS2集群节点 [root@lb01 ~]# ipvsadm -a -t 172.16.1.100:80 -r 172.16.1.7:80 -m [root@lb01 ~]# ipvsadm -a -t 172.16.1.100:80 -r 172.16.1.8:80 -m # 查看集群状态信息 [root@lb01 ~]# ipvsadm -L -n IP virtual server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward weight ActiveConn InActConn TCP 172.16.1.100:80 rr -> 172.16.1.7:80 Masq 1 0 0 -> 172.16.1.8:80 Masq 1 0 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# NAT Client测试
配置
Client
节点eth0
网络;[root@client ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes NAME=eth0 DEVICE=eth0 ONBOOT=yes IPADDR=10.0.0.100 PREFIX=24 GATEWAY=10.0.0.200 # 真实场景不可能将客户端网关指向企业的路由器上
1
2
3
4
5
6
7
8
9
10重启
Client
节点网络;[root@client ~]# systemctl restart network
1使用
Client
测试访问效果;[root@client ~]# curl -HHost:lvs.birenchong.cn http://172.16.1.100 Web Page RS-Node1 [root@client ~]# curl -HHost:lvs.birenchong.cn http://172.16.1.100 Web Page RS-Node2
1
2
3
4模拟真实场景,首先删除
Client
节点指向Route
网关信息,然后配置Route
打开IP映射DNAT
、以及SNAT(共享上网功能)
;- 删除
Client
节点的网关配置
[root@client ~]# sed -i '/GATEWAY/d' /etc/sysconfig/network-scripts/ifcfg-eth0 [root@client ~]# systemctl restart network
1
2- 配置
Route
路由节点的DNAT
以及SNAT
# DNAT(地址映射,企业环境使用) [root@route ~]# iptables -t nat -A PREROUTING -d 10.0.0.200 -j DNAT --to 172.16.1.100 # SNAT(让内部主机通过路由可以上网) [root@route ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to 10.0.0.200
1
2
3
4
5- 删除
最后用
Client
再次测试;# 真实情况下,客户端节点也是无法连接企业内部网络 [root@client ~]# curl -HHost:lvs.birenchong.cn http://172.16.1.100 curl:(7) Failed to connect to 172.16.1.100:网络不可达 # 需要通过访问路由的公网IP [root@client ~]# curl -HHost:lvs.birenchong.cn http://10.0.0.200 Web Page RS-Node2 [root@client ~]# curl -HHost:lvs.birenchong.cn http://10.0.0.200 Web Page RS-Node1
1
2
3
4
5
6
7
8
9
Last Updated: 2022/03/30, 11:09:09