构建高可用性的LVS-DR群集:实现无缝的负载均衡与故障转移

发布于:2025-05-09 ⋅ 阅读:(20) ⋅ 点赞:(0)

目录

一、LVS-DR集群

1.LVS-DR工作原理

2.数据包流向分析

3.LVS-DR模式特点

二、直接路由模式(LVS-DR)

1.资源清单

2.配置负载调度器(lvs)

3.配置节点服务器(web1、web2)

4.测试LVS群集

5.使用NFS发布共享资源(nfs上)

6.在客户机中访问NFS共享资源


一、LVS-DR集群

1.LVS-DR工作原理

LVS-DR模式,Director Server作为群集的访问入口,不作为网关使用,节点Director Server 与Real Server需要在同一个网络中,返回给客户端的数据不需要经过Director Server 。为了响应对整个群集的访问,Director Server 与Real Server都需要配置VIP地址。

2.数据包流向分析

a.客户端发送请求到Director Server,请求的数据报文(源ip是CIP,目标IP是VIP)到达内核空间

b.Director Server 和Real Server在同一网络中,数据通过二层数据链路层来传输

c.内核空间判断数据包的目标IP是本机VIP,此时IPVS比对数据包请求的服务是否是集群服务,是集群服务就重新封装数据包。修改源MAC地址为Director Server的MAC地址,源IP地址与目标IP地址没有改变,然后将数据包发送给Real Server

d.到达Real Server 的请求报文的MAC地址是自身的MAC地址,就接收此报文。数据包重新封装报文(源IP地址为VIP,目标IP为CIP),将响应报文通过lo接口传送给物理网卡然后向外发出

e.Real Server 直接响应报文传送到客户端

3.LVS-DR模式特点

a.Director Server和Real Server必须在同一物理网络中

b.Real Server可以使用私有地址,也可以使用公网地址。如果使用公网地址,可以通过互联网对RIP进行直接访问

c.所有请求报文经由Director Server,但回复响应报文不能经过Director Server

d.Real Server的网关不允许指向Director Server IP,即不允许数据包经过Director Server

e.Real Server上的lo接口配置VIP的IP地址

二、直接路由模式(LVS-DR)

1.资源清单

主机

操作系统

IP地址

应用

lvs

OpenEuler24.03

192.168.16.142

Ipvsadm

web1

OpenEuler24.03

192.168.16.143

Apache、nfs、bind-utils

web2

OpenEuler24.03

192.168.16.144

Apache、nfs、bind-utils

nfs

OpenEuler24.03

192.168.16.145

nfs、bind-utils

2.配置负载调度器(lvs)

a.修改主机名

hostnamectl set-hostname lvs
hostnamectl set-hostname web1
hostnamectl set-hostname web2
hostnamectl set-hostname nfs

b.检查模块

# 加载 ip_vs 模块
modprobe ip_vs

# 查看 ip_vs 版本信息
cat /proc/net/ip_vs

c.安装ipvsadm管理工具

dnf -y install ipvsadm

d.配置虚拟IP地址(VIP)

cd /etc/sysconfig/network-scripts/

#修改配置文件
vi ifcfg-ens33:0

NAME=ens33:0
DEVICE=ens33:0
ONBOOT=yes
IPADDR=192.168.16.100
NETMASK=255.255.255.255

#重启网卡服务
systemctl restart NetworkManager

#查看是否生成vip
ip a

e.调整proc响应参数

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

sysctl -p

f.配置负载均衡分配策略

#ipvsadm -C #清空原有策略

ipvsadm -A -t 192.168.16.100:80 -s rr
ipvsadm -a -t 192.168.16.100:80 -r 192.168.16.143:80 -g -w 1
ipvsadm -a -t 192.168.16.100:80 -r 192.168.16.144:80 -g -w 1
#查看添加的策略
ipvsadm -ln
#保存配置
ipvsadm-save -n > /etc/sysconfig/ipvsadm
systemctl start ipvsadm

3.配置节点服务器(web1、web2)

a.配置虚拟IP地址(VIP)

        - 配置ifcfg-lo:0网卡信息

vi /etc/sysconfig/network-scripts/ifcfg-lo:0

DEVICE=lo:0
IPADDR=192.168.16.100 
NETMASK=255.255.255.255
ONBOOT=yes

        - 安装network服务

dnf install network-scripts -y
#停止networkManger服务
systemctl disable NetworkManager --now

#启动network服务
systemctl enable network --now
#安装此软件包,以便使用ifconfig
dnf -y install net-tools
#启动网卡
ifup lo:0

#查看VIP
ip a

        - 添加VIP本地访问路由

route add -host 192.168.16.100 dev lo:0
echo 'route add -host 192.168.16.100 dev lo:0' >> /etc/rc.local 
chmod +x /etc/rc.d/rc.local 

b.调整proc响应参数

cat >> /etc/sysctl.conf << EOF
# 忽略arp广播
net.ipv4.conf.all.arp_ignore = 1
# 匹配精确ip地址回包
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
EOF
sysctl -p

c.安装httpd,创建测试页

dnf -y install httpd

#创建测试网页
    #web1节点
echo "This is web1" > /var/www/html/index.html
    #web2节点
echo "This is web2" > /var/www/html/index.html

d.启用httpd服务程序

systemctl start httpd
systemctl enable httpd

4.测试LVS群集

C:\Users\Y>curl 192.168.16.100
This is web1

C:\Users\Y>curl 192.168.16.100
This is web2

5.使用NFS发布共享资源(nfs上)

a.安装nfs-utils、rpcbind软件包

dnf -y install rpcbind nfs-utils

b.设置共享目录

mkdir -p /opt/wwwroot

vi /etc/exports

/opt/wwwroot 192.168.16.0/24(rw,sync,no_root_squash)

c.启动NFS共享目录

systemctl start nfs rpcbind 
systemctl enable nfs rpcbind 

d.查看本机发布的NFS共享目录

showmount -e

6.在客户机中访问NFS共享资源

a.安装rpcbind软件包(web1、web2)

dnf -y install rpcbind nfs-utils

b.手动挂载NFS共享目录(web1、web2)

mount 192.168.16.145:/opt/wwwroot /var/www/html

c.NFS创建测试文件(nfs上)

ls /opt/wwwroot/
echo 'this is NFS' > /opt/wwwroot/index.html

d.fstab自动挂载设置(web1、web2)

vi /etc/fstab

192.168.16.145:/opt/wwwroot /var/www/html nfs defaults,_netdev 0 0

e.访问测试

C:\Users\Y>curl 192.168.16.100
this is NFS

C:\Users\Y>curl 192.168.16.100
this is NFS

C:\Users\Y>curl 192.168.16.100
this is NFS

网站公告

今日签到

点亮在社区的每一天
去签到