如何在docker-compose中设置DNS容器? [英] How do I set up a DNS container inside docker-compose?
问题描述
我认为这可能是一个XY问题,所以我将包括问题的上下文,因为我不知道解决这个问题的最佳方法。
I think this may be an XY problem, so I'll include the context of the question since I don't know the best way to solve this problem.
我在AWS上设置了一个kubernetes环境,这样我有两个部分,一个nginx容器和一个后端服务(我称之为SvcA)。由于后端服务可以来回,在我的nginx配置中,我有一些看起来像:
I have a kubernetes environment set up on AWS such that I have two parts, an nginx container, and a backend service (which I'll call SvcA). Since the backend service can come and go, in my nginx config I have something that looks like:
resolver kube-dns.kube-system.svc.cluster.local valid=60s ipv6=off;
server {
# stuff
location / {
set $backend "SvcA.default.svc.cluster.local:8000";
proxy_pass http://$backend;
}
}
此设置在kubernetes上运行良好,但我想要方式在我的本地机器上(几乎)完全相同,用于测试/开发,但没有使用kubernetes的所有开销。我想做的是将这两个容器(nginx,SvcA)粘贴到docker-compose文件中并使其工作。我遇到的问题是,nginx的解析器是硬编码为kubernetes的URL,我认为可能的工作的解决方案是容器是一个dns,它的唯一条目是指向 SvcA.default.svc.cluster.local到名称docker-compose assigns。
This setup works well on kubernetes, but I want a way have having the (almost) exact same set up on my local machine for testing/development but without all the overhead of using kubernetes. What I want to do is stick these two containers (nginx, SvcA) into a docker-compose file and have it working that way. The problem I've run into, is that the resolver for the nginx is hard-coded to be the url on kubernetes, and the solution I think may work is to have container that is a dns, and it's sole entry is to point "SvcA.default.svc.cluster.local" to the name docker-compose assigns.
我不确定这是解决问题的最佳方式,如果这是我不够了解DNS配置来设置这个。这是我问题的最佳解决方案吗?如果是这样,我如何配置一个dns服务器来处理这个问题?
I'm unsure if this is the best way to solve the problem, and if it is, I don't know enough about DNS configuration to set this up. Is this the best solution to my problem, and if so, how would I configure a dns server to handle this?
推荐答案
我不知道你是否需要一个dns。
i am not sure if you need a dns.
你可以这样做,即使URL是硬编码的
例如docker-compose.yml
you can do something like this, even if the URL is hardcoded eg docker-compose.yml
version: '2'
services:
service1:
...
networks:
back-tier:
ipv4_address: "172.16.238.10"
service2:
...
networks:
back-tier:
ipv4_adress: "172.16.238.11"
networks:
back-tier:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
gateway: 172.16.238.1
你可以甚至设置每个容器等的主机名,你也可以在它们之间建立一个链接。如果你需要它是循环的,你可以使用extra_hosts参数来实现。
you can even set hostnames for each container, etc, you could also make a link between them. if you need it to be circular you can do it with extra_hosts parameter.
如果应该能够找到你需要的所有信息:
https://docs.docker.com/compose/compose-file/
if should be able to find all the info you need over here: https://docs.docker.com/compose/compose-file/
如果容器在同一个网络上,他们应该能够相互交谈
if the containers are on the same network they should be able to talk to each other
这篇关于如何在docker-compose中设置DNS容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!