docker - 无法从主机查询 consul DNS 接口(interface)

我可以查询我的远程领事容器的其余服务,即。

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_addraddresses.dns 设置。

如果您没有设置 addresses.dns 条目,则默认为 client_addrclient_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 to client_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/

相关文章:

java - 从另一个项目注入(inject) FeignClient 时出错

java - REST - POST 到资源集合和 If-Match header 时的 ETag

f# - 使用寓言中的节点模块

c# - 使用 Entity Framework 定义关系/FK 的两端是否重要?

python - 查找文本是否突出显示

c# - WCF 服务在多服务器环境中不起作用

android - 键盘打开android时布局正在缩小

r - 如何在 R 中创建边际效应表?

c# - app.config 的困境 - 为什么我不能嵌入应用程序配置?

reactjs - 每个 child 在 React 中应该有一个唯一的关键错误