在 Docker Compose v1 中,/etc/hosts
文件使用链接的容器进行更新。例如。
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 redis redis_1 c381c79fb9c2 romantic_yonath
172.17.0.3 48d2ed7033a1
但是,在 v2 中,这是通过 DNS 完成的,因此不再有条目。我可以使用 hosts 表来引导负载均衡器;与
scale
结合使用时非常有用命令。在容器创建过程中是否有任何方法可以注入(inject)这些?
最佳答案
Nginx 博客在 Using DNS for Service Discovery with NGINX and NGINX Plus 上有这篇文章.
NGINX caches the DNS records until the next restart or configuration reload, ignoring the records’ TTL values.
解释您所看到的,在重新启动容器后,您可以路由到新节点。
在变量中设置域名部分提供了启动时缓存的解决方法示例。
resolver 10.0.0.2 valid=10s;
server {
location / {
set $backend_servers backends.example.com;
proxy_pass http://$backend_servers:8080;
}
}
When you use a variable to specify the domain name in the proxy_pass directive, NGINX re‑resolves the domain name when its TTL expires.
This discussion建议
127.0.0.11
将是容器内解析器的 IP。我在本地使用此配置来确认缩放后反射(reflect) DNS 更改,它似乎工作
resolver 127.0.0.11 valid=5s;
server {
listen 80;
location / {
set $application_servers application;
proxy_pass http://$application_servers:8080;
}
}
关于Docker Compose 在 v2 中注入(inject)链接容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40262935/