自学内容网 自学内容网

【HAProxy07】企业级反向代理HAProxy高级功能之Cookie 会话保持与HAProxy 状态页

HAProxy 高级功能

介绍 HAProxy 高级配置及实用案例

基于 Cookie 的会话保持

cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址 hash 调度算法对客户端的粒度更精准,但同时也加重了haproxy负载,目前此模式使用较少, 已经被 session共享服务器代替

注意:不支持 tcp mode,使用 http mode

配置选项

cookie name  [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [ 
preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ]

 name:  #cookie 的 key名称,用于实现持久连接     
insert: #插入新的cookie,默认不插入cookie    
indirect:   #如果客户端已经有cookie,则不会再发送cookie信息
nocache:    #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,

因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

配置示例

listen  web_port
 bind 10.0.0.7:80
 balance  roundrobin
 mode http                              
#不支持 tcp mode
 log global
 cookie WEBSRV insert nocache indirect
 server web1  10.0.0.17:80 check inter 3000 fall 2 rise 5  cookie web1
 server web2  10.0.0.27:80 check inter 3000 fall 2 rise 5  cookie web2

范例一: 通过命令行验证

[root@master-db ~]#curl -c /root/cookie.txt 172.16.1.211
nginx 10.0.0.52 WEBSITE v3.0
[root@master-db ~]#cat cookie.txt 
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

172.16.1.211FALSE/FALSE0WEBSRVweb01

[root@master-db ~]#curl -b /root/cookie.txt 172.16.1.211
nginx 10.0.0.52 WEBSITE v3.0

[root@master-db ~]#curl -c /root/cookie.txt 172.16.1.211
nginx 10.0.0.53 WEBSITE v3.0
[root@master-db ~]#cat cookie.txt 
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

172.16.1.211FALSE/FALSE0WEBSRVweb02

[root@master-db ~]#curl -b /root/cookie.txt 172.16.1.211
nginx 10.0.0.53 WEBSITE v3.0

范例二 通过命令行验证

[root@centos6 ~]#curl -i 10.0.0.7
 HTTP/1.1 200 OK
 date: Thu, 02 Apr 2020 02:26:08 GMT
 server: Apache/2.4.6 (CentOS)
 last-modified: Thu, 02 Apr 2020 01:44:28 GMT
 etag: "a-5a244f0fd5175"
 accept-ranges: bytes
 content-length: 10
 content-type: text/html; charset=UTF-8
 set-cookie: WEBSRV=web2; path=/
 cache-control: private
 10.0.0.27
 [root@centos6 ~]#curl -i 10.0.0.7
 HTTP/1.1 200 OK
 date: Thu, 02 Apr 2020 02:26:15 GMT
 server: Apache/2.4.6 (CentOS)
 last-modified: Thu, 02 Apr 2020 01:44:13 GMT
 etag: "a-5a244f01f8adc"
 accept-ranges: bytes
 content-length: 10
 content-type: text/html; charset=UTF-8
 set-cookie: WEBSRV=web1; path=/
 cache-control: private
 10.0.0.17
 [root@centos6 ~]#curl -b WEBSRV=web1 10.0.0.7
 10.0.0.17
 [root@centos6 ~]#curl -b WEBSRV=web2 10.0.0.7
 10.0.0.27
 [root@centos6 ~]#curl -vb  WEBSRV=web1 10.0.0.7
 * About to connect() to 10.0.0.7 port 80 (#0)
 *   Trying 10.0.0.7... connected
 * Connected to 10.0.0.7 (10.0.0.7) port 80 (#0)
 > GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 
zlib/1.2.3 libidn/1.18 libssh2/1.4.2
 > Host: 10.0.0.7
 > Accept: */*
 > Cookie: WEBSRV=web1
 > 
< HTTP/1.1 200 OK
 < date: Thu, 02 Apr 2020 02:27:54 GMT
 < server: Apache/2.4.6 (CentOS)
 < last-modified: Thu, 02 Apr 2020 01:44:13 GMT
 < etag: "a-5a244f01f8adc"
 < accept-ranges: bytes
 < content-length: 10
 < content-type: text/html; charset=UTF-8
 < 
10.0.0.17
 * Connection #0 to host 10.0.0.7 left intact
 * Closing connection #0
 [root@centos6 ~]#curl -vb  WEBSRV=web2 10.0.0.7
 * About to connect() to 10.0.0.7 port 80 (#0)
 *   Trying 10.0.0.7... connected
 * Connected to 10.0.0.7 (10.0.0.7) port 80 (#0)
 > GET / HTTP/1.1
 > User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1 
zlib/1.2.3 libidn/1.18 libssh2/1.4.2
 > Host: 10.0.0.7
 > Accept: */*
 > Cookie: WEBSRV=web2
 > 
< HTTP/1.1 200 OK
 < date: Thu, 02 Apr 2020 02:27:57 GMT
 < server: Apache/2.4.6 (CentOS)
 < last-modified: Thu, 02 Apr 2020 01:44:28 GMT
 < etag: "a-5a244f0fd5175"
 < accept-ranges: bytes
 < content-length: 10
 < content-type: text/html; charset=UTF-8
 < 
10.0.0.27
 * Connection #0 to host 10.0.0.7 left intact
 * Closing connection #0

 

HAProxy 状态页

通过web界面,显示当前HAProxy的运行状态

官方帮助:

http://cbonte.github.io/haproxy-dconv/2.4/configuration.html#4-stats%20admin
http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#4-stats%20admin

状态页配置项

stats enable            #基于默认的参数启用stats page    
stats hide-version         #将状态页中haproxy版本隐藏  
stats refresh <delay>     #设定自动刷新时间间隔,默认不自动刷新,以秒为单位  
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中的管理功能

 

启用状态页示例

listen haproxy-status
  bind :9999
  stats enable
 #stats hide-version 
  stats uri  /haproxy-status           
  stats realm HAProxy\ Stats\ Page     
  stats auth haadmin:123456            
#自定义stats page uri
 #账户认证时的提示信息
#支持多个用户
  stats auth admin:123456
 #stats refresh 30
  stats admin if TRUE                  

登录状态页说明

pid = 27134 (process #1, nbproc = 1, nbthread = 1) #pid为当前pid号,process为当前进
程号,nbproc和nbthread为一共多少进程和每个进程多少个线程
uptime = 0d 0h00m04s #启动了多长时间
system limits: memmax = unlimited; ulimit-n = 200029 #系统资源限制:内存/最大打开文件
数/
 maxsock = 200029; maxconn = 100000; maxpipes = 0 #最大socket连接数/单进程最大连接数/
最大管道数maxpipes
 current conns = 2; current pipes = 0/0; conn rate = 2/sec; bit rate = 0.000 kbps 
#当前连接数/当前管道数/当前连接速率
Running tasks: 1/14; 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)

Backend Server 信息说明

session 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:拒绝回复量

 

 

 

 


原文地址:https://blog.csdn.net/weixin_74814027/article/details/143731770

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!