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

clear counters : clear max statistics counters (add ‘all’ for all counters)clear table : remove an entry from a tablehelp : this messageprompt : toggle interactive mode with promptquit : disconnectshow info : report information about the running processshow stat : report counters for each proxy and servershow errors : report last request and response errors for each proxyshow sess [id] : report the list of current sessions or dump this sessionshow table [id]: report table usage stats or dump this table’s contentsget 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 valuedisable : put a server or frontend in maintenance modeenable : re-enable a server or frontend which is in maintenance modeshutdown : 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
来运行命令了。
比如
```bash
echo “set weight std1/ap2 50” | socat unix-connect:./haproxy.stats stdio
把bankend std1里的server ap2 的权重设置为50,至于权重的获取就是

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

disable某个服务器

echo ” disable server std1/ap2 ” | socat unix-connect:./haproxy.stats stdio
enbale某个服务器
```bash
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共享一个表。
stick on src

stick-table type ip size 200k expire 30s

```bash
echo “show table std2 ” | socat unix-connect:./haproxy.stats stdio可以查看到对应的信息
如果不使用简写形式stick on src的话
就这样写:
```bash
backend std1balance roundrobinmode tcpserver ser1 server1.net:80 weight 10 check id 3server ser2 server2.net:80 weight 10 check id 4stick match src table std2stick store-request src table std2backend std2balance roundrobinmode tcpserver ser2 server2.net:8080 weight 10 check id 4server ser1 server1.net:8080 weight 10  check id 3stick match src table std2stick store-request src table std2stick-table type ip size 200k expire 30s

这个可以参考文档: Examples :# The following form …stick on src table pop if !localhost