Linux实战:HAProxy全方位指南

一、负载均衡核心概念

1.1 负载均衡定义
负载均衡(Load Balance,简称LB)是一种基于硬件设备或软件服务的高可用反向代理技术。它将特定业务(如Web服务、网络流量)分发到后端的一个或多个服务器/设备,从而提升业务并发处理能力、保障高可用性,并支持业务水平动态扩展。

阿里云SLB介绍 :SLB技术原理浅析-阿里云开发者社区

1.2 负载均衡的核心价值

  • 动态水平扩展:Web服务器扩容对用户无感知

  • 突破单点瓶颈:提升业务并发处理能力

  • 节约公网IP:降低IT成本支出

  • 隐藏服务器IP:增强内部服务器安全性

  • 配置简单:固定格式的配置文件

  • 功能丰富:支持四层/七层代理、动态下线主机

  • 高性能:支撑数万至数十万并发

1.3 负载均衡类型
1.3.1 硬件负载均衡

  • 、F5(美国F5网络公司)

  • Netscaler(美国思杰公司)

  • Array(华耀科技)

  • AD-1000(深信服)

1.3.2 四层负载均衡(L4)

  • 基于IP+Port转发流量

  • 流量请求进行NAT处理后转发

  • 记录TCP/UDP连接与服务器的映射关系
    常用软件

  • LVS(重量级)

  • Nginx(轻量级,通过upstream模块实现)

  • HAProxy(模拟四层转发)

1.3.3 七层负载均衡(L7)

  • 通过虚拟URL或主机IP识别流量,解析应用层信息

  • 代理客户端与后端服务器建立连接(如Nginx双向TCP连接)

  • 常用软件

    • Nginx(基于HTTP协议,通过proxy_pass实现)

    • HAProxy(支持会话保持、路径转移等高级功能)

1.3.4 四层与七层的核心区别

特性四层负载均衡七层负载均衡
工作层级传输层及以下应用层及以下
性能更高(无需解析报文内容)较低(需解析应用层数据)
原理基于IP+Port基于虚拟URL/主机IP
功能类比类似路由器类似代理服务器
安全性无法防御DDoS攻击可防御SYN Cookie/Flood攻击
二、HAProxy深度解析

HAProxy由Willy Tarreau于2000年使用C语言开发,具备以下特性:

  • 高性能TCP/HTTP负载均衡(支持万级并发)

  • 支持Cookie持久化、自动故障切换

  • 兼容正则表达式及Web状态统计

  • 官方资源

    • 企业版:https://www.haproxy.com

    • 社区版:http://www.haproxy.org

    • GitHub:https://github.com/haprox

企业版 vs 社区版功能对比

三、HAProxy安装与配置详解

3.1.实验环境

3.2.软件安装

软件包下载地址

https://github.com/haproxy/wiki/wiki/Packages

安装软件包:

haproxy ~]# dnf install haproxy -y

查看版本

haproxy软件基本信息

软件安装包: haproxy-2.4.22-3.el9_3.x86_64.rpm

启动文件: /lib/systemd/system/haproxy.service

主配置目录: /etc/haproxy/

主配置文件: /etc/haproxy/haproxy.cfg

子配置目录: /etc/haproxy/conf.d

3.3.haproxy的基本配置信息

官方文档:http://cbonte.github.io/haproxy-dconv/

HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是:

global:全局配置段

进程及安全配置相关的参数

性能调整相关参数

Debug参数

proxies:代理配置段

defaults:为frontend, backend, listen提供默认配置

frontend:前端,相当于nginx中的server {}

backend:后端,相当于nginx中的upstream {}

listen:同时拥有前端和后端配置,配置简单,生产推荐使用

3.3.1.global配置
3.3.1.1 global 配置参数说明
global log 127.0.0.1 local2 #定义全局的syslog服务器;日志服务器需要开启UDP协 议,最多可以定义两个

chroot /var/lib/haproxy #锁定运行目录

pidfile /var/run/haproxy.pid #指定pid文件

maxconn 100000 #指定最大连接数

user haproxy #指定haproxy的运行用户

group haproxy #指定haproxy的运行组

daemon #指定haproxy以守护进程方式运行

# turn on stats unix socket

stats socket /var/lib/haproxy/stats #指定haproxy的套接字文件

nbproc 2 #指定haproxy的work进程数量,默认是1个

