Vagrant: port forwarding is working oddly - vagrant

I've got this in my Vagrantfile:
web.vm.network "private_network", ip: "192.168.33.20"
web.vm.network "forwarded_port", guest: 80, host: 8080
but on my Mac Host I'm able to access the web VM using 192.168.33.20:80 but NOT 192.168.33.20:8080.
Why is this not working as expected?

vagrant Port forwarding is used if you dont have static IP and you want to reach a specific port for a process bound to this specific port running on the VM, so you will access it using http://localhost:8080 and it will forward to port 80 on the VM.
When you have a static IP, you reach directly the network interface to this VM so it tries to connect you on the port 8080 directly in the VM, if you dont have anything running there, it wont show up.
Rules: if you use static IP (wether private or public network) you dont need port forwarding

Related

Vagrant : Connect from client to local https site

I am working on a chef project which is running in Vagrant. I have installed chef server on my local machine and I need to connect it from vagrant instance. I am getting a connection refused. I have added port-forwarding, but vagrant wont let me forward port-443 as it's already occupied. My chef server is running on https i.e port-443.
Error log :
[2019-05-03T10:21:00+00:00] INFO: Client key /etc/chef/client.pem is not present - registering
[2019-05-03T10:21:00+00:00] ERROR: Connection refused connecting to https://my-system-product-name/organizations/internetdevelopment/clients, retry 1/5
[2019-05-03T10:21:05+00:00] ERROR: Connection refused connecting to https://my-system-product-name/organizations/internetdevelopment/clients, retry 2/5
VagrantFile :
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 443, host: 8443
config.vm.network "forwarded_port", guest: 80, host: 8080
i would offer you to assign a static ip address to your vagrant virtual machine (vm) rather than use a port forwarding.
You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
It is up to the users to make sure that the static IP does not collide with any other machines on the same network.
While you can choose any IP you would like, you should use an IP from the reserved private address space. These IPs are guaranteed to never be publicly routable, and most routers actually block traffic from going to them from the outside world.

How configure Vagrant so doesnt block forwarded ports on host when on private network

I want a vagrant VM to behave like a box not hosted on my host machine.
I want to be able to do
curl <vagrant private ip> #nginx on the vm
and have that work from the host machine
curl localhost #e.g. iis
and have that work from the host machine
but it seems it's not possible as when you do port forwarding it always occupies the host port too on the loopback/or IP and you cant restrict it to just the private IP of the VM, basically, you cant have the VM running and start iis.
If that is the case ok, that's that.
currently, my Vagrant file has
config.vm.network "forwarded_port", guest: 80, host: 80
config.vm.network "private_network", ip: "192.168.33.11
I know the forwarded_port has guest_ip and host_ip settings but I didn't find they did anything when I tried to set to the private network IP.
Any suggestion of easy workaround also welcome.

How can I configure vagrant to use the same forwarded port for different nodes on different ips?

I would like 10.211.11.100:8080 to forward to port 4369 on the machine with ip 10.211.11.100.
I would like 10.211.11.101:8080 to forward to port 4369 on the machine with ip 10.211.11.101.
If I try something like this:
config.vm.network :forwarded_port, guest: 4369, host: 4369, host_ip: 10.211.11.100
config.vm.network :forwarded_port, guest: 4369, host: 4369, host_ip: 10.211.11.101
I get an error message like this:
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 4369 is already in use
on the host machine.
There must be a way to specify the same port number at different IPs. I would appreciate any help with this.
which vagrant version are you using ?
normally Support for port forwarding in an IP aliased environment issue has been fixed since vagrant 1.9.2

How to forward a port to vagrant guest if this port already listened by host machine

For example I have an app which run vagrant machines dynamically and expect some info from them will be sent through http to specific host machine port. So, my app listening specified port (http server) and I can't forward that port:
C:\node-vagrant-test-task>vagrant reload
==> default: Clearing any previously set forwarded ports... Vagrant cannot forward the specified ports on this VM, since they would
collide with some other application that is already listening on these
ports. The forwarded port to 8080 is already in use on the host
machine.
To fix this, modify your current project's Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:
config.vm.network :forwarded_port, guest: 8080, host: 1234
Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding. You could
try 'vagrant reload' (equivalent of running a halt followed by an up)
so vagrant can attempt to auto-correct this upon booting. Be warned
that any unsaved work might be lost.
If you do not want to mess with forwarding port, the best is to use a static IP for the guest
you can do either private or public networks with a static IP
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
or
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.50.4"
end
so you dont need to forward the 8080 port and can access your app directly on http://192.168.50.4:8080
If you need to then access the host machine for this guest, you can access it via 192.168.50.1

Vagrant forwarded_port on sub-machine

I couldn't manage to publish a virtual machine's port this way:
config.vm.define "n1" do |n1|
n1.vm.hostname = "n1"
n1.vm.network "private_network", ip: "172.20.20.10"
n1.vm.network "forwarded_port", guest: 8500, host: 8080
end
Access inside the VM works fine:
vagrant#n1:~$ curl http://localhost:8500/v1/health/state/any
but host access (outside of the VM, from my computer web browser) won't work:
http://localhost:8080/v1/health/state/any
Is what I try to achieve possible? Can somebody give me an hint, please?
I am not vagrant expert. BUT, in networking, you CANNOT forward port that bind to localhost(127.0.0.1) without explicitly mentioned it. Universal binding only works for 0.0.0.0.
You may try this, but I wouldn't guarantee it will works
n1.vm.network "forwarded_port", guest: 8500, guest_ip: 127.0.0.1, host: 8080
It is better to start your application binding to IP address then do the forwarding.

Resources