LVS简单的NAT配置

发布于:2025-03-28 ⋅ 阅读:(32) ⋅ 点赞:(0)

LVS/NAT

lvs/nat网络地址转换模式,进站/出站的数据流量经过分发器(IP负载均衡,他修改的是IP地址)  --利用三层功能

原理:就是把客户端发来的数据包的IP头的目的地址,在负载均衡器上换成其中一台RS的IP地址,并发至此RS来处理,RS处理完成后把数据交给经过负载均衡器,负载均衡器再把数据包的源IP地址改为自己的IP,将目的地址改为客户端IP地址即可。期间,无论是进来的流量,还是出去的流量,都必须经过负载均衡器。   

优点:集群中的物理服务器可以使用任何支持TCP/IP操作系统,只有负载均衡器需要一个合法的IP地址。

缺点:扩展性有限。当服务器节点(普通PC服务器)增长过多时,负载均衡器将成为整个系统的瓶颈,因为所有的请求包和应答包的流向都经过负载均衡器。当服务器节点过多时,大量的数据包都交汇在负载均衡器那,速度就会变慢!

题目:

使用LVS的 NAT 模式实现 3 台RS的轮询访问。

ip规划:

主机 角色 系统 网络 IP
client client redhat9.5 仅主机 192.168.10.100/24
lvs lvs redhat9.5 仅主机/NAT

192.168.10.200/24 VIP

192.168.113.8/24 DIP

nginx rs1 redhat9.5 NAT 192.168.113.7/24 RIP
nginx rs2 redhat9.5 NAT 192.168.113.17/24 RIP
nginx rs3 redhat9.5 NAT 192.168.113.37/24 RIP

RS:Real Server                #后端请求处理服务器

VIP:Director Virtual IP      #负载均衡器虚拟IP 

DIP:Director IP                 #负载均衡器IP

RIP:Real Server IP          #后端请求处理服务器IP

配置RS服务器

配置RS1