cpu-map 1 0 #指定第一个work绑定第一个cpu核心

cpu-map 2 1 #指定第二个work绑定第二个cpu核心

nbthread 2 #指定haproxy的线程数量,默认每个进程一个线 程,此参数与nbproc互斥

maxsslconn 100000 #每个haproxy进程ssl最大连接数,用于haproxy 配置了证书的场景下

maxconnrate 100 #指定每个客户端每秒建立连接的最大数量
3.3.1 global全局配置

globallog 127.0.0.1 local2          # 定义syslog服务器chroot /var/lib/haproxy        # 锁定运行目录pidfile /var/run/haproxy.pid   # 指定PID文件maxconn 100000                 # 最大连接数user haproxy                   # 运行用户group haproxy                  # 运行组daemon                         # 守护进程模式stats socket /var/lib/haproxy/stats  # 状态套接字文件nbproc 2                       # 工作进程数cpu-map 1 0                    # 进程1绑定CPU0cpu-map 2 1                    # 进程2绑定CPU1nbthread 2                     # 每个进程的线程数maxsslconn 100000              # 每进程SSL最大连接数maxconnrate 100                # 每秒最大新建连接数
3.3.1.2 多进程和线程

多进程和socket文件配置如下:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...globallog 127.0.0.1 local2chroot          /var/lib/haproxypidfile         /var/run/haproxy.pidmaxconn         100000user            haproxygroup           haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 # 启用多个sock文件stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2nbproc 2              #启用多进程cpu-map 1 0           #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗cpu-map 2 1           #2 表示第二个进程,1表示第二个cpu核心...下面内容省略 ..
查看多进程信息

haproxy haproxy]# pstree -p | grep haproxy

|-haproxy(4816)-+-haproxy(4820)

