haprox external-check 健康检测

haprox external-check 健康检测可以自定义脚本来检测后端服务,这非常有利于那些经常出现假死,无法通过常规判断端口来检测后台是否正常的问题。
比如,RabbitMQ,业务程序自定义检查等。

HAProxy可以使用二进制或脚本运行外部命令来执行运行状况检查。 当这样做的时候,它需要将自己fork一个新进程。
如果您使用chroot,请确保该命令及其所有依赖项在chroot中可用。
以下指令可用:

In the global section:
external-check: Allows the use of the external check feature. It is disabled by default for security purposes.

In the defaults, backend, or listen section:

option external-check: Enables the use of an external command to perform health check
external-check command : Name of the command to run.
下面的参数被传递给命令:

写了一个bash脚本测试,发现在backend那里传了4个参数,如下,只有后面两个可用
Quoted Values: NOT_USED NOT_USED 172.20.2.71 5672
Total Number of Parameters : 4

where:

and are set only when this directive is configured in a listen section.
只有在侦听部分配置了此伪指令时,才会设置 和 。
Their values are derived from the first listener (bind) that is either IPv4, IPv6 or a UNIX socket.
它们的值来源于IPv4,IPv6或UNIX套接字的第一个侦听器(绑定)。
In the case of a UNIX socket listener, the is the path of the socket, and the is set to NOT_USED.
In a back end section where there is no bind, both are set to NOT_USED.
and are set to the server address and port dedicated to health checking
Other information is provided through the environment variable which you can use to run a more accurate health check sequence:

HAPROXY_PROXY_ADDR: first bind address, available when used in a listen section
HAPROXY_PROXY_ID: back end UUID
HAPROXY_PROXY_NAME: back end name
HAPROXY_PROXY_PORT: first bind port, available when used in a listen section
HAPROXY_SERVER_ADDR: server address
HAPROXY_SERVER_CURCONN: current active connections on the server
HAPROXY_SERVER_ID: server UUID
HAPROXY_SERVER_MAXCONN: server’s maxconn
HAPROXY_SERVER_NAME: server name
HAPROXY_SERVER_PORT: server port (empty for a UNIX socket)
PATH: PATH environment variable set by external-check path

The exit code is used to determine the server’s operational status:
0: check is considered as positive
any other value is considered as a failure

退出代码用于确定服务器的运行状态:
0:检查被认为是正面的
任何其他值被视为失败

In the external-check path : set the PATH environment variable of to . Default path is “”.
在外部检查路径 中:将的PATH环境变量设置为 。 默认路径是“”。
比如python的路径

Example
Run a bash script that needs simple commands like ping to perform health checking:

global
	# chroot /var/empty  # disable chroot
	external-check
	[...]
	backend b_myapp
	option external-check
	external-check path "/usr/bin: /bin:/usr/bin/"
	external-check command /etc/hapee-1.5/scripts/check.sh"

文章来源:

Author:JavasBoy
link:https://www.liurongxing.com/haprox-external-check.html