实验前准备:
LVS负载调度器:ens33:192.168.20.11 ens34:192.168.188.3
Web1节点服务器1:192.168.20.12
Web2节点服务器2:192.168.20.13
NFS服务器:192.168.20.14
客户端(win11):192.168.188.1(使用的是本机,非虚拟机)
-
部署共享存储(NFS服务器:192.168.20.14)
-
关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0 -
安装nfs服务
yum install -y nfs-utils rpcbind -
启动服务
systemctl start nfs.service
systemctl start rpcbind.service
systemctl enable nfs.service
systemctl enable rpcbind.service -
创建两个目录作为共享目录
mkdir /opt/kgc /opt/dzd
chmod 777 /opt/kgc /opt/dzd -
配置共享文件并发布共享
exportfs -rv/usr/share *(ro,sync) /opt/kgc 192.168.20.0/24(rw,sync) /opt/dzd 192.168.20.0/24(rw,sync)
showmount -e
-
-
配置节点服务器(192.168.20.12 192.168.20.13)
-
关闭防火墙
-
安装httpd服务
yum install -y httpd
systemctl start httpd.service
systemctl enable httpd.service -
安装nfs服务
yum install -y nfs-utils rpcbind
systemctl start rpcbind.service
systemctl enable rpcbind.service查看共享情况
showmount -e 192.168.20.14 -
挂载共享目录
192.168.20.12的服务器:mount.nfs 192.168.20.14:/opt/kgc /var/www/html/ echo "hello,here is kgc" > /var/www/html/index.html
vim /etc/fstab(进行永久挂载)
192.168.20.14:/opt/kgc /var/www/html nfs defaults,_netdev 0 0
mount -a(刷新)
192.168.20.13的服务器:mount.nfs 192.168.20.14:/opt/dzd /var/www/html/ echo 'hello,i am dzd' > /var/www/html/index.html
vim /etc/fstab
192.168.20.14:/opt/dzd /var/www/html nfs defaults,_netdev 0 0
mount -a
-
-
配置负载调度器LVS(ens33:192.168.20.11 ens34:192.168.188.3)
-
关闭防火墙
-
配置SNAT转发规则
vim /etc/sysctl.confnet.ipv4.ip_forward=1
sysctl -p
iptables -t nat -F(删nat表)
iptables -F(以防出意外可以全删掉,工作中请不要这样)
iptables -t nat -vnL(查看一下nat表是不是删干净了)
添加规则iptables -t nat -A POSTROUTING -s 192.168.20.0/24 -o ens34 -j SNAT --to-source 192.168.188.3
-
加载LVS内核模块
modprobe ip_vs #手动加载ip_vs模块
cat /proc/net/ip_vs #查看ip_vs版本信息 -
安装ipvsadm管理工具
yum install -y ipvsadm
启动服务前必须保存负载分配策略,否则将会报错
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service -
配置负载分配策略
ipvsadm -C #清除原有策略
ipvsadm -A -t 192.168.188.3:80 -s rr
ipvsadm -a -t 192.168.188.3:80 -r 192.168.20.12:80 -m -w 1
ipvsadm -a -t 192.168.188.3:80 -r 192.168.20.13:80 -m -w 1
ipvsadm #启动策略
ipvsadm -ln #查看节点状态,Masq代表 NAT模式
ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略
-
-
测试: