当前位置:网站首页>LVS+NAT 负载均衡群集,NAT模式部署
LVS+NAT 负载均衡群集,NAT模式部署
2022-08-04 18:08:00 【世上无难事754】
文章目录
一,案例部署环境,所需机器配置信息
- LVS调度器作为Web服务器池的网关,LVS两块网卡,分别连接内外网,使用轮询调度算法 (图片只是例子根据自己ip地址配置)
LVS负载均衡调节器: ens33:192.168.35.10 (Vmnet 8 NAT模式)
ens36:12.0.0.1(Vmnet 2 仅主机模式)
Web1 节点服务器:192.168.35.8 网关:192.168.35.10
Web2 节点服务器:192.168.35.7 网关:192.168.35.10
NFS服务器:192.168.113.113.126
客户端 win10:12.0.0.12 网关:12.0.0.1 (Vmnet 2 仅主机模式)
注:干净环境下配置
一.部署LVS负载均衡调节器(192.168.35.10)
1.添加双网卡
- 确认自己的VM2的IP地址
2.编辑ens36内容
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[[email protected] network-scripts]# vim ifcfg-ens36
- 去掉UUID,更改网卡名字,配置网关跟VM2一致,不需要的可以注释或删除,更改后图如下
3.重启网卡,查看是否更改
[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# ifconfig
二,部署共享存储(NFS 服务器 192.168.35.9)
1.关闭防火墙和开机自启,关闭核心防护
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
2.安装共享环境,另外俩也要安装,为了共享(切记关闭防火墙,核心防护)
##192.168.35.9安装
[[email protected] ~]# yum install -y nfs-utils rpcbind
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install -y nfs-utils rpcbind
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install -y nfs-utils rpcbind
3.开启并开机自启动共享服务
##192.168.113.113.126
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl enable rpcbind
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl enable rpcbind
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
[[email protected] ~]# systemctl start rpcbind
[[email protected] ~]# systemctl enable rpcbind
4.创建共享目录,并赋权
##NFS 服务器 192.168.35.9
[[email protected] ~]# mkdir /opt/dxj /opt/xtj
[[email protected] ~]# chmod 777 /opt/dxj /opt/xtj
[[email protected] ~]# ll /opt
总用量 0
drwxrwxrwx. 2 root root 6 7月 31 18:35 dxj
drwxr-xr-x. 2 root root 6 9月 7 2017 rh
drwxrwxrwx. 2 root root 6 7月 31 18:35 xtj
##添加给予共享的目录和网段地址,和权限
[[email protected] ~]# vim /etc/exports
/opt/dxj 192.168.35.0/24(rw,sync)
/opt/xtj 192.168.35.0/24(rw,sync)
##发布共享
[[email protected] ~]# exportfs -rv
exporting 192.168.35.0/24:/opt/xtj
exporting 192.168.35.0/24:/opt/dxj
##查看共享列表是否发布
[[email protected] ~]# showmount -e
Export list for localhost.localdomain:
/opt/xtj 192.168.35.0/24
/opt/dxj 192.168.35.0/24
5.挂载共享目录
##测试是否能共享
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# showmount -e 192.168.35.9
Export list for 192.168.35.9:
/opt/xtj 192.168.35.0/24
/opt/dxj 192.168.35.0/24
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# showmount -e 192.168.35.9
Export list for 192.168.35.9:
/opt/xtj 192.168.35.0/24
/opt/dxj 192.168.35.0/24
##Web1 节点服务器:192.168.35.8
##安装httpd
[[email protected] ~]# yum install -y httpd
##Web2 节点服务器:192.168.35.7
##安装httpd
[[email protected] ~]# yum install -y httpd
##挂载共享目录
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# mount.nfs 192.168.35.9:/opt/xtj /var/www/html
##查看挂载
[[email protected] ~]# df -h
192.168.35.9:/opt/xtj 47G 4.1G 43G 9% /var/www/html
##设置个页面在共享目录下,测试是否共享
[[email protected] ~]# echo 'this is xtj' > /var/www/html/index.html
[[email protected] ~]# ls /var/www/html/
index.html
##挂载共享目录
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# mount.nfs 192.168.35.9:/opt/dxj /var/www/html
##查看挂载
[[email protected] ~]# df -h
192.168.35.9:/opt/dxj 47G 4.1G 43G 9% /var/www/html
##设置个页面在共享目录下
[[email protected] ~]# echo 'this is dxj' > /var/www/html/index.html
[[email protected] ~]# ls /var/www/html/
index.html
##NFS 服务器 192.168.35.9查看
[[email protected] ~]# ls /opt/xtj
index.html
[[email protected] ~]# ls /opt/dxj
index.html
6.重启服务流量器访问测试
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# systemctl start httpd
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# systemctl start httpd
7.配置更改web1 web2 的网关
##Web1 节点服务器:192.168.35.8
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[[email protected] ~]# systemctl restart network
##Web2 节点服务器:192.168.35.7
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[[email protected] ~]# systemctl restart network
- Web1 节点服务器:192.168.35.8配置图片如下
- Web2 节点服务器:192.168.35.7配置图片如下
三,配置SNAT转发规则
1.进入配置文件添加规则
- 192.168.35.10 机器配置
##行尾添加
[[email protected] network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
##查看
[[email protected] network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
2.安装iptables,并添加规则
[[email protected] network-scripts]# yum install -y iptables*
[[email protected] network-scripts]# systemctl start iptables.service
[[email protected] network-scripts]# iptables -F ##清空所有规则
[[email protected] network-scripts]# iptables -t nat -vnl ##查看
##添加规则
[[email protected] network-scripts]# iptables -t nat -A POSTROUTING -s 192.168.35.0/24 -o ens36 -j SNAT --to-source 12.0.0.1
[[email protected] network-scripts]# iptables -t nat -vnL
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 SNAT all -- * ens36 192.168.35.0/24 0.0.0.0/0 to:12.0.0.1
- 添加内容解析
nat表:修改数据包中的源、日标IP地址或湍口
POSTROUTING:在进行路由判断之"后"所要进行的规则(SNAT/MASQUERADE)
PREROUTING:在进行路由判断之"前"所要进行的规则(DNAT/REDIRECT)
-A: 在规则链的末尾加入新规则
-s: 匹配来源地址IP/MASK.
-o:K网卡名称匹配从这块网卡流出的数据
-i:网卡名称匹配从这块网卡流入的数据
-j:控制类型
3.加载LVS内核模块
##手动加载ip_vs模块
[[email protected] network-scripts]# modprobe ip_vs
##查看ip_vs版本信息
[[email protected] network-scripts]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
4.安装ipvsadm管理工具
[[email protected] network-scripts]# yum install -y ipvsadm
##注:启动服务前必须保存负载分配策略,否则将会保存
ipvsadm-save > /etc/sysconfig/ipvsadm
或者
ipvsadm --save > /etc/sysconfig/ipvsadm
或者
touch /etc/sysconfig/ipvsadm
[[email protected] network-scripts]# ipvsadm-save > /etc/sysconfig/ipvsadm
##启动服务
[[email protected] network-scripts]# systemctl start ipvsadm.service
5.配置负载分配策略
- NAT模式只要在服务器上配置,节点服务器不需要特殊配置
##清空原有策略
[[email protected] network-scripts]# ipvsadm -C
##添加新策略
[[email protected] network-scripts]# ipvsadm -A -t 12.0.0.1:80 -s rr
[[email protected] network-scripts]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.35.8:80 -m -w 1
[[email protected] network-scripts]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.35.7:80 -m -w 1
##启动服务
[[email protected] network-scripts]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 192.168.35.8:http Masq 1 0 0
-> 192.168.35.7:http Masq 1 0 0
##保存负载分配策略,防止重启机器策略丢失
[[email protected] network-scripts]# ipvsadm-save > /etc/sysconfig/ipvsadm
- 添加选项说明
-A 添加虚拟服务器
-S 指定负载调度算法(轮询:rr、加权轮询:wrr、最少连接:1c、加权最少连接:w1c)
-a 表示添加真实服务器(后端节点服务器)
-t 指定VIP地址及TCP端口
-m 表示使用NAT群集模式
-W 设置权重(权重为0时表示暂停节点)
四,浏览器范围测试
1.更改win10网卡
-a 表示添加真实服务器(后端节点服务器)
-t 指定VIP地址及TCP端口
-m 表示使用NAT群集模式
-W 设置权重(权重为0时表示暂停节点)
四,浏览器范围测试
1.更改win10网卡
[外链图片转存中…(img-PEMDZ4S3-1659442274964)]
[外链图片转存中…(img-pfKW8thB-1659442274965)]
[外链图片转存中…(img-GLOM02YZ-1659442274967)]
[外链图片转存中…(img-tn5isb53-1659442274968)]
[外链图片转存中…(img-6mPatPzi-1659442274969)]
在windows机器测试192.168.127.1
边栏推荐
猜你喜欢
巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...
JS兼容问题总结
通配符SSL证书不支持多域名吗?
The Industrial Metaverse Brings Changes to Industry
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
报道称任天堂在2023年3月前不会推出任何新硬件产品
"Involution" Index Analysis Based on AHP
(ECCV-2022)GaitEdge:超越普通的端到端步态识别,提高实用性
Thrift IDL Sample File
解决错误:The package-lock.json file was created with an old version of npm
随机推荐
不论你是大众,科班和非科班,我这边整理很久,总结出的学习路线,还不快卷起来
静态iP与权限更改[通俗易懂]
Google Earth Engine APP - one-click online viewing of global images from 1984 to this year and loading an image analysis at the same time
YOLOv7-Pose尝鲜,基于YOLOv7的关键点模型测评
After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution
基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
ATF中断处理的设计模型
dotnet core 使用 CoreRT 将程序编译为 Native 程序
【web自动化测试】Playwright快速入门,5分钟上手
华为云计算HCIE之oceanstor仿真器的安装教程
容器化 | 在 NFS 备份恢复 RadonDB MySQL 集群数据
2022 May 1 Mathematical Modeling Question C Explanation
当项目中自动格式化插件Prettier和ESLint冲突报错时如何解决
2018年南海区小学生程序设计竞赛详细答案
flink-cdc支持并行读取一张mysql表的binlog不?
智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
【STM32】入门(五):串口TTL、RS232、RS485
Enterprise survey correlation analysis case
leetcode 14. 最长公共前缀