HAProxy 高级功能与配置
配置和验证的环境看这篇文章: HAProxy 各种调度算法介绍
一.基于 cookie 的会话保持
使用cookie
关键字来配置后端服务器基于 cookie 的会话持久连接。
cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
[ postonly ] [ preserve ] [ httponly ] [ secure ]
[ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]
[ dynamic ]
# name :需要被监控或者修改或者插入到客户端的cookie的名称。该cookie被通过响应头
# 设置"Set-Cookie"发送给客户端。并且被客户端在所有请求头中发送到服务器。
# insert :给参数指明如果客户端还没有允许访问本网站的cookie时由HAProxy在请求头插入
# 配置文件指明的cookie
# indirect :不会向客户端发送服务器已经处理过请求的cookie信息,间接
# nocache :当client和hapoxy之间有缓存时,不缓存cookie
配置示例
listen stats
mode http
bind 172.20.2.189:9999
stats enable
log global
stats uri /haproxy_status
stats auth haadmin:stevenux
frontend WEB_PORT_80
bind 172.20.2.189:8080
mode http
use_backend web_prot_http_nodes
backend web_prot_http_nodes
mode http
balance roundrobin
option forwardfor
cookie SRV-COOKIE insert indirect nocache maxidle 30m maxlife 8h
server node1 172.20.2.37:80 send-proxy cookie srv1-cookie weight 1 check inter 3000 fall 3 rise 5
server node2 172.20.2.43:80 cookie srv2-cookie weight 1 check inter 3000 fall 3 rise 5
server node3 172.20.2.44:80 cookie srv3-cookie weight 1 check inter 3000 fall 3 rise 5
server node4 172.20.2.45:80 cookie srv4-cookie weight 1 check inter 3000 fall 3 rise 5
访问测试–client-node1
[root@client-node1 ~]# curl --cookie "SRV-COOKIE=srv1-cookie" http://172.20.2.189:8080
node1 172.20.2.37
[root@client-node1 ~]# curl --cookie "SRV-COOKIE=srv4-cookie" 172.20.2.189:8080
node4 172.20.2.45
[root@client-node1 ~]# curl --cookie "SRV-COOKIE=srv3-cookie" 172.20.2.189:8080
node3 172.20.2.44
[root@client-node1 ~]# curl --cookie "SRV-COOKIE=srv2-cookie" 172.20.2.189:8080
访问测试–物理机 172.20.1.1
二.HAProxy 状态页配置和使用
HAProxy 提供了 WEB 信息界面,用来查看和管理后端服务器的状态信息。在配置
文件中使用stats
关键字指明各个控制状态页的参数。
2.1 状态页配置选项
stats 关键字配置参数有下面的功能
stats enable # 基于默认的参数启用stats page
# 默认参数使用如下:
- stats uri : /haproxy?stats
- stats realm : "HAProxy Statistics"
- stats auth : no authentication
- stats scope : no restriction
stats hide-version # 隐藏HAProxy版本
stats refresh <delay> # 设定WEB页面自动刷新时间间隔
stats uri <prefix> # 自定义stats page uri,默认值:/haproxy?stats
stats realm <realm> # 账户认证时的提示信息,示例:stats realm : HAProxy\ Statistics
stats auth <user>:<passwd> # 认证时的账号和密码,可使用多次,默认:no authentication
stats admin {
if | unless } <cond> # 启用stats page中的管理功能
2.2 启用状态页配置
配置示例
listen stats
mode http
log global
bind 172.20.2.189:9999
stats enable
stats refresh 5s
stats realm Status\ Page\ Input\ Passwd:
stats uri /haproxy_status
stats auth haadmin1:stevenux
stats auth haadmin2:s3cr3tpass
访问测试
如上图验证的界面,提示信息为stats realm
定义
2.3 状态页参数说明
非表格中的状态信息说明
pid = 46948 (process #1, nbproc = 1, nbthread = 2)
# pid 为当前进程pid号
# process 为当前进程编号
# nbproc 进程数
# nbthread 为线程数
uptime = 0d 0h02m26s
# HAProxy 启动以来经过的时间
system limits: memmax = unlimited; ulimit-n = 1023
maxsock = 1023; maxconn = 489; maxpipes = 0
# memmax 内存使用限制为无限制
# ulimit-n 最大打开文件数为1023
# maxsock 最大的socket连接数
# maxconn 单个进程最大的并发连接数
# maxpipes 最大的管道打开数
current conns = 1; current pipes = 0/0; conn rate = 1/sec; bit rate = 0.000 kbps
# current conns 当前进程的连接数
# current pipes 当前进程打开的管道数
# conn rate 连接速率
# bit rate 比特速率
Running tasks: 1/20; idle = 100 %
# 后端服务器显示颜色对应的状态
active UP:# 在线服务器
backup UP:# 标记为backup的服务器
active UP, going down:# 监测未通过正在进入down过程
backup UP, going down:# 备份服务器正在进入down过程
active DOWN, going up:# down的服务器正在进入up过程
backup DOWN, going up:# 备份服务器正在进入up过程
active or backup DOWN:# 在线的服务器或者是backup的服务器已经转换成了down状态
not checked:# 标记为不监测的服务器
active or backup DOWN for maintenance (MAINT) # active或者backup服务器人为下线的
active or backup SOFT STOPPED for maintenance # active或者backup被人为软下线(人为将weight改成0)
表格中的信息主要说明各后端服务器的详细状态
ession rate(每秒的连接会话信息) | Errors(错误统计信息) |
---|---|
cur:每秒的当前会话数量 | Req:错误请求量 |
max:每秒新的最大会话数量 | conn:错误链接量 |
limit:每秒新的会话限制量 | Resp:错误响应量 |
sessions(会话信息) | Warnings(警告统计信息) |
---|---|
cur:当前会话量 | Retr:重新尝试次数 |
max:最大会话量 | Redis:再次发送次数 |
limit: 限制会话量 | - |
Total:总共会话量 | - |
Server(real server 信息) |
---|
LBTot:选中一台服务器所用的总时间 |
Status:后端机的状态,包括 UP 和 DOWN |
Last:和服务器的持续连接时间 |
LastChk:持续检查后端服务器的时间 |
Wght:权重 |
Bytes(流量统计) |
---|
Act:活动链接数量 |
In:网络的字节输入总量 |
Bck:备份的服务器数量 |
Out:网络的字节输出总量 |
Chk:心跳检测时间 |
Dwn:后端服务器连接后都是 DOWN 的数量 |
Denied(拒绝统计信息) |
---|
Dwntme:总的 downtime 时间 |
Req:拒绝请求量 |
Thrtle:server 状态 |
Resp:拒绝回复量 |
2.4 修改报文配置
在 http 模式下,基于实际需求修改客户端的请求报文与响应报文,通过 reqadd
和 reqdel 在请求报文添加删除字段,通过 rspadd 与 rspidel 在响应报文中添
加与删除字段。
在请求报文尾部添加指定首部
reqadd