负载均衡 ---- DR模式
负载均衡中的DR模式
直接路由(Direct Routing)简称 DR模式;采用半开放式的网络结构,与TUN模式的结构类似,但各节点并不是分散在各地,而是与调度器位于同一物理网络;负载调度器与各节点服务器通过本地网络连接,不需要建立专用的IP隧道。
LVS-DR中存在的ARP问题
在LVS-DR负载均衡集群中,负载均衡器与节点服务器都要配置相同的VIP地址
在局域网中具有相同的IP地址,势必会造成各服务器ARP通信的紊乱
当一个ARP广播发送到LVS-DR集群时,因为负载均衡器和节点服务器都是连接到相同的网络上的,它们都会接收到ARP广播
此时只有前端的负载均衡器进行响应,其他节点服务器不应该响应ARP广播
对节点服务器进行处理,使其不响应针对VIP的ARP请求
使用虚接口lo:0承载ⅥP地址
设置内核参数 arp_ignore=1:系统只响应目的IP为本地IP的ARP请求
RealServe返回报文(源IP是VIP)经路由器转发,在重新封装报文时需要先获取路由器的MAC地址
发送ARP请求时, Linux默认使用IP包的源IP地址(即VIP)作为ARP请求包中的源IP地址,而不使用发送接口(例如ens33)的IP地址
路由器收到ARP请求后,将更新ARP表项
原有的VIP对应 Director的MAC地址会被更新为ⅥP对应RealServer的MAC地址
此时新来的请求报文,路由器根据ARP表项,会将该报文转发给RealServer,从而导致 Director的VIP失效
网络环境部署
-
节点服务器群集
1、一台LVS调度器(vm1连接方式)
ip :192.168.100.11/24 网关:192.168.100.1 ;2、两台节点服务器(vm1连接方式)
ip :192.168.100.12/24 网关:192.168.100.1
ip :192.168.100.13/24 网关:192.168.100.13、一台NFS共享存储器(vm1连接方式)
ip :192.168.100.14/24 网关:192.168.100.1 ; -
客户机
1、一台 windows 系统的虚拟机(客户端)(vm2连接方式)
更改网络适配器的网址 ip:192.168.200.100/24 网关:192.168.200.1 -
关闭所有虚拟机的防火墙、核心防护、安装本地 yum源
开始配置 LVS — DR模式
配置 LVS调度器(192.168.100.11)
1、配置虚拟IP地址(VIP)
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ll
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens33:0
[root@localhost network-scripts]# vi ifcfg-ens33:0
NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.100.100
NETMASK=255.255.255.255
[root@localhost network-scripts]# ifup ens33:0
[root@localhost network-scripts]# ip addr ####查看 lo:0 接口
2、调整/proc响应参数
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p ####查看是否生效
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
3、配置负载分配策略
[root@localhost /]# ipvsadm -v
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# cat /proc/net/ip_vs
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# ipvsadm -A -t 192.168.100.100:80 -s rr #### 可以 ipvsadm -ln 查看下
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.12:80 -g -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.100.100:80 -r 192.168.100.13:80 -g -w 1
[root@localhost ~]# ipvsadm-save > /opt/ipvsadm
[root@localhost ~]# 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.100.100:80 rr
-> 192.168.100.12:80 Route 1 0 0
-> 192.168.100.13:80 Route 1 0 0
配置 NFS共享存储(192.168.100.14)
rpm -q nfs-utils ###如果没装,yum -y install nfs-utils
rpm -q rpcbind ###如果没装,yum -y install rpcbind
[root@localhost ~]# mkdir /opt/51xit /opt/52xit
[root@localhost ~]# echo 'this is 51xit' > /opt/51xit/index.html
[root@localhost ~]# echo 'this is 52xit' > /opt/52xit/index.html
[root@localhost ~]# vi /etc/exports #####添加下面二行(共享出去)
/opt/51xit 192.168.100.0/24 (rw,sync)
/opt/52xit 192.168.100.0/24 (rw,sync)
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
配置 节点服务器(192.168.100.12)
【1】配置虚拟IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ip addr ####我是最小化安装
看看 lo:0 接口上设的IP是否出现
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.100 dev lo:0 ####在末尾添加
[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
( -bash: route: command not found #### 出现这个报错就 yum -y install net-tools 安装这个工具)
123456789101112131415161718
【2】调整/proc响应参数
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p ####查看是否生效
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
12345678910111213141516
【3】安装httpd 挂载测试页
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount 192.168.100.14:/opt/51xit /var/www/html/
[root@localhost ~]# df -Th ####查看挂载情况
[root@localhost ~]# vi /etc/fstab
192.168.100.14:/opt/51xit /var/www/html nfs defaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# showmount -e 192.168.100.14
Export list for 192.168.100.14:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
l浏览器登录192.168.100.22测试网站是否出现 this is 51xit
配置 节点服务器(192.168.100.13)
【1】配置虚拟IP地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.100.100
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# ifup lo:0
[root@localhost network-scripts]# ip addr
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.100.100 dev lo:0 ####在末尾添加
[root@localhost network-scripts]# route add -host 192.168.100.100 dev lo:0
【2】调整/proc响应参数
[root@localhost network-scripts]# vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
[root@localhost network-scripts]# sysctl -p ####查看是否生效
【3】安装httpd 挂载测试页
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# mount 192.168.100.14:/opt/52xit /var/www/html/
[root@localhost ~]# df -Th ####查看挂载情况
[root@localhost ~]# vi /etc/fstab
192.168.100.14:/opt/52xit /var/www/html nfs defaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# showmount -e 192.168.100.14
Export list for 192.168.100.14:
/opt/52xit 192.168.100.0/24
/opt/51xit 192.168.100.0/24
浏览器登录192.168.100.23测试网站是否出现 this is 52xit
测试 LVS 负载均衡
刷新网页可靠是否轮询出现 this is 51xit this is 52xit
这些都正确了说明实验成功