Haproxy服务

目录

一.haproxy介绍

1.主要特点和功能

2.haproxy 调度算法

3.haproxy 与nginx 和lvs的区别

二.安装 haproxy 服务

1. yum安装

2.第三方rpm 安装

3.编译安装haproxy

三.配置文件详解

1.官方地址配置文件官方帮助文档

2.HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分

3.global配置

4.状态页

5.指定进程线程个数

6.cpu亲缘性

7.多进程和多线程线程

8.日志

9.Proxies配置

10.子配置文件

四.调度算法

1.静态算法

1.1 socat工具

1.2 static-rr

1.3 fist

2.动态算法

2.1   roundrobin (默认算法)

2.2 leastconn

2.3  random

3.其他算法

3.1 source

3.2 一致性hash

3.3 uri 

3.4 url_param

3.5 hdr

五.附录


一.haproxy介绍

HAProxy(High Availability Proxy)是一个开源的负载均衡和代理服务器,它运行在 Linux 和其他类 Unix 系统上。HAProxy 最初是为了解决高可用性和负载均衡问题而开发的,因此它具有非常高的性能、稳定性和丰富的特性。

1.主要特点和功能

  1. 负载均衡: HAProxy 提供了强大的负载均衡能力,能够根据多种算法(如轮询、加权轮询、最少连接数等)将流量分发到多个后端服务器上,实现负载均衡。

  2. 健康检查: HAProxy 能够定期检查后端服务器的健康状态,通过配置的方式判断服务器是否可用,并且能够自动将请求转发到可用的服务器上。

  3. SSL/TLS 终结: 支持对 SSL/TLS 的终结,可以在 HAProxy 服务器上对加密的请求进行解密,并将请求转发到后端的非加密服务器上。

  4. 会话保持: HAProxy 能够根据客户端 IP 或 cookie 等方式保持会话,确保同一客户端的请求能够路由到同一个后端服务器上,适用于需要保持状态的应用场景。

  5. 反向代理: 可以作为反向代理服务器,接收客户端的请求并将其转发到后端的多个服务器上,同时隐藏了后端服务器的实际信息。

  6. 动态配置: HAProxy 支持热部署和动态配置更新,可以在运行时添加或删除后端服务器,而无需重启整个服务,从而实现高可用性和灵活性。

  7. 日志和统计: 提供详细的日志记录和统计信息,可以实时监控服务的运行状态,帮助管理员进行故障排查和性能优化。

  8. 支持多种协议: 支持 HTTP、HTTPS、TCP 和 UDP 等多种协议的负载均衡和代理。

2.haproxy 调度算法

  1. 轮询 (Round Robin): 轮询算法是最简单的负载均衡算法之一。请求按顺序依次分配给后端服务器,每个请求依次落在不同的服务器上。这种方式确保了每个后端服务器都能平均分担负载。

  2. 加权轮询 (Weighted Round Robin): 加权轮询允许为每个后端服务器分配一个权重值,根据权重值决定请求分配的频率。权重越高的服务器,收到的请求越多。这种算法适用于不同服务器性能不均的情况。

  3. 最少连接数 (Least Connections): 最少连接数算法会将请求分配给当前连接数最少的服务器。这样可以有效地将负载分散到连接数较少的服务器上,从而避免某些服务器负载过重。

  4. IP哈希 (IP Hash): IP 哈希算法根据客户端的 IP 地址来决定将请求分发到哪个后端服务器。同一客户端的请求总是被分发到同一台后端服务器,有助于保持会话的连续性。

  5. URI哈希 (URI Hash): URI 哈希算法根据请求的 URI(通常是路径部分)来决定将请求分发到哪个后端服务器。相同 URI 的请求总是被分发到同一台后端服务器上,适用于特定的应用场景。

  6. 动态加权 (Dynamic Weighting): 动态加权算法根据后端服务器的负载情况动态调整权重。负载较低的服务器会获得更高的权重,从而分担更多的请求,这种算法可以自动平衡负载。

调度算法的 对比:

nginxhaproxylvs
rr(轮询)rrrr
加权轮询static-rrwrr 加权
ip hash最小连接源地址 hash SH
url hashsource 根据源地址目的地址 hash DH
cookie hash根据请求来调度默认 wlc 加权最小连接
fair根据cookie 来调度lc 最小连接
最小连接根据请求头SED初始连接高权重优先
NQ 每个人都有一个链接后 再使用 初始链接高权重

