HAProxy Lua 如何更改服务器端口?

HAProxy Lua how to change server port?

Server.set_addr (sv, addr)

我不明白该方法将什么作为输入。 看起来 Server.get_addr(sv) returns ip:port - 所以你还需要传递 ip:port,但没有。这不起作用。

文档内容如下: See the documentation for the control socket for more information on the string format.

嗯,我看到你需要转移ip port: port(例如:127.0.0.1 port 80)。但这也不管用。

唯一有效的方法是仅传递 ip(示例:127.0.0.1)。

问题是如何更改端口?

代码示例:(如果不起作用 - 日志中没有任何条目)

-- work
-- log entry: changed its IP from 1.1.1.1 to 2.2.2.2 by Lua script.
local newAdr = backendServer.ip
server:set_addr(newAdr)

-- not work
local newAdr = backendServer.port
server:set_addr(newAdr)

-- not work
local newAdr = backendServer.ip .. ":" .. backendServer.port
server:set_addr(newAdr)

-- not work
local newAdr = backendServer.ip .. " " .. backendServer.port
server:set_addr(newAdr)

-- not work
local newAdr = backendServer.ip .. " port " .. backendServer.port
server:set_addr(newAdr)

更新: 从 HAProxy 2.2dev7 开始,此功能现已合并:set_addr 采用额外的可选参数来指定新端口。


目前无法通过 Lua 执行此操作。查看 HAProxy 的源代码可以发现 set_addr calls hlua_server_set_addr, which calls server_parse_addr_change_request, which calls update_server_addr, which only updates the address and not the port. Contrast this with the management socket's set server, which calls cli_parse_set_server, which calls update_server_addr_port,它也会更新端口。

不过,有两个好消息:

  1. 由于管理套接字可以做到,您可以将其用作解决方法。
  2. 修改 HAProxy 的 Lua 接口以支持更新端口将非常简单,因为所有逻辑都已经存在。我将 a patch that does so 发送到他们的邮件列表。