Search code examples
macosvagrantportforwardingconsul

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. So far I simply followed the Getting Started instructions of the official 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)

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:

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:

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

and the expected HTML page is returned.

Trying to access http://localhost:8500/ui/ in a browser or via curl on the host does not work though. Using curl on the host results in:

*   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:

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

Solution

  • You have 2 options :

    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

    Note : in such case, you dont need to have the forwarded port in your Vagrantfile

    1. per consul doc on bind option

    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. By default, this is "0.0.0.0", meaning Consul will bind to all addresses on the local machine

    so you will be able to reach consul running on your VM from your host

    NB: my preferences goes for option #1