NAT模式 LVS负载均衡群集部署
LVS负载调度器:ens33:192.168.50.22 ens36:10.0.0.1(vmnet1)
Web1 节点服务器1:192.168.50.56
Web2 节点服务器2:192.168.50.57
NFS服务器:192.168.50.37
客户端(虚拟机):10.0.0.12 (Vmnet1)
1、部署共享存储(NFS服务器:192.168.50.37)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0yum install -y nfs-utils rpcbind
systemctl start nfs.servicesystemctl enable nfs.service
systemctl start rpcbind.service
systemctl enable rpcbind.servicemkdir /opt/lc1 /opt/lc2
chmod 777 /opt/lc1 /opt/lc2vim /etc/exports
/opt/lc1 192.168.50.0/24(rw,sync)
/opt/lc2 192.168.50.0/24(rw,sync)#发布共享
exportfs -rv
showmount -e
2、配置节点服务器(后端服务器)
192.168.50.56、192.168.50.57
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0yum install -y httpd
systemctl start httpd.service
systemctl enable httpd.serviceyum install -y nfs-utils rpcbind
systemctl start rpcbind.service
systemctl enable rpcbind.serviceshowmount -e 192.168.50.37
#web1:192.168.50.56
一、临时挂载
mount 192.168.50.37:/opt/lc1 /var/www/html/echo 'lc is my love' > /var/www/html/index.html
二、永久挂载
vim /etc/fstab
192.168.50.37:/opt/lc1 /var/www/html nfs defaults 0 0
mount -a#web2:192.168.50.57
mount 192.168.50.37:/opt/lc2 /var/www/html/echo 'my love is lc' > /var/www/html/index.html
vim /etc/fstab
192.168.50.37:/opt/lc2 /var/www/html nfs defaults 0 0mount -a
3.配置负载调度器LVS
ens33:192.168.50.22 ens36:10.0.0.1
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0#配置SNAT转发规则
vim /etc/sysctl.conf
net.ipv4.ip_forward=1sysctl -p
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -nL[root@localhost ~]# iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens36 -j SNAT --to-source 10.0.0.1
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vim ifcfg-ens36[root@localhost network-scripts]# systemctl restart network
modprobe ip_vs #手动加载ip_vs模块
cat /proc/net/ip_vs #查看ip_vs版本信息
yum install -y ipvsadm
ipvsadm --save > /etc/sysconfig/ipvsadm #启动服务前必须保存负载分配策略systemctl start ipvsadm.service
配置负载分配策略(NAT模式只要在服务器上配置,节点服务器不需要特殊配置)ipvsadm -C #清除原有策略
ipvsadm -A -t 10.0.0.1:80 -s rr
ipvsadm -a -t 10.0.0.1:80 -r 192.168.50.56:80 -m -w 1
ipvsadm -a -t 10.0.0.1:80 -r 192.168.50.57:80 -m -w 1ipvsadm #启动策略
ipvsadm -ln #查看节点状态,Masq代表 NAT模式
ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略
4.客户端(虚拟机):10.0.0.12 (Vmnet1)使用浏览器访问10.0.0.1,刷新浏览器测试负载均衡
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33