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
相关推荐
落笔画忧愁e21 分钟前
扣子Coze飞书多维表插件添加数据记录
java·服务器·飞书
GuokLiu3 小时前
250708-Debian系统安装Edge浏览器并配置最小中文输入法
运维·edge·debian
Two_brushes.3 小时前
【linux 网络】网络基础
linux·网络
Code Warrior3 小时前
【Linux】基础开发工具(3)
linux·服务器
鬼才血脉4 小时前
Linux(centos)安装 MySQL 8
linux·mysql·centos
guygg884 小时前
ubuntu手动编译VTK9.3 Generating qmltypes file 失败
linux·运维·ubuntu
JeffersonZU4 小时前
Linux/Unix 套接字Socket编程(socket基本概念,流程,流式/数据报socket,Unix domain socket示例)
linux·c语言·tcp/ip·udp·unix·gnu
先做个垃圾出来………5 小时前
自动化一次通过率
运维·自动化
Two_brushes.5 小时前
【linux网络】网络编程全流程详解:从套接字基础到 UDP/TCP 通信实战
linux·开发语言·网络·tcp/udp
夕泠爱吃糖5 小时前
Linux中的静态库和动态库
linux·运维·服务器