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
相关推荐
爱喝水的鱼丶11 分钟前
SAP-ABAP: Open SQL集合函数COUNT(统计行数)、SUM(数值求和)、AVG(平均值)、MAX/MIN(极值)深度指南
运维·数据库·sql·sap·报表·abap·程序
AI 嗯啦20 分钟前
linux的用户操作(详细介绍)
linux·运维·服务器
AOwhisky34 分钟前
云计算一阶段Ⅱ——12. SELinux 加固 Linux 安全
linux·安全·云计算
Ronin3051 小时前
【Linux系统】进程间通信:命名管道
linux·服务器·命名管道
东东今天敲代码了吗1 小时前
Ubuntu20.04 离线安装 FFmpeg 静态编译包
linux·运维·服务器·ubuntu·ffmpeg
The god of big data1 小时前
最新教程 | CentOS 7 下 MySQL 8 离线部署完整手册(含自动部署脚本)
linux·mysql·centos
LH_R1 小时前
OneTerm 开源堡垒机 | 历时三个月重构大更新
运维·后端·安全
老实巴交的麻匪1 小时前
(三)学习、实践、理解 CI/CD 与 DevOps:声明式 API,Docker Compose 容器编排
运维·敏捷开发·自动化运维
敢敢のwings1 小时前
Latent Action在具身智能中的使用
运维·chrome
mylove_北极甜虾1 小时前
OpenWebUI通过pipeline对接dify的workflow
运维·容器·dify·openwebui