简体   繁体   English

在Vagrant上运行Consul:从客户端转发到不在Mac OS X上工作的主机端口

[英]Running Consul on Vagrant: Port forwarding from guest to host not working on Mac OS X

I would like to setup a Consul Cluster with a UI on my local machine (Mac OS X) using Vagrant. 我想使用Vagrant在本地计算机(Mac OS X)上设置带有UI的Consul Cluster。 So far I simply followed the Getting Started instructions of the official Consul Docs: https://www.consul.io/intro/getting-started/join.html 到目前为止,我只是按照官方Consul Docs的入门说明进行操作: https//www.consul.io/intro/getting-started/join.html

In order to be able to access the UI from the host, I simply added this one line to the provided Vagrantfile ( https://github.com/hashicorp/consul/blob/master/demo/vagrant-cluster/Vagrantfile ) 为了能够从主机访问UI,我只需将这一行添加到提供的Vagrantfile( https://github.com/hashicorp/consul/blob/master/demo/vagrant-cluster/Vagrantfile

n1.vm.network "forwarded_port", guest: 8500, host: 8500

To start Consul with a UI on the guest machine I ssh into the machine and then simply added the -ui flag to the provided command: 要在客户机上使用UI启动Consul,我将ssh插入机器,然后只需将-ui标志添加到提供的命令中:

consul agent -server -bootstrap-expect=1 \
    -data-dir=/tmp/consul -node=agent-one -bind=172.20.20.10 \
    -enable-script-checks=true -config-dir=/etc/consul.d -ui

Consul starts without problems and from with the guest machine I can execute: Consul启动没有问题,我可以执行以下客户机:

curl -v 'http://localhost:8500/ui/'

and the expected HTML page is returned. 并返回预期的HTML页面。

Trying to access http://localhost:8500/ui/ in a browser or via curl on the host does not work though. 尝试在浏览器中访问http://localhost:8500/ui/或通过主机上的curl访问不起作用。 Using curl on the host results in: 在主机上使用curl会导致:

*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8500 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8500 (#0)
> GET /ui/ HTTP/1.1
> Host: localhost:8500
> User-Agent: curl/7.54.0
> Accept: */*
> 
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

Anything I am missing with setting up the port forwarding correctly? 设置正确的端口转发我缺少什么? The logs after running vagrant up look good to me: 运行vagrant up后的日志对我来说很好看:

n1: Forwarding ports...
    n1: 8500 (guest) => 8500 (host) (adapter 1)

You have 2 options : 你有2个选择:

  1. Use a static IP (either a public network or private network ) so you will bind your consul agent on the static IP (say 172.20.20.10 ) and you will be able to access through http://172.20.20.10:8500/ui/ from your web browser 使用静态IP( 公共网络专用网络 ),因此您将在静态IP上绑定您的领事代理(例如172.20.20.10 ),您将可以通过http://172.20.20.10:8500/ui/访问从您的网络浏览器

Note : in such case, you dont need to have the forwarded port in your Vagrantfile 注意:在这种情况下,您不需要在Vagrantfile中拥有转发端口

  1. per consul doc on bind option 每个领事doc关于绑定选项

The address that should be bound to for internal cluster communications. 应该绑定到内部群集通信的地址。 This is an IP address that should be reachable by all other nodes in the cluster. 这是群集中所有其他节点都应该可以访问的IP地址。 By default, this is "0.0.0.0" , meaning Consul will bind to all addresses on the local machine 默认情况下,这是"0.0.0.0" ,这意味着Consul将绑定到本地计算机上的所有地址

so you will be able to reach consul running on your VM from your host 这样您就可以从主机上获取在VM上运行的领事

NB: my preferences goes for option #1 注意:我的偏好是选项#1

consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=agent-one -bind=172.20.20.10 -client=172.20.20.10 -enable-script-checks=true -config-dir=/etc/consul.d -ui is a good solution, but it has side effect that any consul cli operations will return error because it uses 127.0.0.1 in http request. consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=agent-one -bind=172.20.20.10 -client=172.20.20.10 -enable-script-checks=true -config-dir=/etc/consul.d -ui是一个很好的解决方案,但它有副作用,任何consul cli操作都会返回错误,因为它在http请求中使用127.0.0.1 so maybe changing -client=172.20.20.10 to -client=0.0.0.0 is better. 所以也许改变-client=172.20.20.10-client=0.0.0.0更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM