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 仅主机模式)
注:干净环境下配置
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
##192.168.35.9安装
[root@localhost ~]# yum install -y nfs-utils rpcbind
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# systemctl stop firewalld
[root@server2 ~]# systemctl disable firewalld
[root@server2 ~]# setenforce 0
[root@server2 ~]# yum install -y nfs-utils rpcbind
##Web2 节点服务器:192.168.35.7
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install -y nfs-utils rpcbind
##192.168.113.113.126
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# systemctl start nfs
[root@server2 ~]# systemctl enable nfs
[root@server2 ~]# systemctl start rpcbind
[root@server2 ~]# systemctl enable rpcbind
##Web2 节点服务器:192.168.35.7
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
##NFS 服务器 192.168.35.9
[root@localhost ~]# mkdir /opt/dxj /opt/xtj
[root@localhost ~]# chmod 777 /opt/dxj /opt/xtj
[root@localhost ~]# 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
##添加给予共享的目录和网段地址,和权限
[root@localhost ~]# vim /etc/exports
/opt/dxj 192.168.35.0/24(rw,sync)
/opt/xtj 192.168.35.0/24(rw,sync)
##发布共享
[root@localhost ~]# exportfs -rv
exporting 192.168.35.0/24:/opt/xtj
exporting 192.168.35.0/24:/opt/dxj
##查看共享列表是否发布
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/xtj 192.168.35.0/24
/opt/dxj 192.168.35.0/24
##测试是否能共享
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# 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
[root@localhost ~]# 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
[root@server2 ~]# yum install -y httpd
##Web2 节点服务器:192.168.35.7
##安装httpd
[root@localhost ~]# yum install -y httpd
##挂载共享目录
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# mount.nfs 192.168.35.9:/opt/xtj /var/www/html
##查看挂载
[root@server2 ~]# df -h
192.168.35.9:/opt/xtj 47G 4.1G 43G 9% /var/www/html
##设置个页面在共享目录下,测试是否共享
[root@server2 ~]# echo 'this is xtj' > /var/www/html/index.html
[root@server2 ~]# ls /var/www/html/
index.html
##挂载共享目录
##Web2 节点服务器:192.168.35.7
[root@server2 ~]# mount.nfs 192.168.35.9:/opt/dxj /var/www/html
##查看挂载
[root@server2 ~]# df -h
192.168.35.9:/opt/dxj 47G 4.1G 43G 9% /var/www/html
##设置个页面在共享目录下
[root@server2 ~]# echo 'this is dxj' > /var/www/html/index.html
[root@localhost ~]# ls /var/www/html/
index.html
##NFS 服务器 192.168.35.9查看
[root@localhost ~]# ls /opt/xtj
index.html
[root@localhost ~]# ls /opt/dxj
index.html
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# systemctl start httpd
##Web2 节点服务器:192.168.35.7
[root@localhost ~]# systemctl start httpd
##Web1 节点服务器:192.168.35.8
[root@server2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@server2 ~]# systemctl restart network
##Web2 节点服务器:192.168.35.7
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost ~]# systemctl restart network
##行尾添加
[root@localhost network-scripts]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
##查看
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost network-scripts]# yum install -y iptables*
[root@localhost network-scripts]# systemctl start iptables.service
[root@localhost network-scripts]# iptables -F ##清空所有规则
[root@localhost network-scripts]# iptables -t nat -vnl ##查看
##添加规则
[root@localhost network-scripts]# iptables -t nat -A POSTROUTING -s 192.168.35.0/24 -o ens36 -j SNAT --to-source 12.0.0.1
[root@localhost 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:控制类型
##手动加载ip_vs模块
[root@localhost network-scripts]# modprobe ip_vs
##查看ip_vs版本信息
[root@localhost 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
[root@localhost network-scripts]# yum install -y ipvsadm
##注:启动服务前必须保存负载分配策略,否则将会保存
ipvsadm-save > /etc/sysconfig/ipvsadm
或者
ipvsadm --save > /etc/sysconfig/ipvsadm
或者
touch /etc/sysconfig/ipvsadm
[root@localhost network-scripts]# ipvsadm-save > /etc/sysconfig/ipvsadm
##启动服务
[root@localhost network-scripts]# systemctl start ipvsadm.service
##清空原有策略
[root@localhost network-scripts]# ipvsadm -C
##添加新策略
[root@localhost network-scripts]# ipvsadm -A -t 12.0.0.1:80 -s rr
[root@localhost network-scripts]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.35.8:80 -m -w 1
[root@localhost network-scripts]# ipvsadm -a -t 12.0.0.1:80 -r 192.168.35.7:80 -m -w 1
##启动服务
[root@localhost 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
##保存负载分配策略,防止重启机器策略丢失
[root@localhost network-scripts]# ipvsadm-save > /etc/sysconfig/ipvsadm
-A 添加虚拟服务器
-S 指定负载调度算法(轮询:rr、加权轮询:wrr、最少连接:1c、加权最少连接:w1c)
-a 表示添加真实服务器(后端节点服务器)
-t 指定VIP地址及TCP端口
-m 表示使用NAT群集模式
-W 设置权重(权重为0时表示暂停节点)
-a 表示添加真实服务器(后端节点服务器)
-t 指定VIP地址及TCP端口
-m 表示使用NAT群集模式
-W 设置权重(权重为0时表示暂停节点)
[外链图片转存中…(img-PEMDZ4S3-1659442274964)]
[外链图片转存中…(img-pfKW8thB-1659442274965)]
[外链图片转存中…(img-GLOM02YZ-1659442274967)]
[外链图片转存中…(img-tn5isb53-1659442274968)]
[外链图片转存中…(img-6mPatPzi-1659442274969)]
在windows机器测试192.168.127.1