Skip to main content

Creating a Multi-Machine Vagrant Environment

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:30:00

Difficulty

Beginner

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Multi-Machine Vagrant Environment

Introduction

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.

Solution

Open a terminal session, and log in via SSH using the credentials provided on the lab page.

Create Two Machines

  1. Move into the website directory:

    cd website
  2. Initialize a Vagrantfile: vagrant init

  3. Update the Vagrantfile so two containers are created (feel free to remove the commentary included in the file as well):

    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

  1. 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

Conclusion

Congratulations on successfully completing this hands-on lab!