尝试从 Docker 容器中访问 etcd 时获取 "connection refused"

Getting "connection refused" when trying to access etcd from within a Docker container

我正在尝试从 运行ning Docker 容器中访问 etcd。当我运行

curl http://172.17.42.1:4001/v2/keys

我明白了

curl: (7) Failed to connect to 172.17.42.1 port 4001: Connection refused

我有另外四台主机工作正常,但是这台机器上的每个容器都有这个问题。真是一头雾水,不知道怎么调试。

我的etcd环境变量是

ETCD_ADVERTISE_CLIENT_URLS=http://10.242.10.2:2379
ETCD_DISCOVERY=https://discovery.etcd.io/<token_removed>
ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.242.10.2:2380
ETCD_LISTEN_CLIENT_URLS=http://10.242.10.2:2379,http://127.0.0.1:2379,http://0.0.0.0:4001
ETCD_LISTEN_PEER_URLS=http://10.242.10.2:2380

我也可以通过

从主机访问 etcd
curl http://localhost:4001/v2/keys

所以从容器路由到主机时似乎出现了一些错误。但我不知道它是什么。谁能指出我正确的方向?

原来 etcd 只在那台机器上监听 localhost:4001,这就是我无法从容器内访问它的原因。尽管我将其中一个监听客户端 url 配置为 0.0.0.0:4001.

原来我有运行sudo systemctl enable etcd2,导致它在云配置服务运行之前运行。因此,etcd 从默认配置开始,而不是我在云配置中指定的配置。 运行 sudo systemctl disable etcd2 修复了问题。

我观察到我必须使用 --advertise-client-urls 和 --listen-client-urls。像这样:

./etcd --advertise-client-urls 'http://0.0.0.0:2379,http://0.0.0.0:4001' --listen-client-urls 'http://0.0.0.0:2379,http://0.0.0.0:4001'

然后我就可以成功了

curl -L http://hostname:2379/version

来自任何可以访问该服务器并且它工作的机器。