Simulating dynamic multiple host networking
Vagrant is also very useful when used to simulate multiple hosts in a network. This way you can have full systems able to talk to each other in the same private network and easily test connectivity between systems.
Getting ready
To step through this recipe, you will need the following:
- A working Vagrant installation
- A working VirtualBox installation
- An Internet connection
How to do it…
Here's how we would create one CentOS 7.2 machine with 512 MB of RAM and one CPU, in a private network with a fixed IP 192.168.50.11, and a simple shell output:
vm_memory = 512 vm_cpus = 1 Vagrant.configure("2") do |config| config.vm.box = "bento/centos-7.2" config.vm.provider :virtualbox do |vb| vb.memory = vm_memory vb.cpus = vm_cpus end config.vm.define "srv-1" do |config| config.vm.provision :shell, :inline => "ip addr | grep \"inet\" | awk '{print $2}'" config.vm.network "private_network", ip: "192.168.50.11", virtualbox__intnet: "true" end end
To add a new machine to this network, we could simply duplicate the srv-1
machine definition, as in the following code:
config.vm.define "srv-2" do |config| config.vm.provision :shell, :inline => "ip addr | grep \"inet\" | awk '{print $2}'" config.vm.network "private_network", ip: "192.168.50.12", virtualbox__intnet: "true" end
That's not very DRY, so let's take advantage of the Ruby nature of the Vagrantfile to create a loop that will dynamically and simply create as many virtual machines as we want.
First, declare a variable with the amount of virtual machines we want (2
):
vm_num = 2
Then iterate through that value, so it can generate values for an IP and for a hostname:
(1..vm_num).each do |n| # a lan lab in the 192.168.50.0/24 range lan_ip = "192.168.50.#{n+10}" config.vm.define "srv-#{n}" do |config| config.vm.provision :shell, :inline => "ip addr | grep \"inet\" | awk '{print $2}'" config.vm.network "private_network", ip: lan_ip, virtualbox__intnet: "true" end end
This will create two virtual machines (srv-1
at 192.168.50.11
and srv-2
at 192.168.50.12
) on the same internal network, so they can talk to each other.
Now you can simply change the value of vm_num
and you'll easily spawn new virtual machines in seconds.
There's more…
We can optionally go even further, using the following cloning and networking features.
Speed up deployments with linked clones
Linked clones is a feature that enables new VMs to be created based on an initial existing disk image, without the need to duplicate everything. Each VM stores only its delta state, allowing very fast virtual machines boot times.
As we're launching many machines, you can optionally enable linked clones to speed things up:
config.vm.provider :virtualbox do |vb|
vb.memory = vm_memory
vb.cpus = vm_cpus
vb.linked_clone = true
end
Using named NAT networks
VirtualBox has the option to let you define your own networks for further reference or reuse. Configure them under Preferences | Network | NAT Networks. Luckily, Vagrant can work with those named NAT networks too. To test the feature, you can create in VirtualBox a network (like iac-lab) and assign it the network 192.168.50.0/24
.
Just change the network configuration from the preceding Vagrantfile to launch the VMs in this specific network:
config.vm.network "private_network", ip: lan_ip, virtualbox__intnet: "iac-lab"