gpt4 book ai didi

docker - 使用内部 : true on default docker-compose network leads to slow DNS timeouts

转载 作者:行者123 更新时间:2023-12-02 20:00:19 24 4
gpt4 key购买 nike

背景

我们正在尝试在 docker-compose 设置中针对相当复杂的应用程序堆栈运行测试套件。到目前为止,一切都运行良好。但是,我们想确保我们没有遗漏任何外部依赖项或不小心戳到任何我们不应该的东西(例如生产系统)。

这似乎是什么internal网络选项基本上是为。

问题

使用时 internal: true在 compose 网络上,容器无法按预期访问任何外部主机。到现在为止还挺好。问题是尝试解析组合网络中不存在的主机名需要很长时间才能超时。 A gethostbyname()调用最多可能需要 40 秒才能失败,这会以各种方式破坏应用程序堆栈。

我希望能够在网络中配置 DNS 设置,以便直接给我一个 NXDOMAIN对于不是网络中容器的任何名称,但到目前为止我还没有发现任何迹象表明这是可能的。这似乎是一个明显的用例,我很难相信我是第一个遇到这个问题的人......

我在谷歌上搜索了高低并浏览了我能找到的任何文档,但到目前为止,没有运气。

最少的测试设置

给定一个 docker-compose.yml :

version: '3'

networks:
default:
internal: true

services:
foo:
image: debian:latest
command: /bin/sleep 1000000000

bar:
build:
context: ./bar
command: /bin/true

bar/Dockerfile :
FROM debian:latest
RUN apt-get update && apt-get install -y dnsutils bind9-host netcat-openbsd

然后:
$ docker-compose up -d
Creating network "foo_default" with the default driver
Creating foo_bar_1 ... done
Creating foo_foo_1 ... done
$ docker-compose run bar bash
root@f7f6bf6b65d4:/# time nslookup foo
Server: 127.0.0.11
Address: 127.0.0.11#53

Non-authoritative answer:
Name: foo
Address: 192.168.96.3

real 0m0.010s
user 0m0.004s
sys 0m0.000s
root@f7f6bf6b65d4:/#

即时答复。到现在为止还挺好。

但:
root@f7f6bf6b65d4:/# time nslookup foo.example.com 127.0.0.11
;; connection timed out; no servers could be reached

real 0m15.009s
user 0m0.004s
sys 0m0.004s
root@f7f6bf6b65d4:/#

坏的。希望瞬间 NXDOMAIN回复。

甚至更糟:
root@f7f6bf6b65d4:/# time getent hosts foo.example.com

real 0m40.034s
user 0m0.000s
sys 0m0.000s
root@f7f6bf6b65d4:/#

如前所述,这会以多种方式破坏我们的堆栈并大大减慢测试运行速度。

作为引用,容器的 /etc/resolv.conf是:
nameserver 127.0.0.11
options ndots:0

/etc/nssswitch.conf是:
passwd:         compat
group: compat
shadow: compat
gshadow: files
hosts: files dns
networks: files
protocols: db files
services: db files
ethers: db files
rpc: db files
netgroup: nis

最佳答案

我已经在我的 Ubuntu 18.04 LTS 上测试了这个设置,但我无法重现您的问题。

我得到:

root@cedfc68cfa30:/# time nslookup foo.example.com 127.0.0.11
Server: 127.0.0.11
Address: 127.0.0.11#53

** server can't find foo.example.com: NXDOMAIN


real 0m0.025s
user 0m0.008s
sys 0m0.016s

经过一些测试,结果表明 systemd-resolved 做了所需的魔法来完成这项工作。

如果停止 systemd-resolved 并使用外部 DNS 解析器,docker 将发送带有损坏源 IP 的 DNS 查询,因为 docker 网络的 iptables 中没有 MASQUERADE 规则,然后您必须等待超时。

关于docker - 使用内部 : true on default docker-compose network leads to slow DNS timeouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54234328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com