#修改虚拟机的主机名为rs1
[root@localhost ~]# hostnamectl hostname rs1
#修改rs1的IP地址与网关
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.113.7/24 ipv4.gateway 192.168.113.8 connection.autoconnect yes
#启动网卡
[root@localhost ~]# nmcli c up ens160
#安装nginx服务
[root@rs1 ~]# dnf install nginx -y
#修改默认访问页的内容为主机ip
[root@rs1 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
#设置开机自启动并重启服务
[root@rs1 ~]# systemctl enable --now nginx
#测试nginx服务
[root@rs1 ~]# curl localhost
192.168.113.7

配置RS2

#修改虚拟机的主机名为rs2
[root@localhost ~]# hostnamectl hostname rs2
#修改rs1的IP地址与网关
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.113.17/24 ipv4.gateway 192.168.113.8 connection.autoconnect yes
#启动网卡
[root@localhost ~]# nmcli c up ens160
#安装nginx服务
[root@rs2 ~]# dnf install nginx -y
#修改默认访问页的内容为主机ip
[root@rs2 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
#设置开机自启动并重启服务
[root@rs2 ~]# systemctl enable --now nginx
#测试nginx服务
[root@rs2 ~]# curl localhost
192.168.113.17

配置RS3

#修改虚拟机的主机名为rs3
[root@localhost ~]# hostnamectl hostname rs3
#修改rs1的IP地址与网关
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 192.168.113.37/24 ipv4.gateway 192.168.113.8 connection.autoconnect yes
#启动网卡
[root@localhost ~]# nmcli c up ens160
#安装nginx服务
[root@rs3 ~]# dnf install nginx -y
#修改默认访问页的内容为主机ip
[root@rs3 ~]# echo $(hostname -I) > /usr/share/nginx/html/index.html
#设置开机自启动并重启服务
[root@rs3 ~]# systemctl enable --now nginx
#测试nginx服务
[root@rs3 ~]# curl localhost
192.168.113.37

配置LVS/NAT模式:

查看网络连接设备名称:

#查看网络连接设备
[root@lvs ~]# nmcli connection show 
NAME    UUID                                  TYPE      DEVICE 
ens160  4e81d5b9-0b6b-30f7-8435-dcc3fcc865ae  ethernet  ens160 
ens224  dc267a7e-9eec-3001-8c7f-4120c7429203  ethernet  ens224 
lo      9c65e17a-057c-4554-9864-1295f4f37590  loopback  lo   

修改主机名与ip

#修改主机名为lvs
[root@localhost ~]# hostnamectl hostname lvs
#修改网络ip
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 
192.168.10.200/24 ipv4.gateway 192.168.10.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
重启网卡
[root@localhost ~]# nmcli c up ens160

安装 ipvsamd

[root@lvs ~]# dnf install ipvsadm -y
Last metadata expiration check: 0:00:01 ago on Sat 22 Mar 2025 05:29:29 PM CST.
Dependencies resolved.
====================================================================================================
 Package               Architecture         Version                   Repository               Size
====================================================================================================
Installing:
 ipvsadm               x86_64               1.31-6.el9                AppStream                54 k

Transaction Summary
====================================================================================================
Install  1 Package

Total size: 54 k
Installed size: 89 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                            1/1 
  Installing       : ipvsadm-1.31-6.el9.x86_64                                                  1/1 
  Running scriptlet: ipvsadm-1.31-6.el9.x86_64                                                  1/1 
  Verifying        : ipvsadm-1.31-6.el9.x86_64                                                  1/1 
Installed products updated.

Installed:
  ipvsadm-1.31-6.el9.x86_64                                                                         

Complete!

配置NAT模式网卡:

#修改ip
[root@lvs ~]# nmcli c modify ens224 ipv4.method manual ipv4.addresses 192.168.113.8/24 ipv4.gateway 192.168.113.2 connection.autoconnect yes
#重启网卡
[root@lvs ~]# nmcli c up ens224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)

启动ipvsadm服务:

#用于保存当前 IPVS(Linux 内核级负载均衡)的规则配置,以便系统重启后自动恢复。   
[root@lvs ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
#重启ipvsadm服务
[root@lvs ~]# systemctl restart ipvsadm.service 

LVS 规则匹配

#创建一个虚拟服务器 -A添加虚拟服务器 -t指定虚拟服务器的 TCP 协议和 VIP -s调度算法
ipvsadm -A -t 192.168.10.200:80 -s rr
#向 IPVS 负载均衡集群中添加一台真实服务器 w2 表示比默认权重(1)更高优先级 -m设置 NAT 模式 -r指定真实服务器的 IP: 端口 -a添加(Add)真实服务器到虚拟服务器 -t指定虚拟服务器的 IP: 端口
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.7:80 -m -w2
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.17:80 -m -w2
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.37:80 -m -w2

启用ip转发:

[root@lvs ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@lvs ~]# sysctl -p
net.ipv4.ip_forward = 1

重启ipvsadm服务

systemctl restart ipvsadm.service

使用客户机测试:

[root@client ~]# curl 192.168.10.200
192.168.113.7
[root@client ~]# curl 192.168.10.200
192.168.113.37
[root@client ~]# curl 192.168.10.200
192.168.113.17

使用shell脚本一键部署代码

#!/bin/bash
# LVS NAT 模式轮询配置脚本(VS 执行)

# 配置虚拟服务器
ipvsadm -C  # 清空旧规则
ipvsadm -A -t 192.168.10.200:80 -s rr
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.7:80 -m -w2
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.17:80 -m -w2
ipvsadm -a -t 192.168.10.200:80 -r 192.168.113.37:80 -m -w2

# 启用 IP 转发
sysctl -w net.ipv4.ip_forward=1
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf

# 保存规则
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl enable --now ipvsadm

echo "LVS NAT 轮询配置完成!"
ipvsadm -Ln --detail

总结:

  • RS 必须将 VS 设为默认网关,否则响应无法返回。
  • VS 需开启 IP 转发(net.ipv4.ip_forward=1)。
  • RS 无需配置 VIP(虚拟服务器 IP)。