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
相关推荐
好想打kuo碎21 分钟前
轻量安全的密码管理工具Vaultwarden
linux·安全·ubuntu
Mayer_WOT22 分钟前
Jetson Orin AGX Getting Start with Pytorch
linux
什么半岛铁盒1 小时前
Linux进程异常退出排查指南
linux·运维·服务器
wu~9701 小时前
计算机网络自定向下:第二章复习
服务器·网络·架构
Mylvzi1 小时前
Linux 性能利器:详解 `top` 命令的使用与输出信息解析
linux·服务器·网络
Ares-Wang1 小时前
负载均衡LB》》LVS
运维·负载均衡·lvs
斗转星移31 小时前
解决ubuntu20.04无法唤醒的问题的一种方法
linux·ubuntu·电脑
大连好光景2 小时前
沙箱&虚拟化技术&虚拟机&容器之间的关系详解
运维
饺子大魔王的男人2 小时前
Docker环境下FileRise私有云盘在飞牛NAS的部署与穿透实践
运维·docker·容器
lyh13442 小时前
【Ubuntu崩溃修复】
linux·运维·服务器