3.haproxy 与nginx 和lvs的区别

  1. IVS基于Linux操作系统实现软负载均衡,而HAProxy和Nginx是基于第三方应用实现的软负载均衡;

  2. LVS是可实现4层的IP负载均衡技术,无法实现基于目录、URL的转发。而HAProxy和Nginx都可以实现4层和7层技术,HAProxy可提供TCP和HTTP应用的负载均衡综合解决方案;

  3. LVs因为工作在TCP模型的第四层,其状态监测功能单一,而HAProxy在状态监测方面功能更丰富、强大,可支持端口、URI等多种状态检测方式;

  4. HAProxy功能强大,但整体性能低于4层模式的IVS负载均衡。

  5. Ngrinx主要用于web服务器或缓存服务器。Nginx的upstream模块虽然也支持群集功能,但是对群集节点健康检查功能不强,性能没有Haproxy好。

二.安装 haproxy 服务

1. yum安装

CentOS 7 的默认的base仓库中包含haproxy的安装包文件,但是版本比较旧,是1.5.18的版本,距离当前版本已经有较长时间没有更新,由于版本比较旧所以有很多功能不支持,如果对功能和性能没有要求可以使用此版本,否则推荐使用新版本。

yum tall -y haproxy
#验证haproxy版本
haproxy -v
yum info haproxy
#15.18版本

2.第三方rpm 安装

官方没有提供rpm相关的包,可以通过第三方仓库的rpm包

从第三方网站下载rpm包:https://pkgs.org/download/haproxy

基于互联网第三方仓库在线安装

[root@localhost data]#wget http://mirror.centos.org/centos/7/sclo/x86_64/rh/Packages/r/rh-haproxy18-haproxy-1.8.24-3.el7.x86_64.rpm
[root@localhost data]#yum install -y rh-haproxy18-haproxy-1.8.24-3.el7.x86_64.rpm
#有依赖关系
[root@localhost data]#rpm -ql rh-haproxy18-haproxy
[root@localhost data]#vim /usr/lib/systemd/system/rh-haproxy18-haproxy.service
[root@localhost data]#systemctl start rh-haproxy18-haproxy.service

[root@localhost data]#/opt/rh/rh-haproxy18/root/usr/sbin/haproxy -v
HA-Proxy version 1.8.24 2020/02/15
Copyright 2000-2020 Willy Tarreau <willy@haproxy.org>

第三方yum仓库

yum install centos-release-scl-rh 
yum install rh-haproxy18-haproxy

3.编译安装haproxy

1.5 基础功能 稳定

2.0 新特性

编译安装HAProxy 2.0 LTS版本,更多源码包下载地址:HAProxy - The Reliable, High Perf. TCP/HTTP Load Balancer

解决lua环境

HAProxy 支持基于lua实现功能扩展,lua是一种小巧的脚本语言,于1993年由巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组开发,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。

Lua 官网:www.lua.org

Lua 应用场景

  • 游戏开发

  • 独立应用脚本

  • Web 应用脚本

  • 扩展和数据库插件,如MySQL Proxy

  • 安全系统,如入侵检测系统

由于CentOS7 之前版本自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此需要编译安装较新版本的lua环境,然后才能编译安装HAProxy,过程如下:

#当前系统版本curl -R -O http://www.lua.org/ftp/lua-5.4.6.tar.gz
[root@localhost ~]# lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
 https://www.lua.org/ftp/lua-5.4.6.tar.gz
[root@localhost opt]# wget https://www.lua.org/ftp/lua-5.4.6.tar.gz
tar zxf lua-5.4.6.tar.gz
ln -s lua-5.4.6    lua
cd lua-5.4.6
make all test
cd  src
./lua -v 

编译安装 haproxy 

yum -y install gcc openssl-devel pcre-devel systemd-devel
#安装依赖环境

[root@localhost opt]# tar xf haproxy-2.4.25.tar.gz 
[root@localhost opt]# cd haproxy-2.4.25/
mkdir /usr/local/lua
[root@localhost haproxy-2.4.25]#:cp -r src/   /usr/local/lua/
#查看安装方法
ll Makefile
cat README 
cat INSTALL

make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/opt/lua/src/  LUA_LIB=/opt/lua/src/

make install PREFIX=/apps/haproxy
ln -s /apps/haproxy/sbin/haproxy /usr/sbin/
haproxy  -v
haproxy  -h   查看帮助

vim /usr/lib/systemd/system/haproxy.service

tee /usr/lib/systemd/system/haproxy.service  <<eof

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg  -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

