Linux LVS集群-DR模式的简易搭建
首先,需要四台机器,按顺序编号就叫1号,2号,3号,四号。1号主机为测试负载均衡效果的专用测试机器。2号主机为提供lvs服务的机器,3号和四号为真实服务器,也就是对外提供服务的主机。所有机器网卡模式都为桥接模式。操作系统版本为centos7.
网卡名称都为ifcfg-ens33。IP分别为 192.168.0.19,192.168.0.16,192.168.0.17,192.168.018
四台主机都是关闭防火墙和selinux,1号不做设置。
2号主机:执行安装命令。yum install ipvsadm -y 。添加一个vip网卡,该网卡只有配置,名称为ens33:1,内容如下:
==========================================================================
vim /etc/sysconfig/network-scripte/ifcfg-ens33:1
DEVICE=ens33
IPADDR=192.168.0.16
NETMASK=255.255.255.0
TYPE=Etherneten
BOOTPROTO=none
IPV6INIT=no
USERCTL=no
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
NAME="ens33"
========================================================================================
vim /etc/sysconfig/network-script/ifcfg-ens33
DEVICE=ens33:1
NM_CONTROLLED=yes
IPADDR=192.168.0.15
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Etherneten
BOOTPROTO=static
PREFIX=24
DEFROUTE=yes
NAME="ens33:1"
=================================================================================================
执行命令:ipvsadm -A -t 192.168.0.15:80 -s wrr
ipvsadm -a -t 192.168.0.15:80 -r 192.168.0.17:80 -g -w 2
ipvsadm -a -t 192.168.0.15:80 -r 192.168.0.18:80 -g -w 3
systemctl enable ipvsadm && systemctl start ipvsadm
ipvsadm -S -n > /etc/sysconfig/ipvsadm #2号主机配置完毕,注意防火墙和selinux的关闭
3号主机:
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:1
vim ifcfg-lo:1 #编辑这个文件
DEVICE=lo:1
IPADDR=192.168.0.15
NETMASK=255.255.255.255
# 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)
ONBOOT=yes
NAME=loopback
vim /etc/sysctl.conf #编辑这个文件,在该文件末尾添加如下内容
net.ipv4.conf.ens33.arp_ignore = 1
net.ipv4.conf.ens33.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
执行命令 systctl -p && systemctl restart network
安装httpd,并且写入测试文件
yum install httpd && systemctl enable httpd && echo "192.168.0.17">/var/www/html/index.html
systemctl start httpd #注意防火墙和selinux的关闭
4号主机; 配置同3号主机,测试文件修改成 echo "192.168.0.18">/var/www/html/index.html 即可,
==================================================================================
检验配置:2,3,4 都可以看到虚拟出来的 IP 192.168.0.15
执行命令:ipvsadm -Ln ,输出结果如下:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.15:80 wrr
-> 192.168.0.17:80 Route 1 0 0
-> 192.168.0.18:80 Route 1 0 0
看到如上输出就证明配置完成,进入测试主机1号,执行如下命令:
for i in {1..100};do sleep 1; curl 192.168.0.15;done, 那么会看到均匀的打印输出3和4的测试页、配置完毕。