欢乐狗 发表于 2026-2-12 08:24:32

【一】LVS负载均衡集群摆设之—NAT模式的先容及搭建步调

一、环境准备

1.准备三台rhel9服务器

                        服务器名称
                                                主机名
                        ip地点备注LVS调理服务器lvs.timinglee.org                        eth0:172.25.254.100(外网)
                        eth1:192.168.0.100(内网)
                        关闭selinux和防火墙webserver2网站服务器webserver1.timinglee.orgeth0:192.168.0.10(内网)关闭selinux和防火墙webserver2网站服务器webserver2.timinglee.orgeth0:192.168.0.20(内网)关闭selinux和防火墙2.实验拓扑

https://dis.qidao123.com/imgproxy/aHR0cHM6Ly9pLWJsb2cuY3NkbmltZy5jbi9kaXJlY3QvNmMxMmVkOTZjNWI2NDBmZTk5YWFmODNhY2ZmNjY0NDgucG5n
二、LVS-NAT模式摆设


# 版本
# hostnamectl
Static hostname: lvs.timinglee.org
       Icon name: computer-vm
         Chassis: vm ??
      Machine ID: 4748448370474b72a05bc780ede57860
         Boot ID: de3da80744f044728475c9c7388a2f1c
Virtualization: vmware
Operating System: Red Hat Enterprise Linux 9.1 (Plow)   
   CPE OS Name: cpe:/o:redhat:enterprise_linux:9::baseos
          Kernel: Linux 5.14.0-162.6.1.el9_1.x86_64
    Architecture: x86-64
Hardware Vendor: VMware, Inc.
Hardware Model: VMware Virtual Platform


# lvs主机网卡配置
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=172.25.254.100/24,172.25.254.2
method=manual
dns=114.114.114.114;
# cat /etc/NetworkManager/system-connections/eth1.nmconnection

id=eth1
type=ethernet
interface-name=eth1


address1=192.168.0.100/24
method=manual


webserver1网卡配置,配置网关
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=192.168.0.10/24,192.168.0.100
method=manual
dns=114.114.114.114;

webserver2网卡配置,配置网关
# cat /etc/NetworkManager/system-connections/eth0.nmconnection

id=eth0
type=ethernet
interface-name=eth0


address1=192.168.0.20/24,192.168.0.100
method=manual
dns=114.114.114.114;



 1.给webserver1,webserver2服务器安装httpd


# yum install httpd -y
# echo webserver1 - 192.168.0.10 > /var/www/html/index.html
# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

# yum install httpd -y
# echo webserver1 - 192.168.0.20 > /var/www/html/index.html
# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
2.在LVS服务器中检测


# ipvsadm -A -t ^C
# curl 192.168.0.10
webserver1 - 192.168.0.10
# curl 192.168.0.20
webserver2 - 192.168.0.20
 3.在LVS服务器中安装LVS


# yum install ipvsadm.x86_64 -y
 4.新建LVS集群


# ipvsadm -A -t 172.25.254.100:80 -s rr
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.25.254.100:80 rr
5.添加Real Server服务器节点


-a   添加真实服务器
-d   删除真实服务器
-r   指定真实服务器(Real Server)的地址
-m   使用NAT模式;另外 -g 对应DR模式、-i 对应TUN模式
-w   为节点服务器设置权重,默认为1,只有设置加权轮询或者加权最小连接才生效


#将两个web服务器加入LVS集群,-m表示为NAT模式

# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10:80 -m
# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.20:80 -m
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
TCP172.25.254.100:80 rr
-> 192.168.0.10:80            Masq    1      0          0         
-> 192.168.0.20:80            Masq    1      0          0         
#


#补充:如果想删除节点,将-a换成-d即可6.开启路由转发模式


因为LVS的工作原理是路由转发,所以LVS调度服务器需要开启路由转发

# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0



# cat /etc/sysctl.conf
# 定位最后一行添加
net.ipv4.ip_forward = 1


# 生效
# sysctl -p
net.ipv4.ip_forward = 1
7.效果测试


# curl 172.25.254.100
web2 - 192.168.0.20
# curl 172.25.254.100
web1 - 192.168.0.10
#
# for i in {1..10}
> do
> curl 172.25.254.100
> done
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web1 - 192.168.0.10
web2 - 192.168.0.20
web1 - 192.168.0.10
如上可以看到轮询负载均衡效果!!! 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金
页: [1]
查看完整版本: 【一】LVS负载均衡集群摆设之—NAT模式的先容及搭建步调