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
相关推荐
chennalC#c.h.JA Ptho27 分钟前
lubuntu 系统详解
linux·经验分享·笔记·系统架构·系统安全
冼紫菜28 分钟前
解决 CentOS 7 镜像源无法访问的问题
linux·运维·服务器·centos
几道之旅30 分钟前
分别在windows和linux上使用curl,有啥区别?
linux·运维·windows
季柳东31 分钟前
在虚拟机Ubuntu18.04中安装NS2教程及应用
linux·运维·ubuntu
冼紫菜34 分钟前
如何在 CentOS 7 虚拟机上配置静态 IP 地址并保持重启后 SSH 连接
linux·开发语言·centos·ssh
christine-rr1 小时前
【25软考网工】第六章(4)VPN虚拟专用网 L2TP、PPTP、PPP认证方式;IPSec、GRE
运维·网络·网络协议·网络工程师·ip·软考·考试
oioihoii1 小时前
C++23 views::slide (P2442R1) 深入解析
linux·算法·c++23
乐言3611 小时前
如何用Jmeter实现自动化测试?
运维·jmeter·自动化
hnlucky2 小时前
《基于 Kubernetes 的 WordPress 高可用部署实践:从 MariaDB 到 Nginx 反向代理》
运维·数据库·nginx·云原生·容器·kubernetes·mariadb
Jerry&Louis2 小时前
【Ubuntu】neovim & Lazyvim安装与卸载
linux·ubuntu