当前位置:   article > 正文

LVS负载均衡集群部署之—NAT模式的介绍及搭建步骤

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.实验拓扑

二、LVS-NAT模式部署

  1. # 版本
  2. [root@lvs ~]# hostnamectl
  3. Static hostname: lvs.timinglee.org
  4. Icon name: computer-vm
  5. Chassis: vm ??
  6. Machine ID: 4748448370474b72a05bc780ede57860
  7. Boot ID: de3da80744f044728475c9c7388a2f1c
  8. Virtualization: vmware
  9. Operating System: Red Hat Enterprise Linux 9.1 (Plow)
  10. CPE OS Name: cpe:/o:redhat:enterprise_linux:9::baseos
  11. Kernel: Linux 5.14.0-162.6.1.el9_1.x86_64
  12. Architecture: x86-64
  13. Hardware Vendor: VMware, Inc.
  14. Hardware Model: VMware Virtual Platform
  1. # lvs主机网卡配置
  2. [root@lvs ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  3. [connection]
  4. id=eth0
  5. type=ethernet
  6. interface-name=eth0
  7. [ipv4]
  8. address1=172.25.254.100/24,172.25.254.2
  9. method=manual
  10. dns=114.114.114.114;
  11. [root@lvs ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection
  12. [connection]
  13. id=eth1
  14. type=ethernet
  15. interface-name=eth1
  16. [ipv4]
  17. address1=192.168.0.100/24
  18. method=manual
  19. webserver1网卡配置,配置网关
  20. [root@webserver1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  21. [connection]
  22. id=eth0
  23. type=ethernet
  24. interface-name=eth0
  25. [ipv4]
  26. address1=192.168.0.10/24,192.168.0.100
  27. method=manual
  28. dns=114.114.114.114;
  29. webserver2网卡配置,配置网关
  30. [root@webserver2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
  31. [connection]
  32. id=eth0
  33. type=ethernet
  34. interface-name=eth0
  35. [ipv4]
  36. address1=192.168.0.20/24,192.168.0.100
  37. method=manual
  38. dns=114.114.114.114;

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

  1. [root@webserver1 ~]# yum install httpd -y
  2. [root@webserver1 ~]# echo webserver1 - 192.168.0.10 > /var/www/html/index.html
  3. [root@webserver1 ~]# systemctl enable --now httpd
  4. Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
  5. [root@webserver2 ~]# yum install httpd -y
  6. [root@webserver2 ~]# echo webserver1 - 192.168.0.20 > /var/www/html/index.html
  7. [root@webserver2 ~]# systemctl enable --now httpd
  8. Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

2.在LVS服务器中检测

  1. [root@lvs ~]# ipvsadm -A -t ^C
  2. [root@lvs ~]# curl 192.168.0.10
  3. webserver1 - 192.168.0.10
  4. [root@lvs ~]# curl 192.168.0.20
  5. webserver2 - 192.168.0.20

 3.在LVS服务器中安装LVS

[root@lvs ~]# yum install ipvsadm.x86_64 -y

 4.新建LVS集群

  1. [root@lvs ~]# ipvsadm -A -t 172.25.254.100:80 -s rr
  2. [root@lvs ~]# ipvsadm -Ln
  3. IP Virtual Server version 1.2.1 (size=4096)
  4. Prot LocalAddress:Port Scheduler Flags
  5. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  6. TCP 172.25.254.100:80 rr

5.添加Real Server服务器节点

  1. -a 添加真实服务器
  2. -d 删除真实服务器
  3. -r 指定真实服务器(Real Server)的地址
  4. -m 使用NAT模式;另外 -g 对应DR模式、-i 对应TUN模式
  5. -w 为节点服务器设置权重,默认为1,只有设置加权轮询或者加权最小连接才生效
  6. #将两个web服务器加入LVS集群,-m表示为NAT模式
  7. [root@lvs ~]# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10:80 -m
  8. [root@lvs ~]# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.20:80 -m
  9. [root@lvs ~]# ipvsadm -Ln
  10. IP Virtual Server version 1.2.1 (size=4096)
  11. Prot LocalAddress:Port Scheduler Flags
  12. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  13. TCP 172.25.254.100:80 rr
  14. -> 192.168.0.10:80 Masq 1 0 0
  15. -> 192.168.0.20:80 Masq 1 0 0
  16. [root@lvs ~]#
  17. #补充:如果想删除节点,将-a换成-d即可

6.开启路由转发模式

  1. 因为LVS的工作原理是路由转发,所以LVS调度服务器需要开启路由转发
  2. [root@lvs ~]# sysctl -a | grep ip_forward
  3. net.ipv4.ip_forward = 0
  4. net.ipv4.ip_forward_update_priority = 1
  5. net.ipv4.ip_forward_use_pmtu = 0
  6. [root@lvs ~]# cat /etc/sysctl.conf
  7. # 定位最后一行添加
  8. net.ipv4.ip_forward = 1
  9. # 生效
  10. [root@lvs ~]# sysctl -p
  11. net.ipv4.ip_forward = 1

7.效果测试

  1. [root@lvs ~]# curl 172.25.254.100
  2. web2 - 192.168.0.20
  3. [root@lvs ~]# curl 172.25.254.100
  4. web1 - 192.168.0.10
  5. [root@lvs ~]#
  6. [root@lvs ~]# for i in {1..10}
  7. > do
  8. > curl 172.25.254.100
  9. > done
  10. web2 - 192.168.0.20
  11. web1 - 192.168.0.10
  12. web2 - 192.168.0.20
  13. web1 - 192.168.0.10
  14. web2 - 192.168.0.20
  15. web1 - 192.168.0.10
  16. web2 - 192.168.0.20
  17. web1 - 192.168.0.10
  18. web2 - 192.168.0.20
  19. web1 - 192.168.0.10

如上可以看到轮询负载均衡效果!!! 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/965111
推荐阅读
相关标签
  

闽ICP备14008679号