vagrant port forwarding guest and host port messed up - vagrant

I'm using a vagrant trusty64 box with nginx, flask, gunicorn and port forwarding is not working as expected
In the vagrant file, I have:
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
On the box, I have run:
gunicorn myprj:app -b 10.10.1.10:8090
find nothing on host machine with http://10.10.1.10:3100
trying curl -v http://10.10.1.10:3100 gives the following output:
connect to 10.10.1.10 port 3100 failed: Connection refused
Reachable with guest port on host machine http://10.10.1.10:8090
I am new to the vagrant, did I miss/mess to Vagrant file.
Complete Vagrant file:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.box_check_update = false
config.vm.network "forwarded_port", guest: 8090, host: 3100
config.vm.network "private_network", ip: "10.10.1.10"
config.vm.synced_folder ".", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "lopamudra_dev"
end
config.vm.provision "shell", inline: <<-SHELL
# Upgrading the environment
apt-get update
apt-get upgrade
# Installing nginx + uwsgi + flask
apt-get install -y python-pip python-dev nginx
pip install uwsgi flask gunicorn
SHELL
end

You are forwarding container 8090 to hostIP:3100 (localhost:3100)
At the same time you are creating a IP to access your container with 10.10.1.10 and you container has 8090 exposed, thats why you can acces it on 10.10.1.10:8090
# 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.
# NOTE: This will enable public access to the opened port
config.vm.network "forwarded_port", guest: 8090, host: 3100
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "10.10.1.10"

Related

Consul dev mode unable to see UI using Vagrant

I started a consul agent in dev mode by using the following inside a VM using Vagrant. The command I used is consul agent -dev
I confirmed that the UI is working fine by doing a curl on 8500. curl http://localhost:8500/ui
However when I am testing from the host machine using curl http://172.28.128.3:8500/ui I get the following error.
curl: (7) Failed to connect to 172.28.128.3 port 8500: Connection refused
What could be the error. Following is my VagrantFile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", type: "dhcp"
end
I can see the following when I do vagrant ssh.
System load: 0.07 Processes: 99
Usage of /: 13.5% of 9.63GB Users logged in: 0
Memory usage: 12% IP address for enp0s3: 10.0.2.15
Swap usage: 0% IP address for enp0s8: 172.28.128.3
When I do the following nc -vz 172.28.128.3 8500 on my host mac I get the error
nc: connectx to 172.28.128.3 port 8500 (tcp) failed: Connection refused
There are a number of ways you can expose the running Consul agent UI in the box to the host.
Configure running service port to be forwarded to the host.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.network "private_network", type: "dhcp"
config.vm.network "forwarded_port", guest: 8500, host: 8500
end
The service would be available at http://localhost:8500/ui.
Configure a public network.
Tread with care as this exposes the entire box to the host.
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.0.17"
end
Use vagrant connect command
vagrant connect
The resulting Static IP can be used together with the service port.

Vagrant Machines - Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001

I am currently setting up a development environment whereby there are two vagrant machines. The first hosts a website etc while the second hosts an API which the website calls. Currently my vagrantfiles are as follows:
API
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
end
Website
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
end
When i attempt to load the site which makes a call to the API I get:
Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001 (Connection refused - connect(2) for "localhost" port 3001)):
I understand there are multi-machine approaches but as I already have an seperate environment set up for each project I was wondering if there is any way for me to allow the connection between the two. I have looked into changing the hosts file on the vagrant machine hosting the site but not sure what I should do.
You need to put both VMs into the same private network and Website VM will be able to reach API VM via it:
config.vm.network "private_network", ip: "<ip_address>"
For example, for your case:
API VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3001, host: 3001
config.vm.network "private_network", ip: "192.168.200.3"
end
Website VM:
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/precise64"
config.vm.network "forwarded_port", guest: 3000, host: 3000
config.vm.network "private_network", ip: "192.168.200.2"
end
Now, Website VM can reach API VM on 192.168.200.3:3001. And of course you can reach Website VM on 127.0.0.1:3000 and API VM on 127.0.0.1:3001 from your host machine.

Files created in Vagrant centos/7 do not appear in Windows [duplicate]

This question already has answers here:
Vagrant Synced Folder without reload?
(2 answers)
Closed 5 years ago.
I have primarily used Ubuntu boxes in Vagrant and in those any files created within the box have been successfully synced to Windows also. Usually things like git pull.
However, I now have centos/7 and cannot get any files to sync to Windows. If you create a file in /vagrant it never appears anywhere in Windows - not under C:\cygwin64\ nor C:\vm\machine. When I vagrant reload the box, all the files created inside the box are gone, except configuration files eg. /etc/httpd/conf/httpd.conf. Any changes in them are still present upon vagrant reload and also vagrant halt; vagrant up.
Here's the Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8086, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3386, host_ip: "127.0.0.1"
config.vm.synced_folder "./", "/vagrant"
# I have also tried the following:
# config.vm.synced_folder "C:/vm/machine", "/vagrant"
# config.vm.synced_folder "./", "/vagrant", type: "rsync", disabled: false
end
When doing vagrant up, both methods for synced_folder display this:
==> default: Rsyncing folder: /cygdrive/c/vm/machine/ => /vagrant
I have tried doing vagrant up from both cmd.exe and also from the Cygwin64 Terminal (under /c/vm/machine). Neither of them create files to the Windows directory, which is in C:\vm\machine.
What do I need to do to make files created in the box also sync to Windows?
When I try the same in an Ubuntu box, the file appears in Windows and does not disappear:
This is the Vagrantfile of the Ubuntu box:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "v0rtex/xenial64"
config.vm.network "forwarded_port", guest: 80, host: 8083, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 443, host: 8443, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 3306, host: 3303, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 5000, host: 5003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 8000, host: 8003, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9090, host: 9093, host_ip: "127.0.0.1"
config.vm.network "forwarded_port", guest: 9091, host: 9193, host_ip: "127.0.0.1"
end
Upon vagrant up for the Ubuntu box, I see this:
==> default: Mounting shared folders...
default: /vagrant => C:/vm/ubu1604-64
The solution is as follows:
Change the Vagrantfile synced_folder part to this:
config.vm.synced_folder "./", "/vagrant", type: "virtualbox", disabled: false
And then run this command in the directory where the Vagrantfile is:
vagrant plugin install vagrant-vbguest
Then do either vagrant reload if it was already running, or vagrant up otherwise.
It will run for quite a while - for me it took about 7 minutes altogether. However, it is a one-time install, so all future vagrant up's will take a normal amount of time.
After that, the files created in the /vagrant directory will also appear in Windows C:\vm\machine (for example) and will persist between Vagrant sessions, ie. vagrant halt + vagrant up

