我可以查询我的远程领事容器的其余服务,即。
curl my-ip:8500/v1/catalog/services
{"consul":[],"nginx-80":[],"redis":[]}
curl my-ip:8500/v1/catalog/service/redis
[{"Node":"consul","Address":"172.17.0.3","ServiceID":"954f32f077ed:redis:6379","ServiceName":"redis","ServiceTags":null,"ServiceAddress":"","ServicePort":32768}]
但是当我尝试consul的dns接口(interface)时
dig @my-ip -p 8600 redis.service.consul
; <<>> DiG 9.8.3-P1 <<>> @45.33.119.251 -p 8600 redis.service.consul
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached
我不确定是我查询的格式不正确还是我启动容器的方式有问题。
最佳答案
检查您的 Consul 配置,看看您是否没有设置 client_addr
或 addresses.dns
设置。
如果您没有设置 addresses.dns
条目,则默认为 client_addr
。 client_addr
默认为 127.0.0.1
,因此它仅在环回地址上可用。如果你在 Docker 中运行 Consul(例如,如果你像我一样使用 https://github.com/hashicorp/learn-consul-docker/tree/main/datacenter-deploy-service-discovery 工作)你可能会误以为你已经正确设置了它,特别是因为你的 netstat 显示 TCP 和 UDP 0.0.0.0:8600 上的监听器,但您仍然无法从容器外部连接到它。
引用资料
https://www.consul.io/docs/agent/config/config-files#dns
dns
- The DNS server. Defaults toclient_addr
https://www.consul.io/docs/agent/config/config-files#client_addr
client_addr
Equivalent to the -client command-line flag.
https://www.consul.io/docs/agent/config/cli-flags#_client
-client
- The address to which Consul will bind client interfaces, including the HTTP and DNS servers. By default, this is "127.0.0.1", allowing only loopback connections.
因此,您应该能够将 client_addr
设置为 0.0.0.0
(或您尝试绑定(bind)的任何 IP),或者添加一个 addresses.dns
设置如下:
{
...
"addresses": {
"dns": "0.0.0.0"
}
...
}
配置 Consul 以响应来自环回地址之外的 DNS 查询。
https://stackoverflow.com/questions/38840922/