5

LVS负载均衡集群--DR模式部署

 2 years ago
source link: https://blog.51cto.com/u_15437542/5155820
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一.LVS-DR数据包流向分析

1.为方便进行原理分析,将client与群集机器放在同一网络中,数据包流经的路线为1-2-3-4

LVS负载均衡集群--DR模式部署_vim

2. client 向目标VIP发出请求,Director(负载均衡器)接收

3. Director根据负载均衡算法选择RealServer_1,不修改也不封装IP报文,而是将数据帧的MAC地址改为RealServer_1的MAC地址,然后在局域网上发送

4. RealServer_1收到这个帧,解封装后发现目标IP与本机匹配(RealServer事先绑定 了VIP​),于是处理这个报文。随后重新封装报文,发送到局域网

5. Client将收到回复报文。Client认为得到正常的服务,而不会知道是哪一台服务器处理的

(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 的请求报文的 MAC 地址是自身的 MAC 地址,就接收此报文。数据包重新封装报文(源 IP 地址为 VIP,目标 IP 为 CIP),将响应报文通过 lo 接口传送给物理网卡然后向外发出
(5)Real Server 直接将响应报文传送到客户端

注意: 如果跨网段,则报文通过路由器经由Internet返回给用户

二.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 地址

三.DR模式 LVS负载均衡群集部署

环境准备

DR 服务器:192.168.116.100
Web 服务器1:192.168.116.80
Web 服务器2:192.168.116.90
nfs 服务器: 192.168.116.60
客户端:192.168.116.123
vip:192.168.116.200

1.配置负载调度器(192.168.116.100)

systemctl stop firewalld.service
setenforce 0

modprobe ip_vs
cat /proc/net/ip_vs
yum -y install ipvsadm

(1)配置虚拟 IP 地址(VIP:192.168.116.200)
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens33:0 #若隧道模式,复制为ifcfg-tunl0
vim ifcfg-ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.116.200
NETMASK=255.255.255.255

ifup ens33:0
ifconfig ens33:0
route add -host 192.168.116.200 dev ens33:0

(2)调整 proc 响应参数
由于 LVS 负载调度器和各节点需要共用 VIP 地址,应该关闭Linux 内核的重定向参数响应。
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

sysctl -p

(3)配置负载分配策略
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm

ipvsadm -C
ipvsadm -A -t 192.168.116.200:80 -s rr
ipvsadm -a -t 192.168.116.200:80 -r 192.168.116.80:80 -g #若隧道模式,-g替换为-i
ipvsadm -a -t 192.168.116.200:80 -r 192.168.116.90:80 -g
ipvsadm

ipvsadm -ln #查看节点状态,Route代表 DR模式

LVS负载均衡集群--DR模式部署_服务器_02

LVS负载均衡集群--DR模式部署_封装_03

LVS负载均衡集群--DR模式部署_服务器_04

LVS负载均衡集群--DR模式部署_vim_05

LVS负载均衡集群--DR模式部署_vim_06

LVS负载均衡集群--DR模式部署_封装_07

2.部署共享存储(NFS服务器:192.168.116.60)

systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0

yum install nfs-utils rpcbind -y
systemctl start nfs.service
systemctl start rpcbind.service
systemctl enable nfs.service
systemctl enable rpcbind.service

mkdir /opt/accp /opt/benet
chmod 777 /opt/accp /opt/benet

vim /etc/exports
/usr/share *(ro,sync)
/opt/accp 192.168.116.0/24(rw,sync)
/opt/benet 192.168.116.0/24(rw,sync)

--发布共享---
exportfs -rv

LVS负载均衡集群--DR模式部署_封装_08

LVS负载均衡集群--DR模式部署_封装_09

LVS负载均衡集群--DR模式部署_vim_10

LVS负载均衡集群--DR模式部署_vim_11

LVS负载均衡集群--DR模式部署_服务器_12

LVS负载均衡集群--DR模式部署_封装_13

3.配置节点服务器(192.168.116.80、192.168.116.90)

systemctl stop firewalld.service
setenforce 0

(1)配置虚拟 IP 地址(VIP:192.168.116.200)
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-lo:0
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.116.200
NETMASK=255.255.255.255 #注意:子网掩码必须全为 1

ifup lo:0
ifconfig lo:0
route add -host 192.168.116.200 dev lo:0 #添加VIP本地访问路由,将访问VIP的数据限制在本地,以避免通信紊乱

vim /etc/rc.local
/sbin/route add -host 192.168.116.200 dev lo:0

chmod +x /etc/rc.d/rc.local

(2)调整 proc 响应参数
vim /etc/sysctl.conf
......
net.ipv4.conf.lo.arp_ignore = 1 #系统只响应目的IP为本地IP的ARP请求
net.ipv4.conf.lo.arp_announce = 2 #系统不使用IP包的源地址来设置ARP请求的源地址,而选择发送接口的IP地址
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
或者
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

sysctl -p

yum -y install nfs-utils rpcbind httpd
systemctl start rpcbind
systemctl start httpd

--192.168.116.80---
mount.nfs 192.168.116.60:/opt/accp /var/www/html

--192.168.116.90---
mount.nfs 192.168.116.60:/opt/benet /var/www/html

web节点1:

LVS负载均衡集群--DR模式部署_封装_14

LVS负载均衡集群--DR模式部署_vim_15

LVS负载均衡集群--DR模式部署_vim_16

LVS负载均衡集群--DR模式部署_vim_17

LVS负载均衡集群--DR模式部署_封装_18

LVS负载均衡集群--DR模式部署_服务器_19

LVS负载均衡集群--DR模式部署_服务器_20

LVS负载均衡集群--DR模式部署_封装_21

LVS负载均衡集群--DR模式部署_服务器_22

LVS负载均衡集群--DR模式部署_服务器_23

4.测试 LVS 群集

在客户端使用浏览器访问 http://192.168.116.200/,默认网关指向192.168.116.200

LVS负载均衡集群--DR模式部署_服务器_24

LVS负载均衡集群--DR模式部署_vim_25

LVS负载均衡集群--DR模式部署_服务器_26


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK