对比 LVS 负载均衡群集的 NAT 模式和 DR 模式,比较其各自的优势 , 基于 openEuler 构建 LVS-DR 群集。

发布于:2025-02-15 ⋅ 阅读:(18) ⋅ 点赞:(0)

对比 LVS 负载均衡群集的 NAT 模式和 DR 模式,比较其各自的优势 

NAT模式的优势:

可以隐藏后端服务器的IP地址,提高了系统的安全性。

支持多个后端服务器共享同一个IP地址,提高了系统的可扩展性。

可以在负载均衡器和后端服务器之间添加防火墙等网络设备,进一步提高了系统的安全性。

支持对客户端请求进行SNAT(源网络地址转换),使得后端服务器可以直接返回数据给客户端,减轻了后端服务器的负载。

配置相对简单,无需复杂的网络设置,适合初学者和小型网络环境。

网络架构灵活,LVS调度器和后端服务器可以位于不同的网段,增加了配置的灵活性。

DR模式的优势:

性能更高,延迟更低。因为DR模式在转发请求时不需要进行IP地址转换,数据包可以直接路由到后端服务器,减少了处理时间。

可以通过配置路由器或交换机实现负载均衡器与后端服务器之间的物理隔离,提高了系统的安全性

支持对客户端请求进行DNAT(目的网络地址转换),实现负载均衡器的高可用性。

配置简单(但相对于NAT模式,其网络配置可能更复杂一些),易于维护和扩展,特别是当后端服务器数量较多时。

适用于高并发、大规模的负载均衡需求,例如大型网站或数据中心。

构建 LVS-DR 集群

首先准备以下服务器

192.168.1.11(nginx1)

192.168.1.134(nginx2)

192.168.1.135(lvs)

192.168.1.12(Client) 

不要忘了vip 192.168.1.10

1、192.168.1.11(nginx1)

关闭防火墙和selinux,安装nginx软件

[root@openEuler1 ~]# systemctl stop firewalld
[root@openEuler1 ~]#  setenforce 0
setenforce: SELinux is disabled
[root@openEuler1 ~]# yum install nginx -y

编辑内容

[root@openEuler1 ~]# cd /usr/share/nginx/html/
[root@openEuler1 html]# echo "web1 ip192.168.1.11" > index.html
[root@openEuler1 html]# cd /usr/share/nginx/html/
[root@openEuler1 html]# ls
404.html  50x.html  index.html  nginx-logo.png

将nginx设置为开机自启动,并且curl测试访问成功 

[root@openEuler1 html]# systemctl enable --now nginx.service
[root@openEuler1 html]# curl localhost
web1 ip192.168.1.11

网络中增加VIP(192.168.1.10)

[root@openEuler1 html]# nmcli connection add type dummy ifname dummy2 ipv4.method manual ipv4.addresses 192.168.1.10/32

arp内核配置在增加命令 

net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.dummy2.arp_ignore = 1
net.ipv4.conf.dummy2.arp_announce = 2

2、192.168.1.134(nginx2) 

关闭防火墙和selinux,安装nginx软件

 编写内容被设置开机自启

网络中增加VIP(192.168.1.10) 

arp内核配置在增加命令 

3、192.168.1.135(LVS)

网络中增加VIP(192.168.1.10)

 安装和配置 LVS,命令安装 ipvsadm

手动执行配置添加LVS服务器增加两台RS

4、192.168.1.12(Client) 

在客户端上测试效果

[root@mynewhostname ~]# for ((i=1;i<=6;i++))
> do
> curl 192.168.1.10
> done
web2 ip192.168.1.134
web1 ip192.168.1.11
web2 ip192.168.1.134
web1 ip192.168.1.11
web2 ip192.168.1.134
web1 ip192.168.1.11