LVS--DR模式

目录

1 DR模式原理

2 DR模式请求回复过程

3 实验环境

4 开始实验

4.1 配置实验环境

4.2 Router 配置路由转发

4.3  LVS 设置转发规则

4.4 解决vip响应问题

4.5 Web1配置

4.6 Web2配置

5 测试效果


1 DR模式原理

  1. 当用户向负载均衡调度器(Director Server)发起请求,调度器将请求发往至内核空间

  2. PREROUTING链首先会接收到用户请求,判断目标IP确定是本机IP,将数据包发往INPUT链

  3. IPVS是工作在INPUT链上的,当用户请求到达INPUT时,IPVS会将用户请求和自己已定义好的集群服务进行比对,如果用户请求的就是定义的集群服务,那么此时IPVS会强行修改数据包里的目标IP地址及端口,并将新的数据包发往POSTROUTING链

  4. POSTROUTING链接收数据包后发现目标IP地址刚好是自己的后端服务器,那么此时通过选路,将数据包最终发送给后端的服务器

2 DR模式请求回复过程

1、客户端向目标VIP发送请求:客户端发起HTTP/HTTPS等协议的请求,目标地址为虚拟IP(VIP)。

2、负载均衡器接收并处理请求:负载均衡器接收到请求后,会根据预设的负载均衡策略(如轮询、权重、最少连接数等),选择一个合适的后端服务器进行转发。

3、数据包转发至后端服务器:负载均衡器不会更改IP报文的内容,而是将数据包的目的MAC地址更改为所选后端服务器的MAC地址,然后将数据包发送到后端服务器所在的网络。

4、后端服务器处理请求和返回响应:后端服务器接收到数据包后,检查其目的IP地址是否与自己绑定的VIP相同,如果是,则处理该请求。将响应报文通过lo接口传送给物理网卡然后向外发出。处理完成后,后端服务器将响应数据包直接发回给客户端

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

3 实验环境

序号                        源                                目的
1

192.168.239.200/24

mac:client

172.25.254.200/24

mac:Router-eth0

2

192.168.239.200/24

mac:Router-eth1

172.25.254.200/24

mac:LVS

3

192.168.239.200/24

mac:LVS

172.25.254.200/24

mac:web1

4

172.25.254.200/24

mac:web1-eth0

172.25.254.100/24

mac:Router-eth1

5

172.25.254.200/24

mac:Router-eth0

172.25.254.100/24

mac:client

4 开始实验

4.1 配置实验环境

# Client
[root@client ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.239.200/24,192.168.239.100
method=manual


# Router
[root@router ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=192.168.239.100/24
method=manual
[root@router ~]# cat /etc/NetworkManager/system-connections/eth1.nmconnection 
[connection]
id=eth1
type=ethernet
interface-name=eth1

[ipv4]
address1=172.25.254.100/24
method=manual


# LVS 配置
[root@LVS ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection 
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.254.50/24,172.25.254.100
method=manual


# web1
[root@web1 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.254.10/24,172.25.254.100
method=manual


# web2
[root@web2 ~]# cat /etc/NetworkManager/system-connections/eth0.nmconnection
[connection]
id=eth0
type=ethernet
interface-name=eth0

[ipv4]
address1=172.25.254.20/24,172.25.254.100
method=manual

4.2 Router 配置路由转发

# 虚拟机本身是不开启路由功能的,要想开启请在内核中开启路由转发功能

[root@mysql-03 test]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
net.ipv4.ip_forward_update_priority = 1
net.ipv4.ip_forward_use_pmtu = 0

[root@mysql-03 test]# echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf
[root@router ~]# sysctl -p
net.ipv4.ip_forward = 1

4.3  LVS 设置转发规则

# 给环回接口填上
[root@LVS test]# ip addr add 172.25.254.200/32 dev lo

# 设置转发规则
[root@LVS test]# ipvsadm -A -t 172.25.254.200:80 -s wrr
[root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.10 -w 1 -g
[root@LVS test]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.20 -w 1 -g

# 查看规则
[root@LVS ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.25.254.200:80 wrr
  -> 172.25.254.10:80             Route   1      0          5         
  -> 172.25.254.20:80             Route   1      0          6 

4.4 解决vip响应问题

DR 模型中各主机上均需要配置 VIP ,解决地址冲突的方式有三种:
  • (1)在前端网关做静态绑定
  • (2)在各RS使用arptables
  • (3)在各RS修改内核参数,来限制arp响应和通告的级别
限制响应级别 :arp_ignore
  • 0:默认值,表示可使用本地任意接口上配置的任意地址进行响应
  • 1:仅在请求的目标IP配置在本地主机的接收到请求报文的接口上时,才给予响应
限制通告级别 :arp_announce
  • 0:默认值,把本机所有接口的所有信息向每个接口的网络进行通告
  • 1:尽量避免将接口信息向非直接连接网络进行通告
  • 2:必须避免将接口信息向非本网络进行通告

4.5 Web1配置

# 增加VIP
[root@web1 test]# ip addr add 172.25.254.200/32 dev lo

# 查看网关是否指向路由器
[root@web1 test]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.254.100  0.0.0.0         UG    100    0        0 eth0
172.25.254.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0



[root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@web1 test]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web1 test]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore

4.6 Web2配置

# 增加VIP
[root@web2 test]# ip addr add 172.25.254.200/32 dev lo

# 查看网关是否指向路由器
[root@web2 test]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.25.254.100  0.0.0.0         UG    100    0        0 eth0
172.25.254.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0



[root@web2 test]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@web2 test]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@web2 test]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@web2 test]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore


5 测试效果

[root@client ~]# for i in {1..10}
> do
> curl 172.25.254.200
> done
this is web1
this is web2
this is web1
this is web2
this is web1
this is web2
this is web1
this is web2
this is web1
this is web2

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妍妍的宝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值