eof

建立文件

mkdir /etc/haproxy
vim /etc/haproxy/haproxy.cfg

global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
#nbproc 4
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info

defaults
option http-keep-alive
option  forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client  300000ms
timeout server  300000ms

listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:123456

listen  web_port
 bind 0.0.0.0:8899
 mode http
 log global
 server web1  127.0.0.1:8080  check inter 3000 fall 2 rise 5

mkdir  /var/lib/haproxy
#pid 文件路径
#设置用户和目录权限
[root@centos7 ~]# useradd -r -s /sbin/nologin  haproxy
[root@centos7 ~]# systemctl enable --now haproxy

三.配置文件详解

1.官方地址配置文件官方帮助文档

http://cbonte.github.io/haproxy-dconv/
http://cbonte.github.io/haproxy-dconv/2.4/configuration.html
https://www.haproxy.org/download/2.5/doc/configuration.txt

2.HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分

global:全局配置段

进程及安全配置相关的参数
性能调整相关参数
Debug参数

proxies:代理配置段

defaults:为frontend, backend, listen提供默认配置
frontend:前端,相当于nginx中的server {}    定义虚拟机       监听 分配请求
backend:后端,相当于nginx中的upstream {}         真实服务器
listen:同时拥有前端和后端配置,配置简单,生产推荐使用  前端 和后端 更简单

3.global配置

官方文档:http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#3

详细讲解

chroot #锁定运行目录,类似于  ftp中的禁锢
deamon #以守护进程运行
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1   #socket文件 进程件通信
user, group, uid, gid  #运行haproxy的用户身份


nbproc   n #开启的haproxy worker 进程数,默认进程数是一个,保持与 淳朴个数相同
#nbthread 1 #和多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程
#如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务

Apr  7 14:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot 
enable multiple processes if multiple threads are configured. Please use either 
nbproc or nbthread but not both.

cpu-map 1 0   		#绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPU
cpu-map 2 1     	#绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1号CPU
maxconn n   		#每个haproxy进程的最大并发连接数
maxsslconn n   		#每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
maxconnrate n   	#每个进程每秒创建的最大连接数量
spread-checks n 	#后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认值0
pidfile 			#指定pid文件路径
log 127.0.0.1 local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个
4.状态页
listen stats
 mode http
 bind 0.0.0.0:9999
 stats enable
 log global
 stats uri     /haproxy-status
 stats auth    haadmin:123456


http://192.168.240.12:9999/haproxy-status
5.指定进程线程个数

进程与线程会有冲突

nbproc     n     #开启的haproxy work 进程数,默认进程数是一个
#nbthread  1    #和多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程
#如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务Apr  714:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.
nbproc     4
nbthread   2 

放入全局配置
6.cpu亲缘性
nbproc  2
cpu-map 1  0     #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPU
cpu-map 2  1     #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1  号CPU
                
ps axo  pid,cmd,psr  |grep haproxy
7.多进程和多线程线程

范例:多进程和socket文件

vim /etc/haproxy/haproxy.cfg

global
maxconn 100000
chroot /apps/haproxy
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1       
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
uid 99
gid 99
daemon
nbproc 2

systemctl restart haproxy
pstree -p |grep haproxy           
ll /var/lib/haproxy
8.日志

HAproxy本身不记录客户端的访问日志.此外为减少服务器负载,一般生产中HAProxy不记录日志.也可以配置HAProxy利用rsyslog服务记录日志到指定日志文件服务器中

#在global配置项定义:
log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
listen web_port
 bind 127.0.0.1:80
 mode http
 log global #开启当前web_port的日志功能,默认不记录日志
 server web1  127.0.0.1:8080 check inter 3000 fall 2 rise 5
9.Proxies配置

官方文档:HAProxy version 2.1.12 - Configuration Manual

defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
frontend <name>   #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
backend <name>   #后端服务器组,等于nginx的upstream和LVS中的RS服务器
listen  <name>   #将frontend和backend合并在一起配置,相对于frontend和backend配置更简洁,生产常用

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

Proxies配置-defaults

defaults 配置参数:

