目录
配置LVS DR模式
- 环境准备
- client1:eth0-> 192.168.88.10
- lvs1:eth0->192.168.88.5
- web1:eth0->192.168.88.100
- web2:eth0->192.168.88.200
服务器搭建好yum源
搭建步骤
1. 在lvs1的eth0上配置vip 192.168.88.15。通过为eth0创建逻辑端口的方式配置vip,为逻辑端口起名为eth0:0
[root@lvs1 ~]# cd /etc/sysconfig/network-scripts/
[root@lvs1 network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
[root@lvs1 network-scripts]# vim ifcfg-eth0:0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.88.15
PREFIX=24
[root@lvs1 network-scripts]# ifup eth0:0
# 查看新的地址
[root@lvs1 network-scripts]# ifconfig
2. 在2台web服务器的lo上配置vip 192.168.88.15
[root@web1 ~]# cd /etc/sysconfig/network-scripts/
[root@web1 network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@web1 network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
NAME=lo:0
[root@web1 network-scripts]# ifup lo:0
[root@web1 network-scripts]# ifconfig
# 把web1的配置拷贝到web2上
[root@web1 network-scripts]# scp ./ifcfg-lo:0 192.168.88.200:/etc/sysconfig/network-scripts/
[root@web2 ~]# ifup lo:0
[root@web2 ~]# ifconfig
3. 在2台web服务器上配置内核参数,使得它们不响应对192.168.88.15的请求
[root@web1 ~]# sysctl -a | grep arp_ignore
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 0
[root@web1 ~]# sysctl -a | grep arp_announce
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 0
[root@web1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web1 ~]# sysctl -p
[root@web2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
[root@web2 ~]# sysctl -p
4. 在lvs1上配置虚拟服务器
# 创建虚拟服务器
[root@lvs1 ~]# ipvsadm -A -t 192.168.88.15:80 -s wlc
# 向虚拟服务器中加真实服务器
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
[root@lvs1 ~]# ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
# 查看配置
[root@lvs1 ~]# ipvsadm -Ln
# 客户验证
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
apache web server2
192.168.99.100
apache web server2
apache web server2
192.168.99.100
apache web server2
5. lvs本身没有应用服务器监控功能,如果web服务器出现问题,需要手工从规则中删掉
[root@web1 ~]# systemctl stop httpd
# 客户端访问时,转发到web1上的请求,将会拒绝连接
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
apache web server2
curl: (7) Failed connect to 192.168.88.15:80; 拒绝连接
apache web server2
apache web server2
curl: (7) Failed connect to 192.168.88.15:80; 拒绝连接
apache web server2
# 的规则中删除web1
[root@lvs1 ~]# ipvsadm -d -t 192.168.88.15:80 -r 192.168.88.100
[root@client1 ~]# for i in {1..6}; do curl http://192.168.88.15/; done
apache web server2
apache web server2
apache web server2
apache web server2
apache web server2
apache web server2
附:出错时,排错步骤:
# 在lvs上可以访问到web服务器
[root@lvs1 ~]# curl http://192.168.88.100/
192.168.99.100
[root@lvs1 ~]# curl http://192.168.88.200/
apache web server2
# 查看vip
[root@lvs1 ~]# ifconfig eth0:0
eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.88.15 netmask 255.255.255.0 broadcast 192.168.88.255
ether 52:54:00:0d:fb:79 txqueuelen 1000 (Ethernet)
[root@web1 ~]# ifconfig lo:0
lo:0: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 192.168.88.15 netmask 255.255.255.255
loop txqueuelen 1000 (Local Loopback)
# 查看内核参数
[root@web1 ~]# sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
# 查看规则
[root@lvs1 ~]# 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.88.15:80 wlc
-> 192.168.88.100:80 Route 1 0 0
-> 192.168.88.200:80 Route 2 0 0