How to keep vagrant configuration organized

I'm provisioning vagrant box with ansible. However, my vagrant host doesn't support ansible, so I must execute ansible against localhost from within the vagrant box. I'm using shell provisioner, executing a script which will install ansible and run the correct ansible command against a playbook in a synced folder.
This solution works, but feels messy and hard to maintain, as configuration data is cluttered between files. As you can see below, there is duplication of playbook path, hard coded both to Vagrantfile and provisioning script. And as we know, duplication is the source of evil. Rather, I would like to keep all configuration strings in Vagrantfile or external file. Is this possible in my case? It would be also nice to be able to override these values with command line parameters.
Here is the vagrant file
VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm.box = BOX_IMAGE
config.vm.network "private_network", ip: "192.168.22.22"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 8081, host: 8081
config.vm.network "forwarded_port", guest: 2222, host: 2223
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.network "forwarded_port", guest: 50000, host: 50000
config.vm.provision :shell, path: "provisioning.sh"
config.vm.synced_folder "./provisioning", "/etc/provisioning/vagrant"
end
Here is the provisioning script
apt-get install -y python-pip
apt-get install -y git-core
if [ -z $PLAYBOOK_PATH ]; then
echo "PLAYBOOK_PATH not set, using default"
PLAYBOOK_PATH=/etc/provisioning/vagrant/playbook.yml
fi
if [ -n $PLAYBOOK_PATH ]; then
echo "PLAYBOOK_PATH set at $PLAYBOOK_PATH"
if [ -f $PLAYBOOK_PATH ]; then
echo "PLAYBOOK FOUND, insalling ansible and starting provisioning"
pip install ansible
pip install markupsafe
if ( ansible-playbook $PLAYBOOK_PATH ); then
echo "provisioning OK"
else
echo "prvosioning failed"
fi
else
echo "PLAYBOOK not found"
fi
else
echo "PLAYBOOK_PATH not set"
fi
And finally here is the playbook inside provisioning folder
- name: Provision local docker host
hosts: localhost
become : yes
roles:
- docker.ubuntu
tasks:
- name: Add vagrant to docker group
become: yes
user: name=vagrant groups=docker append=yes
You dont necessarily need to do all this.
Vagrant supports running ansible on the guest VM aka ansible local
The Vagrant Ansible Local provisioner allows you to provision the guest using Ansible playbooks by executing ansible-playbook directly on the guest machine.
You can have a Vagrantfile like
VAGRANT_API_VERSION = 2
BOX_IMAGE = "ubuntu/trusty64"
PROVISIONING_SRC = "./provisioning"
PROVISIONING_DEST = "/etc/provisioning/vagrant"
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm.box = BOX_IMAGE
config.vm.network "private_network", ip: "192.168.22.22"
config.vm.network "forwarded_port", guest: 8080, host: 8080
config.vm.network "forwarded_port", guest: 8081, host: 8081
config.vm.network "forwarded_port", guest: 2222, host: 2223
config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.network "forwarded_port", guest: 50000, host: 50000
config.vm.provision "ansible_local" do |ansible|
ansible.playbook = "provisioning/playbook.yml"
end
end

Vagrant not listening to my port and using default port

I'm using Vagrant for multiple projects now with a per-project-installation, meaning I have multiple boxes. One for each project (or for production enviroment)
When I want to access my first Vagrantbox I access it via http://sitea.com:8080. My second Vagrantbox should listen to 1337 (I tried multiple ports here) but when I call http://siteb.com:1337 I'm getting an error in all browsers (ERR_CONNECTION_REFUSED in Chrome).
However, when I'm calling my second site like http://siteb.com it works without any problems. It seems that my second Vagrantbox just doesn't listen to the ports and uses the default port. But why is that?
First box (Which works with port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 8080
config.vm.network "private_network", ip: "192.168.50.4"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Second box (Which works without port only)
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/debian-7.8-64-puppet"
config.vm.provision :shell, path: "vagrant_ressources/bootstrap.sh"
config.vm.network :forwarded_port, guest: 80, host: 1337
config.vm.network "private_network", ip: "192.168.50.5"
config.vm.synced_folder ".", "/var/www", type: "nfs"
end
Both have the same vhost configuration, and my hosts file looks like this
# Vagrant
192.168.50.4 sitea.com
192.168.50.5 siteb.com
When using port forwarding with Vagrant you'll usually access localhost since the forwarded port is available at your host machine. Like this:
http://localhost:8080 -> should deliver the website siteA.com
http://localhost:1337 -> should deliver the website siteB.com
If you are able to access http://siteB[:80] it means that the DNS entry of siteB points to 192.168.50.5, which is the private network ip of that box.
Check:
getent hosts | grep siteB\.com
I bet it will give you 192.168.50.5.

Resources