This style basically allows describing multiple virtual machines within a single Vagrantfile. Isn’t that convenient??
If you would like to follow along, these are the system requirements:
If you are working offline or with a limited internet connection (like tethering through a phone), preload a Linux image, such as Ubuntu Xenial. The easiest way to do this after installing Vagrant and Virtualbox is:
- Create a Vagrantfile
The contents of the Vagrantfile to preload Ubuntu Xenial is:
vagrant up, vagrant will download and save the box
image locally, Ubuntu Xenial 64bit in this case.
Benefits of Multimode
The way I setup multiple virtual machines in my previous article was to have a separate Vagrantfile in separate folders.
While this is easy to understand, there’s a lot of duplication between each of the file.
If I want to update all the virtual machine setup, I would have to update each of the files.
For every machine I want to create, I would create another folder with the same Vagrantfile.
In general, these would be managable for a handful of machines, but two handfuls become tedious… and more than that becomes a real pain. When I was working with just four for my article, it was getting pretty painful, I couldn’t imagine ten!
With multimode vagrant, it basically sets up each virtual machine programmatically. I only have one Vagrantfile and I can specify as multipley virtual machines as I want without additional folders or files. Vagrant manages this under the hood, whether I setup two, ten, or even 100 virtual machines.
This is an example of specifying ten virtual machines:
This method is scalable and easier to manage.
Listing and Accessing
To see all the machines, type:
$ vagrant status to list machine
names and states.
Isn’t that beautiful??
To access any worker machine, specify its name after the command. For example, to ssh into the third worker, this would be the command:
Different Machine Types
With multimode, we can bring up multiple machines easily, but configuring specific machines for different roles, like a balance loader, database server, database fail over, etc. can become tedious if we have to manually connect to each one.
Vagrant provides the define method which basically lets you define the configuration for different machine roles.
Here is an example of specifying the IP address and hostname of a “manager” machine with ten additional machines:
Now when we list the machines, this is the output:
and the manager machine can be accessed by its label:
Useful tips I have found when working with mutlimode Vagrantfile:
using global variables and the shared
When configuring multiple different virtual machines, using global variables to keep values consistent across machines. Storing values in a single place keep Vagrantfile clean.
Below is an example of using a common
BOX_IMAGE variable to store
the box image used across different virtual machines and the number of
workers to create.
All vagrant virtual machines have a folder shared with the host that
is accessible by the
Multimode virtual machines all share the same directory, so the
manager virtual machine’s
/vagrant folder will have the same
contents as worker5’s
This can be useful to share data between virtual machines, especially in provisioning.
With Vagrant mutlimode configuration, it is even easier to setup multiple virtual machines in a single file. Using a single command, one can make multiple virtual machines with differing configurations.
I see this tool to be the perfect way to experiment with site-level architectures: load balancers, databases with followers, etc.