Multi-Machine VMs With Vagrant
In this lesson, we’ll be looking at how you can build and run multi-machine Vagrant VMs.
We'll cover the following
What are multi-machine VMs and why do we need them?
Vagrant is able to define and control multiple guest machines per Vagrantfile. This is known as a "multi-machine"
environment.
These machines are generally able to work together or are somehow associated with each other. Here are some use cases describing what people are using multi-machine environments for today:
- Accurately modeling a multi-server production topology, such as separating a web and database server.
- Modeling a distributed system and how they interact with each other.
- Testing an interface, such as an API, to a service component.
- Disaster-case testing: machines dying, network partitions, slow networks, inconsistent world views, etc.
We can also use multi-machine VMs to play around with Ansible.
In this chapter, we’ll be looking at how you can build and run multi-machine Vagrant VMs.
Build a multi-machine Vagrant VM.
Let’s start off with just one machine Vagrantfile.
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
end
We can use the config.vm.define
method in the Vagrantfile
to create multi-machine VMs.
config.vm.define "node1" do |node1|
Here, node1
is the name of the VM, however, the name can be anything.
Let’s provide a box, or an image, to our VM node1
using the vm.box
file. Moreover, let’s assign it a hostname using the node1.vm.hostname
file.
config.vm.define "node1" do |node1|
node1.vm.box = "ubuntu/trusty64"
node1.vm.hostname = "webserver"
end
Similarly, we can add a second VM, node2
, in our Vagrantfile
as shown below.
config.vm.define "node2" do |node2|
node2.vm.box = "centos/7"
node2.vm.hostname = "database"
end
Then, put it all together.
Vagrant.configure("2") do |config|
config.vm.define "node1" do |node1|
node1.vm.box = "ubuntu/trusty64"
node1.vm.hostname = "webserver"
end
config.vm.define "node2" do |node2|
node2.vm.box = "centos/7"
node2.vm.hostname = "database"
end
end
Now, when starting up with vagrant up
, both 2 machines will get started. Now, do the vagrant status
to check the status of your machines.
You can ssh
into an individual machine by specifying its hostname
.
vagrant ssh webserver
You can also specify your primary machine, in case you haven’t already, by adding primary: true
in the Vagrantfile as shown below.
Vagrant.configure("2") do |config|
config.vm.define "node1", primary: true do |node1|
node1.vm.box = "ubuntu/trusty64"
node1.vm.hostname = "webserver"
end
config.vm.define "node2" do |node2|
node2.vm.box = "centos/7"
node2.vm.hostname = "database"
end
end
We’ve added primary: true
against the node1 VM. Now, if the VM hostname is not specified during vagrant ssh <vm-name>
, you’ll get logged into node1 by default.
Conclusion
Congratulations on completing this course. The next step would be to learn some automation tools, like Ansible or chef, and use that technology with Vagrant. All of this will prepare you for your journey towards DevOps.
The last section in this course provides an assessment and a lab session.
Get hands-on with 1400+ tech skills courses.