环境准备
一台LVS
eth1 仅主机 192.168.10.100/24
eth0 NAT 10.0.0.8/24
两台RS
RS1: 10.0.0.7/24 GW: 10.0.0.8 NAT
RS2: 10.0.0.17/24 GW: 10.0.0.8 NAT
客户端
eth0 仅主机 192.168.10.6/24
网卡配置
centos 网卡的配置文件在:/etc/sysconfig/network-scripts/
配置四台机器的网卡
LVS
ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=10.0.0.8
PREFIX=24
ONBOOT=yes
GATEWAY=10.0.0.2
ifcfg-eth1
DEVICE=eth1
NAME=eth1
BOOTPROTO=static
IPADDR=192.168.10.100
PREFIX=24
ONBOOT=yes
RS1
ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=10.0.0.8
ONBOOT=yes
RS2
ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=10.0.0.17
PREFIX=24
GATEWAY=10.0.0.8
ONBOOT=yes
客户端
ifcfg-eth0
DEVICE=eth0
NAME=eth0
BOOTPROTO=static
IPADDR=192.168.10.6
PREFIX=24
ONBOOT=yes
配置服务
LVS
安装服务
yum makecache
yum -y install ipvsadm
开启ip_forward
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
配置LVS,为了方便观察,我设置了轮询算法(rr),-m表示NAT模式
ipvsadm -A -t 192.168.10.100:80 -s rr
ipvsadm -a -t 192.168.10.100:80 -r 10.0.0.7:80 -m
ipvsadm -a -t 192.168.10.100:80 -r 10.0.0.17:80 -m
RS1
yum -y install httpd
echo rs1 10.0.0.7 >/var/www/html/index.html
systemctl start httpd
RS2
yum -y install httpd
echo rs2 10.0.0.17 >/var/www/html/index.html
systemctl start httpd
在客户端测试访问结果(虽然访问的LVS的80端口,但是LVS服务器不需要监听80端口,因为它是内核的功能)
while :;do curl 192.168.10.100;sleep 0.5;done
保存和加载规则
ipvsadm-save -n > /etc/sysconfig/ipvsadm
ipvsadm-restore < /etc/sysconfig/ipvsadm
清空规则
ipvsadm -C