当前位置:网站首页>LVS负载均衡之LVS-NAT搭建Web群集
LVS负载均衡之LVS-NAT搭建Web群集
2022-07-25 10:30:00 【星哥玩云】
实现LVS的NAT模式
其原理及特点详细介绍请看LVS的NAT模式LVS负载均衡之LVS-NAT搭建Web群集
1.实验环境
IP地址规划
客户端访问服务的请求IP地址:VIP 12.0.0.1
Server | IP | 系统 |
|---|---|---|
Director Server | DIP 192.168.10.1 | CentOS7 |
NFS Server | 192.168.10.50 | RedHat6 |
Real Server1 | RIP 192.168.10.51 | CentOS7 |
Real Server2 | RIP 192.168.10.52 | CentOS7 |
在配置的Director的服务器上添加两块网卡,详细步骤可参考DNS分离解析这篇文章中有介绍,设置外网ens37为VIP,内网ens33为DIP,把两个 real server 的网关设置为 Director 的内网 iP即DIP。
在虚拟机的配置中DIP网络连接方式设置为仅主机模式,Real Server也配置为仅主机模式。
2.安装和配置
yum安装软件
方法一:如果虚拟机网络是仅主机模式,没有网络,可以本地创建yum仓库,然后进行yum安装。
方法二:如果虚拟机网络是NAT模式,有网络,可以在线yum安装。
(1) 配置NFS服务器
安装nfs软件并启动nfs服务
yum install nfs-utils -y #7系统版本需要安装nfs工具包
service rpcbind start
service nfs restart创建共享目录并赋予写入权限
mkdir /opt/wwwroot1 /opt/wwwroot2
chmod 777 /opt/wwwroot1 /opt/wwwroot2编辑配置文件
vim /etc/exports
/opt/wwwroot1 192.168.10.0/24(rw,sync)
/opt/wwwroot2 192.168.10.0/24(rw,sync) 发布共享
exportfs -rv关闭防火墙
service iptables stop(2) 配置两个real server服务器
安装nfs客户端
yum install nfs-utils -y
systemctl start rpcbind.service
systemctl start nfs.service查看nfs挂载
showmount -e 192.168.10.50Real Server1挂载nfs
#法一:直接挂载
mount.nfs 192.168.10.50:/opt/wwwroot1 /var/www/html
#法二:修改fatab文件挂载
vim /etc/fstab
192.168.10.50:/opt/wwwroot1 /var/www/html nfs defaults,_netdev 0 0Real Server2挂载nfs
方法同Real Server1,将挂载目录/opt/wwwroot1改成/opt/wwwroot2,其余一样。安装httpd
yum install httpd -y创建测试网页并启动httpd服务
#real server1创建测试网页
echo "Server 192.168.10.51" > /var/www/html/index.html
#real server2创建测试网页
echo "Server 192.168.10.52" > /var/www/html/index.html#启动httpd服务
systemctl start httpd.service
systemctl enable httpd.service 关闭防火墙和安全性策略
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0测试网页打开是否正常
firefox http://127.0.0.1/(3) 配置Director server服务器
安装ipvsadm管理工具
yum install ipvsadm -y调用LVS内核模块
modprobe ip_vs #加载LVS内核模块
cat /proc/net/ip_vs #查看ip_vs版本信息开启路由转发
#法一:编辑sysctl.conf文件,永久路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p #保存
#法二:直接编辑,临时路由转发
echo "1" > /proc/sys/net/ipv4/ip_forward配置SNAT转发规则,设置nat防火墙
iptables -F -t nat #清空nat防火墙
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -o ens37 -j SNAT --to-source 12.0.0.1Director 上编辑 nat 实现负载分配脚本
# 设置 ipvsadm
vim nat.sh
#!/bin/bash
ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略
service ipvsadm start
ipvsadm -C #清除内核虚拟服务器表中的所有记录
ipvsadm -A -t 12.0.0.1:80 -s rr #创建虚拟服务器
ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.51:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.10.52:80 -m
ipvsadmipvsadm管理工具的选项用法:
- -A: 表示添加虚拟服务器
- -t: 用来指定VIP地址及TCP端口
- -s: 用来指定负载均衡调度算法
- -a: 表示添加真实服务器
- -r: 用来指定RIP地址及TCP端口
- -m: 表示使用NAT群集模式
- -g: 表示使用DR群集模式
- -i: 表示使用TUN群集模式
- -w: 用来设置权重
保存nat脚本后直接运行
chmod +x nat.sh
./nat.sh查看ipvsadm设置的规则
ipvsadm -ln3.测试LVS群集
用Windows客户端直接访问http://12.0.0.1, 将能够看到由真实服务器提供的网页内容。
第一次访问:
Real Server连接次数查看:
刷新一次:
Real Server连接次数查看:
LVS的DR模式案例部署详细请看LVS负载均衡之LVS-DR搭建Web群集与LVS结合Keepalived搭建高可用Web群集
边栏推荐
- HCIP (01)
- 让运动自然发生,FITURE打造全新生活方式
- 学习路之PHP--Phpstudy 提示 Mysqld.Exe: Error While Setting Value ‘NO_ENGINE_SUBSTITUTION 错误的解决办法
- SQL语言(四)
- MySQL advanced statement (I) (there is always someone who will make your life no longer bad)
- Review recitation finishing version
- UE4.26源码版学习广域网独立服务器时遇到的客户端运行黑屏问题
- 史上最全的立创元器件封装库导入AD详细教程(一直白嫖一直爽)
- 复习背诵整理版
- C# Newtonsoft.Json 高级用法
猜你喜欢
信息熵的定义
![[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)](/img/a0/9110b83ff5c7965809bbc9f3948956.jpg)
[flask advanced] combined with the source code, explain the operation mechanism of flask (in and out of the stack)

MySQL | GROUP_CONCAT函数,将某一列的值用逗号拼接

BGP federal experiment

ESP8266 使用 DRV8833驱动板驱动N20电机
Learn NLP with Transformer (Chapter 4)

Redis sentry, high availability executor

一篇看懂:IDEA 使用scala 编写wordcount程序 并生成jar包 实测

Motivation of enterprises to practice open source

HCIA experiment (08)
随机推荐
API supplement of JDBC
Syncronized lock upgrade process
I, AI doctoral student, online crowdfunding research topic
最详细的mysql索引解析(文末附赠思维导图)
学习周刊 - 总第 63 期 - 一款开源的本地代码片段管理工具
上周热点回顾(7.18-7.24)
Review recitation finishing version
Code representation learning: introduction to codebert and other related models
HCIA experiment (08)
Flame framework - Flame WTF form: file upload, verification code
Esp32c3 based on the example tutorial of esp32 Rainmaker development under Arduino framework
How to notify users of wechat applet version update?
Motivation of enterprises to practice open source
Learn NLP with Transformer (Chapter 5)
【高并发】如何实现亿级流量下的分布式限流?这些理论你必须掌握!!
AI system frontier dynamics issue 43: ONEFLOW V0.8.0 officially released; GPU finds human brain connections; AI doctoral online crowdfunding research topic
让运动自然发生,FITURE打造全新生活方式
三万字速通Servlet
30000 word express Servlet
Google Earth Engine——统计逐年土地分类的频率