option redispatch    			#当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
option abortonclose   			#当服务器负载很高时,自动结束掉当前队列处理比较久的连接,针对业务情况选择开启
option http-keep-alive 			#开启与客户端的会话保持
option forwardfor     			#透传客户端真实IP至后端web服务器
mode http|tcp 					#设置默认工作类型,使用TCP服务器性能更好,减少压力
timeout http-keep-alive 120s 	#session 会话保持超时时间,此时间段内会转发到相同的后端服务器
timeout connect 120s			#客户端请求从haproxy到后端server最长连接等待时间(TCP连接之前),默认单位ms
timeout server 600s 			#客户端请求从haproxy到后端服务端的请求处理超时时长(TCP连接之后),默认单位ms,如果超时,会出现502错误,此值建议设置较大些,防止502错误
timeout client 600s 			#设置haproxy与客户端的最长非活动时间,默认单位ms,建议和timeout server相同
timeout check   5s   			#对后端服务器的默认检测超时时间
default-server inter 1000 weight 3   #指定后端服务器的默认设置

Proxies配置-listen 简化配置

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

#官网业务访问入口
listen WEB_PORT_80    #业务名称
   bind 10.0.0.7:80   #ip加端口
   mode http          #默认 可以不写
   option forwardfor  #透传客户端真实IP至后端web服务器
   server web1   10.0.0.17:8080   check inter 3000 fall 3 rise 5
   server web2   10.0.0.27:8080   check inter 3000 fall 3 rise 5

Proxies配置-frontend

frontend 配置参数:

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

#格式:
bind [<address>]:<port_range> [, ...] [param*]
#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1


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

Proxies配置-backend

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

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

mode http|tcp     #指定负载协议类型,和对应的frontend必须一致
option #配置选项
server   #定义后端real server,必须指定IP和端口

server 配置

#针对一个server配置
check #对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能
 #默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查
 addr <IP>   #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
 port <num> #指定的健康状态监测端口
 inter <num> #健康状态检查间隔时间,默认2000 ms
 fall <num>   #后端服务器从线上转为线下的检查的连续失效次数,默认为3
 rise <num>   #后端服务器从下线恢复上线的检查的连续有效次数,默认为2
weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接
backup #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server
disabled #将后端服务器标记为不可用状态,即维护状态,除了持久模式,将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
redirect prefix http://www.baidu.com/ #将请求临时(302)重定向至其它URL,只适用于http模式
redir http://www.baidu.com       #将请求临时(302)重定向至其它URL,只适用于http模式
maxconn <maxconn> #当前后端server的最大并发连接数
10.子配置文件

当业务众多时,将所有配置都放在一个配置文件中,会造成维护困难。可以考虑按业务分类,将配置信息拆分,放在不同的子配置文件中,从而达到方便维护的目的。

#创建子配置目录
[root@centos7 ~]#mkdir /etc/haproxy/conf.d/

[root@localhost data]#sed -n '38,$p'  /etc/haproxy/haproxy.cfg
[root@localhost data]#sed -n '38,$p'  /etc/haproxy/haproxy.cfg   > /etc/haproxy/conf.d/test.cfg
# 注意配置文件名要以  cfg  结尾
[root@localhost data]#vim  /etc/haproxy/haproxy.cfg 
#注释掉原来的 38行开始


[root@localhost data]#vim /usr/lib/systemd/system/haproxy.service 
#  将子配置文件的   位置加入进来

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg  -f /etc/haproxy/conf.d/ -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/  -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

[root@localhost data]#systemctl daemon-reload 

四.调度算法

HAProxy通过固定参数 balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。

HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。

官方文档:HAProxy version 2.1.12 - Configuration Manual

1.静态算法

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

1.1 socat工具

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

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

[root@centos7 ~]#yum -y install socat
#查看帮助
[root@centos7 ~]#socat -h
[root@centos7 ~]#echo "help" | socat stdio /var/lib/haproxy/haproxy.sock
#查看  soket文件的帮助, 这个只是  haproxy 软件的帮助

[root@localhost data]#echo "show backend" | socat  stdio  /var/lib/haproxy/haproxy.sock 
#查看 后端服务器的名称
# name
stats
cxk

[root@centos7 ~]#echo "show info" | socat stdio /var/lib/haproxy/haproxy.sock
#查看  haproxy 的版本信息

[root@localhost data]#echo "get weight   cxk/web1" | socat stdio /var/lib/haproxy/haproxy.sock
#查看服务器的权重   cxk 是后端的名字    web1是 真实服务器的名字
1 (initial 1)

修改权重

由于有多个进程所以在发送指令的时候不确定发给谁所以最好一个sock 文件对应一个进程

[root@localhost data]#echo "set weight   cxk/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock

[root@localhost data]#echo "get weight   cxk/web1" | socat stdio /var/lib/haproxy/haproxy.sock
3 (initial 1)

