如何通过命令行从 haproxy 中删除平衡节点?

isN*_*247 50 load-balancing haproxy

我有 haproxy 作为我的负载平衡器运行,并且从 haproxy 附带的 stats Web 界面,我可以将 Web 服务器置于维护模式(并再次将其恢复) - 这太棒了!

但是,我还希望能够从命令行执行相同的操作(用于某些自动部署工作流)。这可能吗,如果可以,怎么办?

非常感谢

isN*_*247 64

更新(2012 年 8 月 28 日):我现在倾向于使用haproxyctl,它利用了下面描述的方法。


经过更多的研究,我已经修复了它,对于其他有同样问题的人:-

您可以在配置中添加一个 unix 套接字,然后与之交互(这里是可能的命令)。

建立:

sudo nano /etc/haproxy/haproxy.cfg
Run Code Online (Sandbox Code Playgroud)

在您的“全局”部分添加:

stats socket /etc/haproxy/haproxysock level admin
Run Code Online (Sandbox Code Playgroud)

重新启动您的 haproxy 守护进程(例如sudo service haproxy restart

现在你需要 socat(如果你没有它,就apt-get install socat在 Ubuntu 上)。

现在你需要做的就是发出这个命令来关闭一个节点:

echo "disable server yourbackendname/yourservername" | socat stdio /etc/haproxy/haproxysock
Run Code Online (Sandbox Code Playgroud)

要恢复它,请在上面的命令中将 disable 替换为 enable。


Kyl*_*ndt 21

除了beardwizzle 的echo 方法,你还可以交互地做到这一点:

root@ny-lb01:/etc/haproxy# sudo socat readline /var/run/haproxy.stat 

prompt
> set timeout cli 1d
> disable server foo/web01
> help
Unknown command. Please enter one of the following commands only :
  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
  disable server : set a server in maintenance mode
  enable server  : re-enable a server that was previously in maintenance mode
Run Code Online (Sandbox Code Playgroud)

  • 这里的一个_大_陷阱是 Debian 的 socat 不支持“readline”,尽管它在手册页中这么说。由于 libreadline (GPL) 和 OpenSSL 之间的许可证冲突,他们修补了它。在这种情况下,您对每个命令都使用 `socat /var/run/haproxy.stat stdio` (7认同)

slm*_*slm 9

如果您只能访问 netcat ( nc),您可以使用它以类似于socat.

$ echo "show info" | nc -U /var/lib/haproxy/stats | head -10
Name: HAProxy
Version: 1.5.2
Release_date: 2014/07/12
Nbproc: 1
Process_num: 1
Pid: 29745
Uptime: 0d 0h14m35s
Uptime_sec: 875
Memmax_MB: 0
Ulimit-n: 8034
Run Code Online (Sandbox Code Playgroud)

要禁用服务器:

$ echo "enable server bk_dservers/ds02" | nc -U /var/lib/haproxy/stats
Run Code Online (Sandbox Code Playgroud)

请注意确保套接字文件具有适当的访问级别来执行上述操作。主要是这样的:

stats       socket /var/lib/haproxy/stats level admin
Run Code Online (Sandbox Code Playgroud)

否则你会得到权限被拒绝的错误:

$ echo "disable server bk_dservers/ds02" | nc -U /var/lib/haproxy/stats
Permission denied

$
Run Code Online (Sandbox Code Playgroud)

参考


小智 7

简单的方法是:

1 - 如果存在名为 maintenance.html 的文件(例如),则配置您的 Web 服务器以返回 503 代码。使用 apache,您可以执行以下操作:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{ENV:REDIRECT_STATUS} !=503
  RewriteCond "/var/www/maintenance.html" -f
  RewriteRule ^(.*)$ /$1 [R=503,L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

2 - 配置您的 haproxy 后端以检查 URL 而不是仅检查端口,如下所示:

backend site
    balance roundrobin
    option httpchk GET /index.html
    server myserver1.example.com 192.0.2.1:80 cookie S1 check inter 2000 fall 3
    server myserver2.example.com 192.0.2.2:80 cookie S2 check inter 2000 fall 3
Run Code Online (Sandbox Code Playgroud)

3 - 重新启动您的网络服务器和负载平衡器。

4 - 将您的 Web 服务器置于维护模式。

touch /var/www/maintenance.html
Run Code Online (Sandbox Code Playgroud)

5 - 从维护模式中删除您的 Web 服务器。

rm -f /var/www/maintenance.html
Run Code Online (Sandbox Code Playgroud)