如何在同一主机上的服务的consul容器中定义HTTP健康检查? [英] how to define HTTP health check in a consul container for a service on the same host?
问题描述
我们正在一个也运行服务的主机上使用一个领事代理。 (RabbitMQ)
要验证服务是否准备好,我们已经定义了基于卷曲的健康检查。
但是,我们正在使用注册器使用env变量注入此检查。
SERVICE_CHECK_SCRIPT = curl hostname
:15672 /....
We are using a consul agent on a host that also runs a service. (RabbitMQ)
To verify that the service is ready we have defined a curl based health check.
however, we are using the registrator to inject this check using env variable.
SERVICE_CHECK_SCRIPT=curl hostname
:15672/....
问题是,我们也向总领事告诉其主机名与主机相同。
(我们必须拥有此功能,因为我们希望看到正确的主机名注册到领事馆。
problem is, we've also told the consul-agent that its hostname is the same as the host. (We must have this feature since we want to see the correct hostname registered with the consul cluster.
当领事代理运行健康检查时,它看起来对于自己的容器上的URL ...
这显然是失败...
有人知道如何定义这个健康检查(我们正在使用mesos来做到这一点),以便curl将尝试连接到右边的ip?
When the consul agent runs the health check, it looks for the URL on its own container... this obviously fails... does anybody knows how to define this health check (we are using mesos to do it) so that curl will attempt to connect to the right ip?
推荐答案
你可以使用注册人的 HTTP健康检查(前提是您在 容器),例如:
You can use Registrator's HTTP health check (provided that you run Consul inside progrium/docker-consul
container), for example:
ENV SERVICE_CHECK_HTTP=/howareyou
ENV SERVICE_CHECK_INTERVAL=5s
此检查将运行 check-http脚本由 docker-consul
提供,其中使用Docker API解析目标容器的IP和端口。
This check will run check-http script provided by docker-consul
, which resolves target container's IP and port using the Docker API.
另一个选项是配置领事DNS来监听Docker的网桥IP(已经在 progrium / docker-consul
的 运行
脚本),并启动Docker守护进程,其全局DNS选项指向相同的IP,例如 docker -d --bip 10.0.42.1/24 --dns 10.0.42.1 --dns 8.8.8.8
。
The other option is to configure Consul DNS to listen on Docker's bridge IP (which is already done in progrium/docker-consul
's run
script), and launch Docker daemon with the global DNS option pointing to the same IP, e.g. docker -d --bip 10.0.42.1/24 --dns 10.0.42.1 --dns 8.8.8.8
.
之后,领事DNS将在您的任何容器中可用,包括 docker-consul
一个,这意味着您可以运行 curl http: //my-app.service.consul:12345/howareyou
,甚至查询 SRV
记录,例如使用 dig -t SRV
,以获取服务的IP和端口。然后,您可以将 10.0.42.1
添加到主机的 resolv.conf
中,以获得不仅可以查询领事DNS
After that Consul DNS will be available in any of your containers, including the docker-consul
one, which means that you can run curl http://my-app.service.consul:12345/howareyou
, or even query SRV
records, e.g. with dig -t SRV
, to obtain both IP and port of a service. And then you may add 10.0.42.1
to the host's resolv.conf
to gain the ability to query Consul DNS not only within containers, but from the host OS too.
首选方法imho是使用注册人的 SERVICE_CHECK_HTTP
,但是为您的容器设置领事DNS是值得的。
The preferred method, imho, is to use Registrator's SERVICE_CHECK_HTTP
, but setting up Consul DNS for your containers is worth doing anyway.
这两种方法都需要在每个应用程序主机上以客户端模式运行Consul代理,但是领事代理运行在这种模式下,使用这么少的系统资源,你几乎不会注意到它。您还需要它来执行健康检查:)
Both these methods require running Consul agent in client mode on each of your application hosts, but Consul agent, when ran in this mode, uses such a tiny amount of system resources that you'll hardly notice it. And you need it anyway to perform health checks :)
这篇关于如何在同一主机上的服务的consul容器中定义HTTP健康检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!