LVS使用防火墙标记解决轮询调度问题

目录

[1 防火墙标签解决轮询错误](#1 防火墙标签解决轮询错误)

[1.1 轮询规则中可能会遇到的错误](#1.1 轮询规则中可能会遇到的错误)

[1.2 测试效果](#1.2 测试效果)

[2 防火墙标记解决轮询调度问题](#2 防火墙标记解决轮询调度问题)

[2.1 使用iptables 在LVS主机打标记:](#2.1 使用iptables 在LVS主机打标记:)

[2.1.1 格式](#2.1.1 格式)

[2.1.2 实践](#2.1.2 实践)

[2.2 LVS中定义集群转发的服务](#2.2 LVS中定义集群转发的服务)

[2.2.1 格式](#2.2.1 格式)

[2.2.2 添加LVS转发规则](#2.2.2 添加LVS转发规则)

[2.2.3 测试结果](#2.2.3 测试结果)


1 防火墙标签解决轮询错误

1.1 轮询规则中可能会遇到的错误

以 http 和 https 为例,当我们在 RS 中同时开放 80 和 443 端口,那么默认控制是分开轮询的,这样我们就出现了一个轮询错乱的问题
第一次访问 80 被轮询到 web1 后下次访问 443 仍然可能会被轮询到 web1 上
问题如下

bash 复制代码
[root@LVS ~]# ipvsadm -A -t 172.25.254.200:80 -s wrr
[root@LVS ~]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.10:80 -g -w 1
[root@LVS ~]# ipvsadm -a -t 172.25.254.200:80 -r 172.25.254.20:80 -g -w 1

[root@LVS ~]# ipvsadm -A -t 172.25.254.200:443 -s wrr
[root@LVS ~]# ipvsadm -a -t 172.25.254.200:443 -r 172.25.254.10:443 -g -w 1
[root@LVS ~]# ipvsadm -a -t 172.25.254.200:443 -r 172.25.254.20:443 -g -w 1


[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 0
-> 172.25.254.20:80 Route 1 0 0
TCP 192.168.0.100:443 wrr
-> 172.25.254.10:443 Route 1 0 0
-> 172.25.254.20:443 Route 1 0 0

1.2 测试效果

bash 复制代码
[root@client yum.repos.d]# curl 172.25.254.200;curl -k https://172.25.254.200
this is web1
this is web1

2 防火墙标记解决轮询调度问题

FWM:FireWall Mark
MARK target 可用于给特定的报文打标记 , --set-mark value value为标记的值
其中 : value 可为0xffff格式 ,表示十六进制数字借助于防火墙标记来分类报文,而后基于标记定义集群服务: 可将多个不同的应用使用同一个集群服务进行调度
实现方法 :

2.1 使用iptables 在LVS主机打标记:

2.1.1 格式

bash 复制代码
iptables -t mangle -A PREROUTING -d $vip -p $proto -m multiport --dports $portl,$port2,.. -i MARK --set-mark NUMBER

2.1.2 实践

bash 复制代码
[root@LVS ~]# iptables  -t mangle -A PREROUTING -d 172.25.254.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 6666
[root@LVS ~]# iptables -nL -t mangle
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
MARK       6    --  0.0.0.0/0            172.25.254.200       multiport dports 80,443 MARK set 0x1a0a

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination  
  1. **-t mangle:**指定操作的目标表为mangle表。mangle表主要用于修改数据包的TTL、TOS字段以及做标记等操作。

  2. -A PREROUTING:将规则追加到PREROUTING链。PREROUTING链是在数据包进入系统之前执行的一系列规则。

  3. **-d 172.25.254.200:**指定目标地址为172.25.254.200。这意味着只有发往这个地址的数据包才会受到这个规则的影响。

  4. **-p tcp:**指定协议类型为TCP。只有TCP协议的数据包会被这个规则影响。

  5. -m multiport --dports 80,443:使用multiport模块,并指明目标端口为80和443。这意味着只有发往这两个端口的TCP数据包才会受到这个规则的影响。

  6. -j MARK --set-mark 6666 执行MARK动作,并设置标记值为6666。这个标记将会被附加到符合以上条件的所有数据包上。

2.2 LVS中定义集群转发的服务

2.2.1 格式

bash 复制代码
ipvsadm -A -f 标记值 [options]

2.2.2 添加LVS转发规则

在vs调度器中设定端口标签,人为80和443是一个整体

bash 复制代码
[root@LVS ~]# ipvsadm -A -f 6666 -s rr
[root@LVS ~]# ipvsadm -a -f 6666 -r 172.25.254.10 -g
[root@LVS ~]# ipvsadm -a -f 6666 -r 172.25.254.20 -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
FWM  6666 rr
  -> 172.25.254.10:0              Route   1      0          0         
  -> 172.25.254.20:0              Route   1      0          0 

2.2.3 测试结果

bash 复制代码
[root@client ~]# for i in {1..10};do curl http://172.25.254.200;curl -k https://172.25.254.200 ;done
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
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
this is web1
相关推荐
R.X. NLOS3 分钟前
VS Code远程开发新方案:使用SFTP扩展解决Remote-SSH连接不稳定问题
运维·服务器·ssh·debug·vs code
cuijiecheng201832 分钟前
Ubuntu下布署mediasoup-demo
linux·运维·ubuntu
独行soc2 小时前
2025年渗透测试面试题总结-2025年HW(护网面试) 33(题目+回答)
linux·科技·安全·网络安全·面试·职场和发展·护网
java龙王*3 小时前
开放端口,开通数据库连接权限,无法连接远程数据库 解决方案
linux
bcbobo21cn3 小时前
Linux命令的命令历史
linux·histsize·histfile
轩情吖4 小时前
Qt的第一个程序(2)
服务器·数据库·qt·qt creator·qlineedit·hello world·编辑框
jingyu飞鸟4 小时前
linux系统源代码安装apache、编译隐藏版本号
linux·运维·apache
世事如云有卷舒4 小时前
Ubunt20.04搭建GitLab服务器,并借助cpolar实现公网访问
linux·服务器·gitlab
Little-Hu5 小时前
QML TextEdit组件
java·服务器·数据库
riverz12275 小时前
TCP backlog工作机制
服务器·网络·tcp/ip