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_portbind 10.0.0.7:80balance roundrobinmode http
#不支持 tcp modelog globalcookie WEBSRV insert nocache indirectserver web1 10.0.0.17:80 check inter 3000 fall 2 rise 5 cookie web1server 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.211 FALSE / FALSE 0 WEBSRV web01[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.211 FALSE / FALSE 0 WEBSRV web02[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.7HTTP/1.1 200 OKdate: Thu, 02 Apr 2020 02:26:08 GMTserver: Apache/2.4.6 (CentOS)last-modified: Thu, 02 Apr 2020 01:44:28 GMTetag: "a-5a244f0fd5175"accept-ranges: bytescontent-length: 10content-type: text/html; charset=UTF-8set-cookie: WEBSRV=web2; path=/cache-control: private10.0.0.27[root@centos6 ~]#curl -i 10.0.0.7HTTP/1.1 200 OKdate: Thu, 02 Apr 2020 02:26:15 GMTserver: Apache/2.4.6 (CentOS)last-modified: Thu, 02 Apr 2020 01:44:13 GMTetag: "a-5a244f01f8adc"accept-ranges: bytescontent-length: 10content-type: text/html; charset=UTF-8set-cookie: WEBSRV=web1; path=/cache-control: private10.0.0.17[root@centos6 ~]#curl -b WEBSRV=web1 10.0.0.710.0.0.17[root@centos6 ~]#curl -b WEBSRV=web2 10.0.0.710.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-statusbind :9999stats 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 30stats 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连接数/单进程最大连接数/
最大管道数maxpipescurrent 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 信息说明
|