Bootstrap

LVS负载均衡群集——DR模式

目录

1、数据包流向分析

2、DR 模式的特点:

3、LVS-DR中的ARP问题

4、部署DR模式

192168.52.140调度服务器

关闭防火墙与selinux,下载ipvsadm

配置虚拟IP地址

调整proc响应参数

加载模块

 配置负载分配策略,启动服务

添加策略

保存设置

192.168.52.110NFS服务器

设置权限

设置共享策略

 开启服务发布共享

192.168.52120web1服务器

 安装apache服务

挂载

 重启服务并输入IP地址查看

配置网关

 重启网卡

设置路由

 开机执行命令

 调整proc响应参数

192.168.52.130web2服务器

 安装apache服务

挂载

重启服务并输入IP地址查看

 配置网关

重启服务并查看

设置路由

开机执行命令

 调整proc响应参数

客户机测试192.168.52.102


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.52.140

NFS服务器192.168.52.110

web1服务器192.168.52.120

web2服务器192.168.52.130

客户端192.168.52.102

VIP192.168.52.188

192168.52.140调度服务器

关闭防火墙与selinux,下载ipvsadm

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

 

配置虚拟IP地址

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

DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.52.188
NETMASK=255.255.255.0
#GATEWAY=192.168.52.2
#DNS1=8.8.8.8

 重启网卡

[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.52.188:80 -s rr
[root@localhost network-scripts]# ipvsadm -a -t 192.168.52.188:80 -r 192.168.52.120:80 -g
[root@localhost network-scripts]# ipvsadm -a -t 192.168.52.188:80 -r 192.168.52.130: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.52.120:http          Route   1      0          0         
  -> 192.168.52.130: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.52.188:80 rr
  -> 192.168.52.120:80            Route   1      0          0         
  -> 192.168.52.130:80            Route   1      0          0         
[root@localhost network-scripts]# ipvsadm-save >/etc/sysconfig/ipvsadm

192.168.52.110NFS服务器

[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh
[root@localhost opt]# mkdir nfs
[root@localhost opt]# cd nfs/
[root@localhost nfs]# mkdir my qyd
[root@localhost nfs]# ls
my  qyd
[root@localhost nfs]# echo "this is my" >my/index.html
[root@localhost nfs]# echo "this is qyd" >qyd/index.html
[root@localhost nfs]# 

设置权限

[root@localhost nfs]# chmod 777 *
[root@localhost nfs]# ll
总用量 0
drwxrwxrwx. 2 root root 24 5月  13 16:30 my
drwxrwxrwx. 2 root root 24 5月  13 16:30 qyd

 

设置共享策略

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

/opt/nfs/my 192.168.52.0/24(rw,sync,no_root_squash)
/opt/nfs/qyd 192.168.52.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/qyd 192.168.52.0/24
/opt/nginx/my  192.168.52.0/24

192.168.52.120web1服务器

关闭防火墙

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

[root@localhost ~]# showmount -e 192.168.52.110
Export list for 192.168.52.110:
/opt/nfs/qyd 192.168.52.0/24
/opt/nfs/my  192.168.52.0/24

 安装apache服务

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

挂载

[root@localhost ~]# mount 192.168.52.110:/opt/nfs/my /var/www/html/
[root@localhost ~]# df -h
文件系统                    容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root      20G  3.7G   17G   19% /
devtmpfs                    473M     0  473M    0% /dev
tmpfs                       489M     0  489M    0% /dev/shm
tmpfs                       489M  7.2M  481M    2% /run
tmpfs                       489M     0  489M    0% /sys/fs/cgroup
/dev/sda1                  1014M  161M  854M   16% /boot
tmpfs                        98M     0   98M    0% /run/user/0
tmpfs                        98M   12K   98M    1% /run/user/42
192.168.52.110:/opt/nfs/my   10G  3.7G  6.4G   37% /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# cat index.html 
this is my

 重启服务并输入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.52.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.52.2    0.0.0.0         UG    100    0        0 ens33
192.168.52.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.52.188  0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.122.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.52.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.52.130web2服务器

 关闭防火墙

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

[root@localhost ~]# showmount -e 192.168.52.110
Export list for 192.168.52.110:
/opt/nfs/qyd 192.168.52.0/24
/opt/nfs/my  192.168.52.0/24

 安装apache服务

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

挂载

[root@localhost ~]# mount 192.168.52.110:/opt/nfs/qyd /var/www/html/
[root@localhost ~]# df -h
文件系统                     容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root       15G  3.7G   12G   25% /
devtmpfs                     897M     0  897M    0% /dev
tmpfs                        912M     0  912M    0% /dev/shm
tmpfs                        912M  9.1M  903M    1% /run
tmpfs                        912M     0  912M    0% /sys/fs/cgroup
/dev/sda1                    497M  172M  326M   35% /boot
tmpfs                        183M  4.0K  183M    1% /run/user/42
tmpfs                        183M   44K  183M    1% /run/user/0
192.168.52.110:/opt/nfs/qyd   10G  3.7G  6.4G   37% /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# cat index.html 
this is qyd

重启服务并输入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.52.118
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.52.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.52.2    0.0.0.0         UG    100    0        0 ens33
192.168.52.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.52.188  0.0.0.0         255.255.255.255 UH    0      0        0 lo
192.168.122.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.52.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.52.102

浏览器输入回环lo:0VIP地址:192.168.52.188

;