DR模式LVS负载均衡集群部署
一、搭建环境
主机 | 操作系统 | IP地址 | 软件包 |
---|---|---|---|
DR 服务器(负载调度器) | CentOS7 | ens33:192.168.117.20 ens33:0 (VIP):192.168.117.100 | ipvsadm |
NFS服务器 | CentOS7 | 192.168.117.30 | rpcbind、nfs-utils |
Web节点服务器1 | CentOS7 | ens33:192.168.117.40 lo:0 (VIP):192.168.117.100 | nfs-utils、rpcbind、httpd |
Web节点服务器2 | CentOS7 | ens33:192.168.117.50 lo:0 (VIP):192.168.117.100 | nfs-utils、rpcbind、httpd |
客户端 | Windows10 | 192.168.117.88 |
二、DR模式LVS负载均衡集群部署
1. 部署共享存储
NFS服务器:192.168.117.30
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum -y install nfs-utils rpcbind
systemctl start rpcbind.service
systemctl start nfs.service
systemctl enable nfs.service
systemctl enable rpcbind.service
mkdir /opt/qq
mkdir /opt/ww
chmod 777 /opt/qq
chmod 777 /opt/ww
vim /etc/exports
/opt/qq 192.168.117.0/24(rw,sync)
/opt/ww 192.168.117.0/24(rw,sync)
exportfs -rv
2. 配置节点服务器
Web1:ens33:192.168.117.40 lo:0 (VIP):192.168.117.100
Web2:ens33:192.168.117.50 lo:0 (VIP):192.168.117.100
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
2.1 配置虚拟IP地址
VIP:192.168.117.100
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.117.100
NETMASK=255.255.255.255
ONBOOT=yes
ifup lo:0
ifconfig lo:0
#设置临时的路由,重启失效;禁锢路由
route add -host 192.168.117.100 dev lo:0
#查看路由
route -n
#开机自动添加路由,生产环境应该用这个
vim /etc/rc.local
/sbin/route add -host 192.168.117.100 dev lo:0
chmod +x /etc/rc.d/rc.local
2.2 调整内核的ARP 响应参数以阻止更新VIP的MAC地址,避免发生冲突
vim /etc/sysctl.conf
......
net.ipv4.conf.lo.arp_ignore = 1 #系统只响应目的IP为本地IP的ARP请求
net.ipv4.conf.lo.arp_announce = 2 #系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
yum install -y nfs-utils rpcbind httpd
systemctl start rpcbind
systemctl enable rpcbind
systemctl start httpd.service
systemctl enable httpd.service
下面两个服务器设置有点不一样了
Web1:ens33:192.168.117.40 lo:0 (VIP):192.168.117.100
showmount -e 192.168.117.30
mount.nfs 192.168.117.30:/opt/qq /var/www/html
echo 'this is qq web!' > /var/www/html/index.html
Web2:ens33:192.168.117.50 lo:0 (VIP):192.168.117.100
showmount -e 192.168.117.30
mount.nfs 192.168.117.30:/opt/ww /var/www/html
echo 'this is ww web!' > /var/www/html/index.html
3. 配置负载调度器
DR 服务器(负载调度器):ens33:192.168.117.20 ens33:0 (VIP):192.168.117.100
3.1 关闭防火墙,加载ip_vs模块
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
#加载ip_vs模块,并安装ipvsadm工具
modprobe ip_vs
cat /proc/net/ip_vs
yum install -y ipvsadm
3.2 配置虚拟IP地址
vim /etc/sysconfig/network-scripts/ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.117.100
NETMASK=255.255.255.255
ifup ens33:0
ifconfig ens33:0
3.3 调整proc响应参数
#由于 LVS 负载调度器和各节点需要共用 VIP 地址,应该关闭Linux 内核的重定向参数响应,不充当路由器,
vim /etc/sysctl.conf
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
3.4 配置负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
或者
ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
#清除原有策略
ipvsadm -C
ipvsadm -A -t 192.168.117.100:80 -s rr
ipvsadm -a -t 192.168.117.100:80 -r 192.168.117.40:80 -g
ipvsadm -a -t 192.168.117.100:80 -r 192.168.117.50:80 -g
#查看节点状态,Route代表 DR模式
ipvsadm -ln