Creating a Multi-Machine Vagrant Environment

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

By using a “Vagrantfile within a Vagrantfile” structure, we can create code that allows Vagrant to provision an environment comprised of multiple machines. This lets us create Vagrant environments that closely mimic production, separating out databases, services, applications, and more as needed. In this hands-on lab, we use the LXC provider to create two different machines and establish that the machines can communicate.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create Two Machines
  1. Move into the website directory and initialize a Vagrantfile:

    cd website
    vagrant init
  2. Update the Vagrantfile so two containers are created:

    vim Vagrantfile
    
    Vagrant.configure("2") do |config|
      config.vm.box = "fgrehm/trusty64-lxc"
      config.vm.provider "lxc"
      config.vm.define "web" do |web|
      end
      config.vm.define "db" do |db|
      end
    end
Configure the Private Network

Still editing your Vagrantfile, add a private network to each guest:

Vagrant.configure("2") do |config|
  config.vm.box = "fgrehm/trusty64-lxc"
  config.vm.provider "lxc"
  config.vm.define "web" do |web|
    web.vm.network "private_network", ip: "192.168.2.110", lxc__bridge_name: "brdg"
  end
  config.vm.define "db" do |db|
    db.vm.network "private_network", ip: "192.168.2.111", lxc__bridge_name: "brdg"
  end
end

Save and exit the file.

Test the Environment
  1. Deploy the environment:

    vagrant up
  2. Log in to the web guest:

    vagrant ssh web
  3. View the IP of the guest:

    ifconfig
  4. ping the db from the web guest:

    ping 192.168.2.111

Additional Resources

You have been tasked with creating a Vagrant environment that the front-end designers can use. This environment needs to include both a web and db host, and a private network needs to be established between them.

Write a Vagrantfile that provisions two LXC containers, and ensure both guests use the fgrehm/trusty64-lxc box. When configuring the network, set the IP for web to 192.168.2.110 and the IP for db to 192.168.2.111. Since you are using the LXC provider, you also need to provide the name of the bridge via the lxc__bridge_name option; set the bridge name to brdg.

Once finished, deploy the environment. Ensure the two guests can communicate.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!