3

我一直在尝试为“微服务”创建一个环境。其中两个关键组件是DockerConsul,它们都在虚拟机上运行(我使用Vagrant创建了这个 vm)。当我 ssh 进入 vm 时,我可以使用dig命令行工具访问Consul提供的 DNS 接口。

问题是:我希望能够从我的主机访问 DNS 接口。我已经为 vm 提供了一个静态 IP,但这并没有解决我的问题。我还打开了我的虚拟机的 53 端口,这也没有解决我的问题。

这是我的 vm 的Vagrant设置脚本:

config.vm.define :app1 do |app1|
# Every Vagrant virtual environment requires a box to build off of.
app1.vm.box = "ubuntu/trusty64"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
app1.vm.network :forwarded_port, guest: 3000, host: 3000
app1.vm.network :forwarded_port, guest: 53, host: 53
app1.vm.network :forwarded_port, guest: 8500, host: 8500
app1.vm.network :forwarded_port, guest: 15672, host: 15672

# Open 32 ports for Docker containers
for port in 32768..32800
  app1.vm.network :forwarded_port, guest: port, host: port
end

# Create a private network, which allows host-only access to the machine
# using a specific IP.
app1.vm.network :private_network, ip: "200.0.0.1"

app1.vm.hostname = "server-1"

config.vm.provider "virtualbox" do |v|
  v.memory = 1024
end

这就是我使用Consul启动Docker 容器的方式:

sudo docker run --name consul -h $HOSTNAME -p 0.0.0.0:8300:8300 -p 0.0.0.0:8301:8301 -p 0.0.0.0:8301:8301/udp -p 0.0.0.0:8302:8302 -p 0.0.0.0:8302:8302/udp -p 0.0.0.0:8400:8400 -p 0.0.0.0:8500:8500 -p 0.0.0.0:53:53 -p 0.0.0.0:53:53/udp -d progrium/consul -advertise 200.0.0.1 -server -bootstrap

我尝试在主机上使用以下内容进行挖掘:

dig @200.0.0.1 someservice.service.consul SRV

这回应:

; <<>> DiG 9.9.5-3ubuntu0.2-Ubuntu <<>> @200.0.0.1 someservice.service.consul ANY
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

有什么方法可以让我的主机可以访问Consul DNS 接口?任何帮助将不胜感激。

4

1 回答 1

4

这对我有用:

$ docker pull gliderlabs/consul
$ docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53/udp --name node1 -h node1 gliderlabs/consul agent -server -bootstrap -client=0.0.0.0 -data-dir /tmp/consul
$ curl '127.0.0.1:8500/v1/catalog/nodes' 
$ # returns [{"Node":"node1","Address":"172.17.0.34"}]

现在 dns 在容器 IP (172.17.0.34) 上可用,不知道为什么不在 0.0.0.0:

$ dig   @172.17.0.34 -p 8600 node1.node.consul
$ # returns:

; <<>> DiG 9.9.5-3ubuntu0.3-Ubuntu <<>> @172.17.0.34 -p 8600 node1.node.consul
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4812
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;node1.node.consul.     IN  A

;; ANSWER SECTION:
node1.node.consul.  0   IN  A   172.17.0.34

;; Query time: 0 msec
;; SERVER: 172.17.0.34#8600(172.17.0.34)
;; WHEN: Wed Jul 22 08:23:51 CEST 2015
;; MSG SIZE  rcvd: 68
于 2015-07-22T06:26:36.070 回答