技术文档:LVS负载均衡群集(NAT模式)部署文档
1 实验准备
主机 | 操作系统 | IP地址 | 工具/软件/安装包 |
---|---|---|---|
负载调度器 | CentOS7 | 内网:192.168.153.10、外网:12.0.0.1 | ipvsadm |
NFS服务器 | CentOS7 | 192.168.153.20 | rpcbind、nfs-utils |
节点服务器1 | CentOS7 | 192.168.153.30 | rpcbind、nfs-utils、httpd |
节点服务器2 | CentOS7 | 192.168.153.40 | rpcbind、nfs-utils、httpd |
客户端 | Windows10 | 12.0.0.12 | / |
2 部署NFS服务器
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/muzi /opt/hua
chmod 777 /opt/muzi /opt/hua
vim /etc/exports
/usr/share *(ro,sync)
/opt/muzi 192.168.199.0/24(rw,sync)
/opt/hua 192.168.199.0/24(rw,sync)
---发布共享---
exportfs -rv
3 部署节点服务器
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.199.40
systemctl start rpcbind.service
systemctl enable rpcbind.service
--192.168.80.20--
mount.nfs 192.168.199.40:/opt/muzi /var/www/html
echo 'this is muzi wed!' > /var/www/html/index.html
--192.168.80.30--
mount.nfs 192.168.199.40:/opt/hua /var/www/html
echo 'this is hua wed!' > /var/www/html/index.html
4 配置负载调度器
- 先增加一块网卡
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
(1)配置SNAT转发规则
vim /etc/sysctl.conf cat /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.199.0/24 -o ens36 -j SNAT --to-source 12.0.0.1
(2)加载LVS内核模块
modprobe ip_vs #加载 ip_vs模块
cat /proc/net/ip_vs #查看 ip_vs版本信息
(3)安装ipvsadm 管理工具
yum -y install ipvsadm
启动服务前须保存负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
或者 ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
(4)配置负载分配策略(NAT模式只要在服务器上配置,节点服务器不需要特殊配置)
ipvsadm -C #清除原有策略
ipvsadm -A -t 12.0.0.1:80 -s rr
ipvsadm -a -t 12.0.0.1:80 -r 192.168.199.20:80 -m [-w 1]
ipvsadm -a -t 12.0.0.1:80 -r 192.168.199.30:80 -m [-w 1]
ipvsadm #启动策略
ipvsadm -ln #查看节点状态,Masq代表 NAT模式
ipvsadm-save > /etc/sysconfig/ipvsadm #保存策略
5 客户机浏览器访问测试
- 在一台IP为12.0.0.12的客户机使用浏览器访问 http://12.0.0.1/ ,不断刷新浏览器测试负载均衡效果,刷新间隔需长点。