LVS load balancing cluster
1. Experiment preparation
Host | operating system | IP address | Tools/software/installation packages |
---|---|---|---|
Load scheduler | CentOS7 | Internal network: 192.168.153.10, external network: 12.0.0.1 | ipvsadm |
NFS server | CentOS7 | 192.168.153.20 | rpcbind、nfs-utils |
Node server 1 | CentOS7 | 192.168.153.30 | rpcbind、nfs-utils、httpd |
Node server 2 | CentOS7 | 192.168.153.40 | rpcbind、nfs-utils、httpd |
Client | Windows10 | 12.0.0.12 | / |
2. Deploy NFS server (192.168.153.40)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum install nfs-utils rpcbind -y
systemctl start rpcbind.service
systemctl start nfs.service
systemctl enable nfs.service
systemctl enable rpcbind.service
mkdir /opt/kgc /opt/benet
chmod 777 /opt/kgc /opt/benet
vim /etc/exports
/usr/share *(ro,sync)
/opt/ 192.168.80.0/24(rw,sync)
/opt/benet 192.168.80.0/24(rw,sync)
exportfs -rv #发布共享
showmount -e #查看 NFS 服务器端共享了哪些目录
3. Deploy the node server (192.168.153.20 192.168.153.30)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum install httpd -y
systemctl start httpd.service
systemctl enable httpd.service
yum install nfs-utils rpcbind -y
showmount -e 192.168.153.40
systemctl start rpcbind.service
systemctl enable rpcbind.service
----192.168.153.20----
mount.nfs 192.168.153.40:/opt/wt /var/www/html
echo 'this is wt wed!' > /var/www/html/index.html
----192.168.153.30----
mount.nfs 192.168.153.40:/opt/dw /var/www/html
echo 'this is dw wed!' > /var/www/html/index.html
vim /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
4. Deploy load scheduler (inner gateway ens33: 192.168.153.10, outer gateway ens37: 12.0.0.1)
①Add gateway
cp /etc/sysconfig/network-scripts/ifcfg-ens33 /etc/sysconfig/network-scripts/ifcfg-ens36
#将ens33的信息复制到ens36
vim /etc/sysconfig/network-scripts/ifcfg-ens36
vim /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network
②Close the firewall and security mechanism
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
③Configure SNAT forwarding rules
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
或 echo '1' > /proc/sys/net/ipv4/ip_forward
sysctl -p
iptables -t nat -F
iptables -F
iptables -t nat -A POSTROUTING -s 192.168.153.0/24 -o ens36 -j SNAT --to-source 12.0.0.1
④Load LVS kernel module
modprobe ip_vs #加载 ip_vs模块
cat /proc/net/ip_vs #查看 ip_vs版本信息
⑤Install ipvsadm management tool
yum -y install ipvsadm
⑥ The load distribution strategy must be saved before starting the service
ipvsadm-save > /etc/sysconfig/ipvsadm #保存负载分配策略
systemctl start ipvsadm.service
ipvsadm -C #清除原有策略
⑦Configure the load distribution strategy (NAT mode only needs to be configured on the server, no special configuration is required for the node server)
ipvsadm -A -t 12.0.0.1:80 -s rr
ipvsadm -a -t 12.0.0.1:80 -r 192.168.153.20:80 -m
ipvsadm -a -t 12.0.0.1:80 -r 192.168.153.30:80 -m
ipvsadm #启动策略
ipvsadm -ln #查看节点状态,Masq代表 NAT模式
ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略
Five, client browser access test
A client with an IP of 12.0.0.12 uses a browser to visit http://12.0.0.1/, and constantly refresh the browser to test the load balancing effect. The refresh interval needs to be longer.