|                            `-haproxy(4821)

启用多线程

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...globallog 127.0.0.1 local2chroot          /var/lib/haproxypidfile         /var/run/haproxy.pidmaxconn         100000user            haproxygroup           haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 # 启用多个sock文件stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2nbproc 2                #启用多进程cpu-map 1 0          #进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗cpu-map 2 1          #2 表示第二个进程,1表示第二个cpu核心nbthread 2 #启用多线程...下面内容省略 ..

多线程对比
未开启多线程

haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status

...上面内容省略...

Threads: 1

...下面内容省略...

开启后

haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status

...上面内容省略...

Threads: 2

...下面内容省略...
 

3.3.2 proxies配置
3.3.2.1.proxies参数说明proxies


name字段只能使用大小写字母,数字,‘-’(dash),'_‘(underscore),'.' (dot)和 ':'(colon),并且严格 区分大小写

3.3.2.2 Proxies配置-defaults


3.3.2.3 Proxies配置-frontend
frontend 配置参数:

bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字 段中

#格式:

bind [<address>]:<port_range>  [, ...] [param*]

#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1

backlog #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不 支持backend

frontend 配置示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...frontend lee-webserver-80bind 172.25.254.100:80mode httpuse_backend lee-webserver-80-RS #调用backend的名称...下面内容省略...
3.3.2.4 Proxies配置-backend

定义一组后端服务器,backend服务器将被frontend进行调用。

注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法 启动

mode http|tcp #指定负载协议类型,和对应的frontend必须一致

option #配置选项

server #定义后端real server,必须指定IP和端口

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更 多应用层检测功能。

server 配置

代码示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...backend lee-webserver-80-RSmode httpserver web1 192.168.0.101:80 check inter 3s fall 3 rise 5server web2 192.168.0.102:80 check inter 3s fall 3 rise 5...上面内容省略...

测试效果:

3.3.2.5 Proxies配置-listen 简化配置

使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用 listen配置示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode httpoption forwardforserver webserver1 192.168.0.101:80 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 check inter 3s fall 3 rise 5...上面内容省略...

3.4.socat 工具
对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat 是 Linux 下的一个多功能的网络工 具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向 通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等

范例:利用工具socat 对服务器动态权重调整

常用示例:

#查看haproxy状态

[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/statsName: HAProxyVersion: 2.4.22-f8e3218Release_date: 2023/02/14Nbthread: 1Nbproc: 1Process_num: 1Pid: 33542Uptime: 0d 0h03m43sUptime_sec: 223Memmax_MB: 0PoolAlloc_MB: 0 #查看集群状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 1# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweightsrv_iweight srv_time_since_last_change srv_check_status srv_check_resultsrv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_idsrv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addrsrv_agent_addr srv_agent_port2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 02 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 04 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 05 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 05 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 05 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 05 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats 1# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweightsrv_iweight srv_time_since_last_change srv_check_status srv_check_resultsrv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_idsrv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addrsrv_agent_addr srv_agent_port2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 02 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 04 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 05 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 05 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 05 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 05 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0

#查看集群权重

[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats2 (initial 2)[root@haproxy ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats1 (initial 1)
#设置权重
[root@haproxy ~]# echo "set weight webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats[root@haproxy ~]# echo "set weight webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats

下线后端服务器

[root@haproxy ~]# echo "disable server webserver_80/webserver1 " | socat stdio /var/lib/haproxy/stats

#上线后端服务器

[root@haproxy ~]# echo "enable server webserver_80/webserver1 " | socat stdio /var/lib/haproxy/stats

针对多进程处理方法

如果开启多进程那么我们在对进程的sock文件进行操作时其对进程的操作时随机的

如果需要指定操作进程那么需要用多soct文件方式来完成

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2nbproc 2cpu-map 1 0cpu-map 2 1...上面内容省略..
这样每个进程就会有单独的sock文件来进行单独管理
[root@haproxy ~]# ll /var/lib/haproxy/总用量 0srw------- 1 root root 0 8月 8 13:43 statssrw------- 1 root root 0 8月 8 13:46 stats1srw------- 1 root root 0 8月 8 13:46 stats2

3.5 haproxy的状态界面

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen statsmode httpbind 0.0.0.0:8888stats enablelog globalstats uri /haproxy-statusstats auth lee:lee...下面内容省略...

四.haproxy的算法
HAProxy通过固定参数 balance 指明对后端服务器的调度算法

balance参数可以配置在listen或backend选项中。

HAProxy的调度算法分为静态和动态调度算法

有些算法可以根据参数在静态和动态算法中相互转换。

4.1 静态算法
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度 等,且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。

4.1.1 static-rr:基于权重的轮询调度
不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)

不支持端服务器慢启动

其后端主机数量没有限制,相当于LVS中的 wrr

慢启动是指在服务器刚刚启动上不会把他所应该承担的访问压力全部给它,而是先给一部分,当没 问题后在给一部分

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode httpbalance static-rrserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

4.1.2 first

根据服务器在列表中的位置,自上而下进行调度

其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务

其会忽略服务器的权重设置

不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rrbalance firstserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

测试效果:

4.2 动态算法

动态算法

基于后端服务器状态进行调度适当调整,

新请求将优先调度至当前负载较低的服务器

权重可以在haproxy运行时动态调整无需重启

4.2.1 roundrobin

1. 基于权重的轮询动态调度算法,

2. 支持权重的运行时调整,不同于lvs中的rr轮训模式,

3. HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),

4. 其每个后端backend中最多支持4095个real server,

5. 支持对real server权重动态调整,

6. roundrobin为默认调度算法,此算法使用广泛

示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance firstbalance roundrobinserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

动态调整权重

[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio /var/lib/haproxy/haproxy.sock

测试效果:

4.2.2 leastconn

leastconn加权的最少连接的动态

支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户 端连接)

比较适合长连接的场景使用,比如:MySQL等场景。

示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobinbalance leastconnserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

4.3 其他算法

其它算法即可作为静态算法,又可以通过选项成为动态算法

4.3.1 source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一 个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服 务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP 模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持 cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法 和一致性hash

示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconnbalance sourceserver webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

测试:

[root@node10 ~]# for N in {1..6}; do curl 172.25.254.100; doneRS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102RS1 server - 192.168.0.102

如果访问客户端时一个家庭,那么所有的家庭的访问流量都会被定向到一台服务器,这时source 法的缺陷

4.3.1.1 map-base 取模法

map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请 求转发至对应的后端服务器。

此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度

缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果 整体改变

hash-type 指定的默值为此算法

所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3

map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重

比如当源hash值时1111,1112,1113,三台服务器a b c的权重均为1,

即abc的调度标签分别会被设定为 0 1 2(1111%3=1,1112%3=2,1113%3=0)

1111 ----- > nodeb

1112 ------> nodec

1113 ------> nodea

如果a下线后,权重数量发生变化

1111%2=1,

1112%2=0,

1113%2=1

1112和1113被调度到的主机都发生变化,这样会导致会话丢失

取模法配置示例:

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance sourcebalance sourceserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

 #不支持动态调整权重值

[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio /var/lib/haproxy/haproxy.sockBackend is using a static LB algorithm and only accepts weights '0%' and '100%'.

#只能动态上线和下线

[root@haproxy ~]# echo "set weight webserver_80/webserver1 0" | socat stdio /var/lib/haproxy/haproxy.sock[root@haproxy ~]# echo "get weight webserver_80/webserver1" | socat stdio /var/lib/haproxy/haproxy.sock0 (initial 1)
4.3.1.2

一致性hash 一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash(o) mod n

该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

算法:

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)

2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,

3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环偏斜问题

增加虚拟服务器IP数量,比如:一个后端服务器根据权重为1生成1000个虚拟IP,再hash。而后端服务器权 重为2则生成2000的虚拟IP,再bash,最终在hash环上生成3000个节点,从而解决hash环偏斜问题

hash对象

Hash对象到后端服务器的映射关系:

一致性hash示意图

后端服务器在线与离线的调度方式

一致性hash配置示例

haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance sourcebalance sourcehash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

4.3.2 uri

基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后 根据最终结果将请求转发到后端指定服务器

适用于后端是缓存服务器场景

默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性 hash

注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp

<scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>

左半部分:/<path>;<params>

整个uri:/<path>;<params>?<query>#<frag>

4.3.2.1 uri 取模法配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance sourcebalance uriserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3.2.2 uri 一致性hash配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance sourcebalance urihash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

访问测试:

访问不同的uri,确认可以将用户同样的请求转发至相同的服务器

[root@rs1 ~]# echo RS1 192.168.0.101 index1 > /var/www/html/index1.html[root@rs1 ~]# echo RS1 192.168.0.101 index2 > /var/www/html/index2.html[root@rs1 ~]# echo RS1 192.168.0.101 index3 > /var/www/html/index3.html[root@rs2 ~]# echo RS1 192.168.0.102 index1 > /var/www/html/index1.html[root@rs2 ~]# echo RS1 192.168.0.102 index2 > /var/www/html/index2.html[root@rs2 ~]# echo RS1 192.168.0.102 index3 > /var/www/html/index3.html

[root@node10 ~]# curl 172.25.254.100/index.htmlRS2 server - 192.168.0.102[root@node10 ~]# curl 172.25.254.100/index1.htmlRS1 192.168.0.101 index1[root@node10 ~]# curl 172.25.254.100/index2.htmlRS1 192.168.0.102 index2[root@node10 ~]# curl 172.25.254.100/index3.htmlRS1 192.168.0.101 index3

4.3.3 url_param

url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器 总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商

通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server

如果无没key,将按roundrobin算法

#假设: url = http://www.timinglee.com/foo/bar/index.php?key=value

#则: host = "www.timinglee.com"

url_param = "key=value"

4.3.3.1 url_param取模法配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance source#balance uribalance url_param name,userid #支持对多个url_param hashtserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...
4.3.3.2 url_param一致性hash配置示例
haproxy ~]# vim /etc/haproxy/haproxy.cfg...上面内容省略...listen webserver_80bind 172.25.254.100:80mode http#balance static-rr#balance first#balance roundrobin#balance leastconn#balance source#balance source#balance uribalance url_param name,userid #支持对多个url_param hashthash-type consistentserver webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5...上面内容省略...

测试访问

[root@node10 ~]# curl 172.25.254.100/index.html?name=timingleeRS1 192.168.0.101 index3[root@node10 ~]# curl 172.25.254.100/index.html?name=timingleeRS1 192.168.0.101 index3[root@node10 ~]# curl 172.25.254.100/index.html?userid=leeRS1 192.168.0.102 index3[root@node10 ~]# curl 172.25.254.100/index.html?userid=leeRS1 192.168.0.102 index3

4.3.6 算法总结
#静态

static-rr--------->tcp/http

first------------->tcp/http

#动态

roundrobin-------->tcp/http

leastconn--------->tcp/http

random------------>tcp/http

#以下静态和动态取决于hash_type是否consistent

source------------>tcp/http

Uri--------------->http

url_param--------->http

hdr--------------->http

4.3.7 各算法使用场景
first #使用较少

static-rr #做了session共享的web集群

roundrobin random leastconn #数据库

source

#基于客户端公网IP的会话保持

Uri--------------->http #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯

url_param--------->http #可以实现session保持

hdr #基于客户端请求报文头部做下一步处理

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/90862.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/90862.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

22 BTLO 蓝队靶场 Countdown 解题记录

Tools: - ELK - CyberChef - OSINT (whole World Wide Web) Hunt #1: Brute Force DetectedSource: winevent-security (1/3) — 可疑暴力破解流量来自哪个IP地址 What is the IP address from which the suspicious brute force traffic is seen?? 我们需要寻找暴力破解…

文心一言4.5开源模型实战:ERNIE-4.5-0.3B轻量化部署与效能突破

文心一言4.5开源模型实战&#xff1a;ERNIE-4.5-0.3B轻量化部署与效能突破 文心一言4.5开源模型实战&#xff1a;ERNIE-4.5-0.3B轻量化部署与效能突破&#xff0c;本文介绍百度文心一言 4.5 开源模型中 ERNIE-4.5-0.3B 的轻量化部署与效能。该 3 亿参数模型破解大模型落地的算力…

SAP-MM-采购订单批量创建 excel 版

采购订单批量创建程序摘要:不含任何定制字段的导入,直接导入系统即可使用 该SAP ABAP程序实现采购订单的批量创建功能,主要特性包括: 支持通过Excel文件批量导入采购订单数据(XLS/XLSX格式) 提供数据校验功能,包括: 物料号有效性检查 采购凭证存在性验证 科目分配类别…

2_软件重构_一种组件化开发方式

一、碎碎念 首先先考虑下&#xff0c;什么情况下软件需要重构&#xff1f;我觉得答案有很多种&#xff0c;而且还有范围。当日益增长的需求与现有软件结构越来越无法匹配时——①具体表现可能为新增需求所导致的bug越来越多&#xff0c;一个新功能的改动牵一发而动全身&a…

今日行情明日机会——20250728

上证指数量能持续在200天均量线上&#xff0c;最近今天横盘震荡&#xff0c;今天依然收在5天均线上方&#xff0c;个股涨跌个数基本相同。目前依然强势&#xff0c;有望冲击3674的前高。需要注意板块的高低切换。深证指数今天缩量收小阳线&#xff0c;均线多头的趋势明显&#…

【iOS】类和分类的加载过程

目录 前言 _objc_init方法 environ_init tis->init方法 static_init方法 &#x1f4a1; _objc_init 是由 libc 调用的&#xff0c;目的是&#xff1a; ❗️“必须自己实现” 是什么意思&#xff1f; runtime_init exception_init cache_t::init _imp_implementati…

大模型算法面试笔记——常用优化器SGD,Momentum,Adagrad,RMSProp,Adam

常用参数&#xff1a;ttt-步数&#xff0c;α\alphaα-学习率&#xff0c;θ\thetaθ-参数&#xff0c;f(θ)f(\theta)f(θ)-目标函数&#xff0c;gtg_tgt​-梯度&#xff0c;β1\beta_1β1​-一阶矩衰减系数&#xff0c;通常取0.9&#xff0c;β2\beta_2β2​-二阶矩&#xff…

【计算机毕业设计】基于SSM的小型超市管理系统+LW

博主介绍&#xff1a;✌全网粉丝3W,csdn特邀作者、CSDN新星计划导师、Java领域优质创作者,掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流✌ 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、…

火线、零线、地线

我们可以用 “水流” 来比喻 “电流”&#xff0c;这样理解起来会很简单&#xff1a;想象一下你家的电路就像一个 “闭合的水循环系统”&#xff1a;&#x1f525; 1. 火线 (Live Wire) - 好比 “进水管的高压端”作用&#xff1a; 从发电厂或变压器输送 高压电 到你家的插座或…

基于Vue3.0+Express的前后端分离的任务清单管理系统

文章目录 一、前端 0、项目介绍 0.1 主要功能介绍 0.2 UI展示 1、首页 2、待办事项管理 2.1 添加待办事项 2.2 展示待办事项 2.3 修改待办事项 2.4 删除待办事项 3、分类管理 3.1 添加分类 3.2 展示分类 3.3 修改分类 3.4 删除分类 4、团队成员管理 4.1 展示团队成员 二、后端 …

基于单片机智能交通灯设计

传送门 &#x1f449;&#x1f449;&#x1f449;&#x1f449;其他作品题目速选一览表 &#x1f449;&#x1f449;&#x1f449;&#x1f449;其他作品题目功能速览 概述 随着城市化进程的加快&#xff0c;城市交通流量日益增大&#xff0c;传统的固定配时交通灯已难以…

Datawhale AI夏令营——列车信息智能问答——科大讯飞AI大赛(基于结构化数据的用户意图理解和知识问答挑战赛)

前言 坐火车的你&#xff0c;遇到过这样的场景吗&#xff1f; 一次又一次查车次信息&#xff1f;赶火车狂奔&#xff0c;找检票口找到怀疑人生…想查“最早到北京的车”&#xff1f;时刻表翻到眼瞎&#xff01;列车晚点&#xff1f;新出发时间算到脑壳疼&#xff01; 我们这次将…

UVA11990 ``Dynamic‘‘ Inversion

UVA11990 Dynamic Inversion题目链接题意输入格式输出格式分析CDQ分治嵌套&#xff08;树状数组套BST&#xff09;分块k-D Tree题目链接 UVA11990 Dynamic’’ Inversion 题意 给一个 1~n 的排列A&#xff0c;要求按照某种顺序删除一些数&#xff08;其他数顺序不变&#xff0…

银河麒麟“安装器”安装方法

书接上回&#xff1a;银河麒麟安装软件商店方法-CSDN博客 过了几天发现当时一不小心把系统自带的“安装器”软件也卸载掉了&#xff0c;导致现在deb文件只能通过命令行安装&#xff0c;寻思这可不行&#xff0c;就想一下应该怎么安装。 首先&#xff0c;为了确认一下安装器的…

计算机毕设分享-基于SpringBoot的健身房管理系统(开题报告+前后端源码+Lun文+开发文档+数据库设计文档)

基于SpringBoot的健身房管理系统分享一套完整的基于SpringBoot的健身房管理系统毕业设计&#xff08;开题报告完整前后端源码Lun文 开发文档数据库设计文档&#xff09;系统分为三个角色功能如下&#xff1a;用户功能需求描述管理员功能需求描述教练功能需求描述开题报告系统功…

代码审计与web安全选择题1

软件供应链安全的基础是&#xff08; &#xff09;A.完善的需求分析B.源代码安全C.渗透测试D.软件测试参考答案&#xff1a;B保证源代码安全的主要措施包括&#xff08; &#xff09;A.开发工具和环境的安全B.代码安全C.渗透测试D.代码审计E.软件的说明文档完整参考…

python基本数据类型 数据类型转换 数字 菜鸟教程笔记

python基本数据类型 数据类型转换 数字 菜鸟教程笔记 1.基本数据类型 Python 中的变量不需要声明。每个变量在使用前都必须赋值&#xff0c;变量赋值以后该变量才会被创建。 在 Python 中&#xff0c;变量就是变量&#xff0c;它没有类型&#xff0c;我们所说的"类型"…

USRP X410 X440 5G及未来通信技术的非地面网络(NTN)

概述 在本白皮书中&#xff0c;我们将介绍NTN的现状、正处于探索阶段的一些新应用&#xff0c;以及最重要的一点&#xff0c;我们需要克服哪些技术挑战才能让这个市场充满活力。最后&#xff0c;我们将概述为实现实用高效的测试&#xff0c;NI围绕NTN所做的努力&#xff0c;该测…

基于SpringBoot+Vue的电脑维修管理系统(WebSocket实时聊天、Echarts图形化分析)

“ &#x1f388;系统亮点&#xff1a;WebSocket实时聊天、Echarts图形化分析”01系统开发工具与环境搭建—前后端分离架构项目架构&#xff1a;B/S架构运行环境&#xff1a;win10/win11、jdk17小程序端&#xff1a;技术&#xff1a;Uniapp&#xff1b;UI库&#xff1a;colorUI…

2025.7.28总结

今天真有点小烦&#xff0c;工作有些不太顺利&#xff0c;我是真没想到&#xff0c;阻塞我工作开展得竟然是我的主管。当初需求澄清的时候&#xff0c;开发说要申请一个便携&#xff0c;我当时申请的时候也跟主管说了&#xff0c;需求测试的时候要使用到&#xff0c;但主管要我…