Vagrant – Part III – Networking and Multimachine
This is the third part of the Vagrant series where I will cover some of the networking and multimachine features options.
The first part and the second part provide some background information.
Networking options cover port forwarding, connection to public network and private network.
Private network allows you to access the VM using an IPv(4|6) address that is not accessible from Internet.
This is how you can define a private network:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "MY_LAMP_UBUNTU16.04" config.vm.box_url = "file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box" config.vm.network "private_network", ip: "192.168.10.10" end parau-mbp:aut parau$
And when the VM is brought up, in addition to the default network card(Adapter 1) which has to always be “nat” type, you can see the second adapter:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports...
VirtualBox created on my host machine a NIC from 192.168.10.0/24 range and configured the VM to use an IP address from this range. If for instance, I would have configured the VM to use an IP address from 192.168.20.0/24, then VirtualBox will create an interface(vboxnet1 in this case) from this range:
parau-mbp:aut parau$ ifconfig vboxnet0 vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500 ether 0a:00:27:00:00:00 inet 192.168.10.1 netmask 0xffffff00 broadcast 192.168.10.255 parau-mbp:aut parau$
I can also access the VM using the IP address from the private network from my host:
parau-mbp:aut parau$ ping 192.168.10.10 PING 192.168.10.10 (192.168.10.10): 56 data bytes 64 bytes from 192.168.10.10: icmp_seq=0 ttl=64 time=0.399 ms ^C --- 192.168.10.10 ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.399/0.399/0.399/0.000 ms parau-mbp:aut parau$
The VM has two interfaces:
vagrant@vagrant-ubuntu-trusty-64:~$ ifconfig | grep "Link encap\|inet addr" eth0 Link encap:Ethernet HWaddr 08:00:27:65:e1:a4 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 eth1 Link encap:Ethernet HWaddr 08:00:27:16:5b:5e inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 vagrant@vagrant-ubuntu-trusty-64:~$
You can add a public network through which the VM can be accessed like this:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "MY_LAMP_UBUNTU16.04" config.vm.box_url = "file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box" config.vm.network "private_network", ip: "192.168.10.10" config.vm.network "public_network" end parau-mbp:aut parau$
When you add a public network, you need specify to which host existing interface will the public network bridge to. If you do not specify in Vagrantfile, then during VM setup you will be asked like below:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Available bridged network interfaces: 1) en0: Wi-Fi (AirPort) 2) en1: Thunderbolt 1 3) en2: Thunderbolt 2 4) bridge0 5) en5: Thunderbolt Ethernet ==> default: When choosing an interface, it is usually the one that is ==> default: being used to connect to the internet. default: Which interface should the network bridge to? 5 ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly default: Adapter 3: bridged ==> default: Forwarding ports...
As you can see, now the third interface on VM, has a public IP address(aquired through DHCP):
vagrant@vagrant-ubuntu-trusty-64:~$ ifconfig | grep "Link encap\|inet addr" eth0 Link encap:Ethernet HWaddr 08:00:27:65:e1:a4 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 eth1 Link encap:Ethernet HWaddr 08:00:27:35:4a:3b inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0 eth2 Link encap:Ethernet HWaddr 08:00:27:07:34:d5 inet addr:195.XX.YYY.175 Bcast:195.XX.YYY.255 Mask:255.255.255.0 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 vagrant@vagrant-ubuntu-trusty-64:~$
Which is similar(from the same subnet) as my host interface to which I bridged the public network:
parau-mbp:aut parau$ ifconfig en5 | grep "flags\|inet " en5: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500 inet 195.XX.YYY.140 netmask 0xffffff00 broadcast 195.XX.YYY.255 parau-mbp:aut parau$
This is how is done through Vagrantfile configuration:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "MY_LAMP_UBUNTU16.04" config.vm.box_url = "file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box" config.vm.network "private_network", ip: "192.168.10.10" config.vm.network "public_network", bridge:"en5: Thunderbolt Ethernet" end parau-mbp:aut parau$
Another networking feature is port forwarding between host and guest and let’s say that you would like to forward TCP 8080 from host to TCP 80 on guest:
parau-mbp:aut parau$ cat Vagrantfile Vagrant.configure("2") do |config| config.vm.box = "MY_LAMP_UBUNTU16.04" config.vm.box_url = "file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box" config.vm.network "forwarded_port", guest: 80, host: 8080, protocol: "tcp", auto_correct: true config.vm.network "private_network", ip: "192.168.10.10" config.vm.network "public_network", bridge:"en5: Thunderbolt Ethernet" end parau-mbp:aut parau$
“auto-correct” solves the situation where there is already a port forwarding for the pair you configured and automatically choose another ports to solve the conflict.
You can see the port mapping during VM boot up:
parau-mbp:aut parau$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Forwarding ports... default: 80 (guest) => 8080 (host) (adapter 1) default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM...
Otherwise you will see something like when a collision happens:
==> default: Fixed port collision for 22 => 2222. Now on port 2202. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: intnet ==> default: Forwarding ports... default: 22 (guest) => 2202 (host) (adapter 1)
Now we can move to the multimachine section and this is nothing else than being able to run multiple VM in the same time.
This is how you can define two guests to use the same box with a private network. In addition during the boot, I used a provisioner to change the hostname of each guest:
Vagrant.configure("2") do |config| config.vm.box = "MY_LAMP_UBUNTU16.04" config.vm.box_url = "file:/Users/parau/aut/MY_LAMP_UBUNTU16.04.box" config.vm.define "vm-1" do |vm1| vm1.vm.network "private_network", ip: "192.168.10.10" vm1.vm.provision :shell, inline: "hostname vm1" end config.vm.define "vm-2" do |vm2| vm2.vm.network "private_network", ip: "192.168.10.11" vm2.vm.provision :shell, inline: "hostname vm2" end end
Both guests are up:
parau-mbp:aut parau$ vagrant status Current machine states: vm-1 running (virtualbox) vm-2 running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`. parau-mbp:aut parau$
And in addition to the usual “vagrant ssh”, you now need to specify to which guest you want to connect:
parau-mbp:aut parau$ vagrant ssh This command requires a specific VM name to target in a multi-VM environment. parau-mbp:aut parau$ vagrant ssh vm-1 Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-144-generic x86_64)
The guests have an interface from the private network defined in the Vagrantfile and you can jump from one guest to the other:
vagrant@vm1:~$ ifconfig | grep "Link encap\|inet addr" eth0 Link encap:Ethernet HWaddr 08:00:27:65:e1:a4 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 eth1 Link encap:Ethernet HWaddr 08:00:27:a8:54:fa inet addr:192.168.10.10 Bcast:192.168.10.255 Mask:255.255.255.0 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 vagrant@vm1:~$ ssh vagrant@192.168.10.11 vagrant@192.168.10.11's password: Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-144-generic x86_64) vagrant@vm2:~$
And that would be some of the things you can do in terms of networking and multimachine with Vagrant.
The public documentation can provide additional details.
In the next part, we will see how we can bring up a Juniper virtual QFX using Vagrant.
I hope you found this information useful.
Paris ARAU
Latest posts by Paris ARAU (see all)
- Junos Fusion – Part IV – Satellite policies and uplink failure detection - 30 July 2018
- Junos Fusion – Part III – Satellite commands and traffic forwarding - 16 July 2018
- Junos Fusion – Part II – Configuration, Administration and Operation - 16 July 2018
- Junos Fusion – Part I – Overview, Components, Ports and Software - 11 July 2018
- Vagrant – Part IV – Network topology using Juniper and Cumulus - 26 April 2018
Comments
So empty here ... leave a comment!