LVS负载均衡之DR模式

1 数据包流向分析

(1)客户端发送请求到Director Server(负载均衡器),请求的数据报文(源IP是CIP,目标IP 是VIP)到达内核空间。
(2)Director server 和 Real Server在同一个网络中,数据通过二层数据链路层来传输。
(3)内核空间判断数据包的目标IP是本机VIP,此时IPVS(IP虚拟服务器)比对数据包请求的服务是否是集群服务,是集群服务就重新封装数据包。修改源MAc地址为 Director Server的MAC地址,修改目标MAC地址为Real Server 的MAC地址,源IP 地址与目标IP地址没有改变,然后将数据包发送给Real server。
(4)到达Real Server的请求报文的NAc 地址是自身的wAc 地址,就接收此报文。数据包重新封装报文(源ⅠP地址为VIP,目标 IP 为CIP),将响应报文通过lo 接口传送给物理网卡然后向外发出。
(5) Real server直接将l响应报文传送到客户端。

2 DR模式的特点

(1) Director server 和 Real Server必须在同一个物理网络中。
(2)Real Server 可以使用私有地址,也可以使用公网地址。如果使用公网地址,可以通过互联网对RIP进行直接访问。
(3) Director Server作为群集的访问入口,但不作为网关使用。
(4)所有的请求报文经由 Director Server,但回复响应报文不能经过 Director Server
(5) Real Server的网关不允许指向 Director Server IP,即Real Server发送的数据包不允许经过Director Server
(6) Real server 上的lo接口配置VIP的IP地址。

3 LVS-DR中的ARP问题

IP地址冲突
在LVS-DR负载均衡集群中,负载均衡器与节点服务器都要配置相同的VIP地址,在局域网中具有相同的IP地址。在局域网中具有相同的地址,势必会造成各服务器ARP通信的紊乱

  • 当ARP广播发送到LVS-DR集群时,因为负载均衡器和节点服务器都是连接到相同的网络上,它们都会接收到ARP广播
  • 只有前端的负载均衡器器进行响应,其他节点服务器不应该响应ARP广播
    解决办法
    对节点服务器进行处理,使其不响应VIP的ARP请求
  • 用虚接口lo:0承载VIP地址
  • 设置内核参数arp_ ignore=1: 系统只响应目的IP为本地IP的ARP请求
    设置完后节点服务器则不会去响应ARP广播,而调度器则仍然会响应ARP所以只能解析到调度器的MAC地址
    路由根据ARP表项,会将新来的请求报文转发给RealServer,导致Director的VIP失效
  • RealServer返回报文(源IP是VIP)经路由器转发,重新封装报文时,需要先获取路由器的MAC地址,
  • 发送ARP请求时,Linux默认使用IP包的源IP地址(即VIP)作为ARP请求包中的源IP地址,而不使用发送接口的IP地址,
  • 路由器收到ARP请求后,将更新ARP表项,原有的VIP对应Director的MAC地址会被更新为VIP对应RealServer的MAC地址。
  • 路由器根据ARP表项,会将新来的请求报文转发给RealServer,导致Director的VIP失效
    解决方法
    对节点服务器进行处理
  • 设置内核参数arp_announce=2
  • 系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
    两个问题的设置方法
    修改/etc/sysctl.conf文件
节点服务器在lo:0虚接口上承载VIP地址
net.ipv4.conf.lo.arp_ignore = 1		  #使本机系统只响应目的IP为本地物理网卡IP的ARP请求
net.ipv4.conf.lo.arp_announce = 2     #本机系统不使用返回数据包的源地址作为ARP请求报文的源地址,而采用发送接口的IP作为ARP请求报文源地址
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

4 部署DR模式

调度服务器192.168.132.22
NFS服务器192.168.132.11
web1服务器192.168.132.56
web2服务器192.168.132.9
客户端192.168.132.200
VIP192.168.132.188

192168.132.22调度服务器

root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install ipvsadm -y

在这里插入图片描述
配置虚拟IP地址
在这里插入图片描述
在这里插入图片描述
重启网卡

[root@localhost network-scripts]# ifdown ifcfg-ens33:0
[root@localhost network-scripts]# ifup ifcfg-ens33:0

在这里插入图片描述
调整proc响应参数

[root@localhost network-scripts]# 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

在这里插入图片描述
刷新配置

[root@localhost network-scripts]# sysctl -p
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

在这里插入图片描述
加载模块

[root@localhost network-scripts]# modprobe ip_vs
[root@localhost network-scripts]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn

在这里插入图片描述
配置负载分配策略,启动服务

[root@localhost network-scripts]# ipvsadm-save >/etc/sysconfig/ipvsadm
[root@localhost network-scripts]# systemctl start ipvsadm.service

在这里插入图片描述
清空ipvsadm

[root@localhost network-scripts]# ipvsadm -C

添加策略
[root@localhost network-scripts]# ipvsadm -A -t 192.168.132.188:80 -s rr
[root@localhost network-scripts]# ipvsadm -a -t 192.168.132.188:80 -r 192.168.132.56:80 -g
[root@localhost network-scripts]# ipvsadm -a -t 192.168.132.188:80 -r 192.168.132.9:80 -g在这里插入图片描述
保存设置