能否在状态页中看到效果   看运气

修改配置文件

[root@localhost data]#vim /etc/haproxy/haproxy.cfg 
#全局配置下添加
stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2

[root@localhost data]#systemctl restart    haproxy.service 
[root@localhost data]#
[root@localhost data]#
[root@localhost data]#ll /var/lib/haproxy/
总用量 4
-rw-r--r--. 1 root root 6 6月  20 15:29 haproxy.pid
srw-------. 1 root root 0 6月  20 12:30 haproxy.sock
srw-------. 1 root root 0 6月  20 15:29 haproxy.sock1
srw-------. 1 root root 0 6月  20 15:29 haproxy.sock2

#
[root@localhost data]#echo "set weight   cxk/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock1
[root@localhost data]#echo "set weight   cxk/web1 3" | socat stdio /var/lib/haproxy/haproxy.sock2

禁用后端服务器

[root@localhost data]#echo "disable server    cxk/web1 " | socat stdio /var/lib/haproxy/haproxy.sock1
[root@localhost data]#echo "disable server    cxk/web1 " | socat stdio /var/lib/haproxy/haproxy.sock2

上线下线脚本

[root@centos7 ~]#cat haproyx_host_up_down.sh
. /etc/init.d/functions
case $1 in
up)
   echo "set weight cxk/$2 1" | socat stdio  /var/lib/haproxy/haproxy.sock
   [ $? -eq 0 ] && action "$2 is up"
   ;;
down)
    echo "set weight cxk/$2 0" | socat stdio /var/lib/haproxy/haproxy.sock
   [ $? -eq 0 ] && action "$2 is down"
   ;;
*)
    echo "Usage: `basename $0` up|down IP"
   ;;
esac
1.2 static-rr

static-rr:基于权重的轮询调度,不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr

官方解释

static-rr   Each server is used in turns, according to their weights.
              This algorithm is as similar to roundrobin except that it is
              static, which means that changing a server's weight on the
              fly will have no effect. On the other hand, it has no design
              limitation on the number of servers, and when a server goes
              up, it is always immediately reintroduced into the farm, once
              the full map is recomputed. It also uses slightly less CPU to
              run (around -1%).

设置

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance static-rr
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
 
backend  cxk
balance  static-rr
server web1  192.168.240.11:80
server web2  192.168.240.13:80

测试是否可以动态修改

[root@localhost data]#echo "set weight cxk/web1 2" |socat stdio /var/lib/haproxy/haproxy.sock1
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
1.3 fist

first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

backend  cxk
balance  first
server web1  192.168.240.11:80  check    maxconn  10
server web2  192.168.240.13:80

maxconn  最大链接数

设置好后可以测试

while true;do curl http://10.0.0.7/index.html ; sleep 0.1;done

2.动态算法

动态算法:基于后端服务器状态进行调度适当调整,优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。
2.1   roundrobin (默认算法)

roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,支持对real server权重动态调整,roundrobin为默认调度算法,此算法使用广泛

roundrobin  Each server is used in turns, according to their weights.
              This is the smoothest and fairest algorithm when the server's
              processing time remains equally distributed. This algorithm
              is dynamic, which means that server weights may be adjusted
              on the fly for slow starts for instance. It is limited by
              design to 4095 active servers per backend. Note that in some
              large farms, when a server becomes up after having been down
              for a very short time, it may sometimes take a few hundreds
              requests for it to be re-integrated into the farm and start
              receiving traffic. This is normal, though very rare. It is
              indicated here in case you would have the chance to observe
              it, so that you don't worry.

示例: 

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance roundrobin
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5


[root@localhost data]#echo "get weight cxk/web1" |socat stdio /var/lib/haproxy/haproxy.sock
#注意此处修改了 haproxy 的 进程模式



[root@localhost data]#curl 192.168.240.12
2.2 leastconn

leastconn 加权的最少连接的动态,支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景。

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance leastconn
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
2.3  random

在1.9版本开始增加 random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概率获取新请求

random配置实例

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance random
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5

3.其他算法

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

3.1 source

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

配置示例

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode tcp
 log global
 balance source
 hash-type map-based 
 server web1  10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 3
 server web2  10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 3
 
 
 #不支持动态调整权重值
