实战操作
(一)环境要求
五台linux虚拟机
1.DR 服务器:192.168.107.136
2.Web 服务器1:192.168.107.137
3.Web 服务器2:192.168.107.138
4.共享服务器:192.168.107.139
5.客户端:192.168.107.140
6.vip(虚拟IP):192.168.107.200
(二)实验步骤
1、配置负载调度器(192.168.107.136)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
modprobe ip_vs #加载ip_vs模块
cat /proc/net/ip_vs #查看ip_vs版本信息
yum install -y ipvsadm
(1)、配置虚拟 IP 地址(VIP:192.168.107.200)
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.107.200
NETMASK=255.255.255.255
ifup ens33:0
ifconfig ens33:0
(2)、调整 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)、配置负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
或者
ipvsadm --save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm.service
ipvsadm -C #清除原有策略
ipvsadm -A -t 192.168.107.200:80 -s rr
ipvsadm -a -t 192.168.107.200:80 -r 192.168.107.137:80 -g #若为隧道模式,-g替换为-i
ipvsadm -a -t 192.168.107.200:80 -r 192.168.107.138:80 -g
ipvsadm -ln #查看节点状态,Route代表 DR模式
2、部署共享存储(NFS服务器:192.168.107.139)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum install -y nfs-utils rpcbind
systemctl start nfs.service
systemctl start rpcbind.service
systemctl enable nfs.service
systemctl enable rpcbind.service
mkdir /opt/dmj /opt/kk
chmod 777 /opt/dmj/ /opt/kk/
vim /etc/exports
/usr/share *(ro,sync)
/opt/dmj 192.168.107.0/24(rw,sync) #/24与()之间不能有空格
/opt/kk 192.168.107.0/24(rw,sync)
exportfs -rv
showmount -e
3、配置节点服务器(192.168.107.137、192.168.107.138)
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
#将两个节点服务器的网关和DNS注释掉后重启网卡,如果有网关服务器则指向网关服务器
(1)、配置虚拟 IP 地址(VIP:192.168.107.200)
#此地址仅用做发送 Web 响应数据包的源地址,并不需要监听客户机的访问请求(改由调度器监听并分发)。因此使用虚接口 lo:0 来承载 VIP 地址,并为本机添加一条路有记录,将访问 VIP 的数据限制在本地,以避免通信紊乱。
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.107.200
NETMASK=255.255.255.255 #注意子网掩码必须全为1
#NETWORK=127.0.0.0
# If you're having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
#BROADCAST=127.255.255.255
ONBOOT=yes
#NAME=loopback
ifup lo:0
ifconfig lo:0
route add -host 192.168.107.200 dev lo:0 #禁锢路由
route -n #查看路由
vim /etc/rc.local
/sbin/route add -host 192.168.107.200 dev lo:0
chmod +x /etc/rc.d/rc.local
(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
或者
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
yum install -y nfs-utils rpcbind httpd
systemctl start rpcbind
systemctl start httpd
----------192.168.107.137-----------------
mount.nfs 192.168.107.139:/opt/dmj /var/www/html/
echo 'this is dmj web!' > /var/www/html/index.html
#设为自动挂载
vim /etc/fstab
192.168.107.139:/opt/dmj /var/www/html nfs defaults,_netdev 0 0
mount -a
----------192.168.107.138-----------------
mount.nfs 192.168.107.139:/opt/kk /var/www/html/
echo 'this is kk web!' > /var/www/html/index.html
#设为自动挂载
vim /etc/fstab
192.168.107.139:/opt/kk /var/www/html nfs defaults,_netdev 0 0
mount -a
4、测试 LVS 群集
在客户端使用浏览器访问: http://192.168.107.200/,默认网关指向 192.168.107.200