[root@localhost network-scripts]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  localhost.localdomain:http rr
  -> 192.168.132.56:http          Route   1      0          0         
  -> 192.168.132.9:http          Route   1      0          0         
[root@localhost network-scripts]# 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.132.188:80 rr
  -> 192.168.132.56:80            Route   1      0          0         
  -> 192.168.132.9:80            Route   1      0          0         
[root@localhost network-scripts]# ipvsadm-save >/etc/sysconfig/ipvsadm

在这里插入图片描述
192.168.132.11 NFS服务器
在这里插入图片描述

[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh
[root@localhost opt]# mkdir nfs
[root@localhost opt]# cd nfs/
[root@localhost nfs]# mkdir smr xu
[root@localhost nfs]# ls
smr xu

[root@localhost nfs]# echo "this is smr" >smr/index.html
[root@localhost nfs]# echo "this is xu" >xu/index.html
[root@localhost nfs]# 

在这里插入图片描述
设置权限

[root@localhost nfs]# chmod 777 *
[root@localhost nfs]# ll
总用量 0

在这里插入图片描述
设置共享策略

[root@localhost nfs]# vim /etc/exports

/opt/nfs/my 192.168.132.0/24(rw,sync,no_root_squash)
/opt/nfs/qyd 192.168.132.0/24(rw,sync,no_root_squash)

在这里插入图片描述
开启服务发布共享

[root@localhost nfs]# systemctl start rpcbind
[root@localhost nfs]# systemctl start nfs
[root@localhost nfs]# showmount -e
Export list for localhost.localdomain:
/opt/nginx/smr 192.168.132.0/24
/opt/nginx/xu  192.168.132.0/24

在这里插入图片描述
192.168.132.56 web1服务器
关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# showmount -e 192.168.132.11
Export list for 192.168.132.11:
/opt/nfs/smr 192.168.132.0/24
/opt/nfs/xu  192.168.132.0/24

在这里插入图片描述
安装apache服务

[root@localhost ~]# yum install httpd -y

挂载
在这里插入图片描述
重启服务并输入IP地址查看

[root@localhost html]# systemctl restart httpd.service

在这里插入图片描述
配置网关
在这里插入图片描述
重启网卡

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig

在这里插入图片描述
在这里插入图片描述
设置路由

[root@localhost network-scripts]# route add -host 192.168.132.188 dev lo:0
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.132.2    0.0.0.0         UG    100    0        0 ens33
192.168.132.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.132.188  0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.132.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

在这里插入图片描述
开机执行命令

[root@localhost network-scripts]# vim /etc/rc.d/rc.local 

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#

/usr/sbin/route add -host 192.168.132.188 dev lo:0
[root@localhost network-scripts]# chmod +x /etc/rc.d/rc.local

在这里插入图片描述
在这里插入图片描述
调整proc响应参数

[root@localhost network-scripts]# vim /etc/sysctl.conf

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
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

在这里插入图片描述
在这里插入图片描述
192.168.132.9 web2服务器
关闭防火墙

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

在这里插入图片描述

[root@localhost ~]# showmount -e 192.168.132.11
Export list for 192.168.132.11:
/opt/nfs/smr 192.168.132.0/24
/opt/nfs/xu  192.168.132.0/24

安装apache服务

[root@localhost ~]# yum install httpd -y

挂载
在这里插入图片描述
重启服务并输入IP地址查看

[root@localhost html]# systemctl restart httpd.service 

在这里插入图片描述
配置网关

[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-ens33  ifdown-ipv6    ifdown-TeamPort  ifup-ippp   ifup-routes       network-functions
ifcfg-lo     ifdown-isdn    ifdown-tunnel    ifup-ipv6   ifup-sit          network-functions-ipv6
ifdown       ifdown-post    ifup             ifup-isdn   ifup-Team
ifdown-bnep  ifdown-ppp     ifup-aliases     ifup-plip   ifup-TeamPort
ifdown-eth   ifdown-routes  ifup-bnep        ifup-plusb  ifup-tunnel
ifdown-ib    ifdown-sit     ifup-eth         ifup-post   ifup-wireless
ifdown-ippp  ifdown-Team    ifup-ib          ifup-ppp    init.ipv6-global
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0
IPADDR=192.168.132.188
NETMASK=255.255.255.255

在这里插入图片描述
在这里插入图片描述
重启服务并查看

[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig

在这里插入图片描述
设置路由

[root@localhost network-scripts]# route add -host 192.168.132.188 dev lo:0
[root@localhost network-scripts]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.132.2    0.0.0.0         UG    100    0        0 ens33
192.168.122.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.132.188  0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.132.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

在这里插入图片描述
开机执行命令

[root@localhost network-scripts]# vim /etc/rc.d/rc.local 

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#

/usr/sbin/route add -host 192.168.132.188 dev lo:0
[root@localhost network-scripts]# chmod +x /etc/rc.d/rc.local

在这里插入图片描述
在这里插入图片描述
调整proc响应参数

[root@localhost network-scripts]# vim /etc/sysctl.conf

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
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

在这里插入图片描述
在这里插入图片描述
客户机测试192.168.132.200
在这里插入图片描述
在这里插入图片描述
浏览器输入回环lo:0VIP地址:192.168.132.188
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值