[root@haproxy ~]#echo "set weight web_host/10.0.0.27 10" | socat stdio 
/var/lib/haproxy/haproxy.sock 
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
#只能动态上线和下线
[root@haproxy ~]#echo "set weight web_host/10.0.0.27 0" | socat stdio 
/var/lib/haproxy/haproxy.sock 
[root@haproxy conf.d]#echo "get weight web_host/10.0.0.27" | socat stdio 
/var/lib/haproxy/haproxy.sock 
0 (initial 1)
3.2 一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动,hash(o)mod n ,该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode tcp
 log global
 balance source
 hash-type consistent
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
3.3 uri 

基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后,根据最终结果将请求转发到后端指定服务器,适用于后端是缓存服务器场景,默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash。

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance uri
 #hash-type consistent
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
3.4 url_param

url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器;通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server,如果无没key,将按roundrobin算法

假设:
url = http://www.laowang.com/foo/bar/index.php?key=value
则:
host = "www.laowang.com"
url_param = "key=value"

配置

isten web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance url_param userid #url_param hash
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5

测试

 curl http://192.168.240.12/index.html?userid=zhou
3.5 hdr

针对用户每个http头部(header)请求中的指定信息做hash,此处由 name 指定的http首部将会被取出并做hash计算,然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。

配置示例

listen web_host
 bind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010
 mode http
 log global
 balance hdr(User-Agent)
 #balance hdr(host)
  
 server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
 server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5

测试访问

[root@centos7~]#curl -v http://10.0.0.7/index.html
[root@centos7 ~]#curl -vA 'firefox' http://10.0.0.7/index.html
[root@centos7 ~]#curl -vA 'chrome' http://10.0.0.7/index.html

五.附录

chroot    #锁定运行目录
deamon    #以守护进程运行,后台运行
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin process 1   #socket文件
user, group, uid, gid  #运行haproxy的用户身份
nbproc    n     #开启的haproxy work 进程数,默认进程数是一个
#nbthread  1    #和多进程 nbproc配置互斥(版本有关,CentOS8的haproxy1.8无此问题),指定每个haproxy进程开启的线程数,默认为每个进程一个线程
#如果同时启用nbproc和nbthread 会出现以下日志的错误,无法启动服务Apr  714:46:23 haproxy haproxy: [ALERT] 097/144623 (1454) : config : cannot enable multiple processes if multiple threads are configured. Please use either nbproc or nbthread but not both.

cpu-map 1  0     #绑定haproxy worker 进程至指定CPU,将第1个work进程绑定至0号CPU
cpu-map 2  1     #绑定haproxy worker 进程至指定CPU,将第2个work进程绑定至1  号CPU
                 #ps axo  pid,cmd,psr  |grep haproxy
maxconn  n      #每个haproxy进程的最大并发连接数
maxsslconn  n   #每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
maxconnrate n   #每个进程每秒创建的最大连接数量
spread-checks n #后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认值0
pidfile         #指定pid文件路径
log 127.0.0.1  local2 info #定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个

defaults
        log     global        #引入global定义的日志格式
        mode    http          #模式为http(7层代理http,4层代理tcp)
        option  httplog           #日志类别为http日志格式             
        option  dontlognull    #不记录健康检查日志信息
        retries 3           #检查节点服务器失败次数,连续达到3次,则反馈不可用 
        redispatch            #当服务器负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000        #最大连接数,此处的数值不能大于全局里的数值
        contimeout      5000  #设置连接超时时间,默认单位是毫秒
        clitimeout      50000 #设置客户端超时时间,默认单位是毫秒
        srvtimeout      50000 #设置服务器超时时间,默认单位是毫秒

#以下是新版本中的
timeout http-request 10s      #默认http请求超时时间
timeout queue 1m             #默认队列超时时间    
timeout connect 10s         #默认连接超时时间,新版本中替代
timeout client 1m
timeout server 1m
timeout http-keep-alive
timeout check 10s


defaults [<name>] #默认配置项,针对以下的frontend、backend和listen生效,可以多个name也可以没有name
frontend <name>   #前端servername,类似于Nginx的一个虚拟主机 server和LVS服务集群。
backend <name>   #后端服务器组,等于nginx的upstream和LVS中的RS服务器
listen   <name>   #将frontend和backend合并在一起配置,相对于frontend和backend配置更简
洁,生产常用


使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用
#官网业务访问入口
listen  webcluster 0.0.0.0:80
        option httpchk GET /test.html
        balance roundrobin
        server  inst1 192.168.240.11:80 check inter 2000 fall 3
        server  inst2 192.168.240.13:80 check inter 2000 fall 3

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值