haproxy的命令行管理以及同一用户的server stick

haproxy虽然没有提供单独的管理工具,但是实际上可以通过一个unix socket进行实时的命令控制,比如简单的查询,权重设置,disable/enable某个服务器

clear counters : clear max statistics counters (add ‘all’ for all counters)
clear table : remove an entry from a table
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
show info : report information about the running process
show stat : report counters for each proxy and server
show errors : report last request and response errors for each proxy
show sess [id] : report the list of current sessions or dump this session
show table [id]: report table usage stats or dump this table’s contents
get weight : report a server’s current weight
set weight : change a server’s weight
set timeout : change a timeout setting
set maxconn : change a maxconn setting
set rate-limit : change a rate limiting value
disable : put a server or frontend in maintenance mode
enable : re-enable a server or frontend which is in maintenance mode
shutdown : kill a session or a frontend (eg:to release listening ports)
要使用这个unix socket需要在global段配置
stats socket /opt/haproxy/etc/haproxy.stats level admin

然后就能通过

echo “show sess ” | socat unix-connect:./haproxy.stats stdio

来运行命令了。

比如

echo “set weight std1/ap2 50” | socat unix-connect:./haproxy.stats stdio

把bankend std1里的server ap2 的权重设置为50,至于权重的获取就是

# echo “get weight std1/ap2 ” | socat unix-connect:./haproxy.stats stdio
50 (initial 10)

disable某个服务器

echo ” disable server std1/ap2 ” | socat unix-connect:./haproxy.stats stdio

enbale某个服务器

echo ” enable server std1/ap2 ” | socat unix-connect:./haproxy.stats stdio
获取session信息
echo ” show sess” | socat unix-connect:./haproxy.stats stdio
另外还发现haproxy有几个 stick store-request, stick-table , stick match 也很有用。可以多个bankend共享一个表。

frontend st1
bind 0.0.0.0:1808
default_backend std1
frontend st2
bind 0.0.0.0:2808
default_backend std2

backend std1
balance roundrobin
mode tcp
server ser1 server1.net:80 weight 10
server ser2 server2.net:80 weight 10
stick on src table std2
backend std2
balance roundrobin
mode tcp
server ser2 server2.net:8080 weight 10
server ser1 server1.net:8080 weight 10

stick on src

stick-table type ip size 200k expire 30s

echo “show table std2 ” | socat unix-connect:./haproxy.stats stdio可以查看到对应的信息

如果不使用简写形式stick on src的话

就这样写:

backend std1
balance roundrobin
mode tcp
server ser1 server1.net:80 weight 10 check id 3
server ser2 server2.net:80 weight 10 check id 4
stick match src table std2
stick store-request src table std2
backend std2
balance roundrobin
mode tcp
server ser2 server2.net:8080 weight 10 check id 4
server ser1 server1.net:8080 weight 10 check id 3
stick match src table std2
stick store-request src table std2
stick-table type ip size 200k expire 30s

这个可以参考文档:

Examples :
# The following form …
stick on src table pop if !localhost

# …is strictly equivalent to this one :
stick match src table pop if !localhost
stick store-request src table pop if !localhost
# Use cookie persistence for HTTP, and stick on source address for HTTPS as
# well as HTTP without cookie. Share the same table between both accesses.
backend http
mode http
balance roundrobin
stick on src table https
cookie SRV insert indirect nocache
server s1 192.168.1.1:80 cookie s1
server s2 192.168.1.1:80 cookie s2

backend https
mode tcp
balance roundrobin
stick-table type ip size 200k expire 30m
stick on src
server s1 192.168.1.1:443
server s2 192.168.1.1:44

此条目发表在Haproxy分类目录。将固定链接加入收藏夹。

发表评论