Developer Automation Tools Bootcamp

Learning about development tools and automation

View project onGitHub

Introduction to Vagrant


"Develop Environments Made Easy" Vagrant by Hashicorop

Getting Started

A vagrant stereotypically lives in a box. The vagrant tool is built to manage all your "boxes" (VMs/servers). It is a brilliant project name.

Mitchell Hashimoto created Vagrant and can explain it best. Here is a great high level description.

Benefits of Vagrant

  • Your local testing environment will more closely match your shared environments
  • No more "works for me" (because I'm running a different version of Java, PHP, etc)
  • Quick tear down and setup (aka - provisioning). You can break it, destroy it and start over until you tweak it just right.
  • With enough system resources you can replicate an entire distributed app (DB + app server + web server + load balancer)
  • Tip: What's going on with my Vagrant box right now?
    vagrant status

Setting up Vagrant

  1. Install Virtualbox***
  2. Install Vagrant
  3. Create your Vagrantfile
*** VMWare is an option with a Vagrant plugin that costs $79. It is highly recommended by Mitchell Hashimoto to use the VMWare option. But the basic steps above are free. Having a VMWare license is not the same as having the Vagrant VMWare plugin license; these are two separate purchases.

The Vagrantfile

Everything to define your Vagrant configuration is done in a Vagrantfile. This includes

  • Which box to use? Ubuntu? CentOS? Windows? Which version?
  • Networking configuration
  • Syncing local folders to vm
  • Basic provisioning: Inline Shell scripts
  • Advanced provisioning: Chef || Puppet || Ansible || Salt
Adding a Vagrantfile to your project, any project, can be a great gift to the engineers you are supporting.

Vagrant Basics

vagrant up        # boots the VM(s) defined in the Vagrantfile
vagrant ssh       # connects to the VM via ssh.  That's it.  No IP needed.
vagrant suspend   # sleeps the server
vagrant halt      # shuts down the server
vagrant destroy   # wipes the server away to nothing

Workout

Review the example Vagrantfile which you checked out during the Git session.
We are going to complete the following three actions:

One - wreck the test box and recover

  1. Start up the single Vagrant box with a vagrant up from the vagrant directory in the ATB Git repo.
  2. Once the box is up log in with a vagrant ssh.
  3. Then delete the entire /etc folder of the VM.
  4. exit the VM and get back to your machine's command prompt.
  5. vagrant halt the server and bring it back vagrant up again. I'll wait.
  6. Now bring up the server the way it was before you destroyed /etc. We do this with a vagrant destroy and another vagrant up
  7. We can again vagrant ssh in to the VM and see that /etc is back and we did not really screw anything up.

Two - run one box as a webserver

  1. vagrant up our single Vagrant box. (Which might already be running from the final step above)
  2. View the VMs default webpage http://localhost:8080.
  3. Connect via SSH and then stop the Apache server from running sudo service apache2 stop.
  4. Reload the web page. Which should no longer load.
  5. Finish up with an exit and the VM and vagrant destroy of the VM.

Three - run three boxes as a small cluster of machines

  1. From your machine cd multi-box so we can work with a different Vagrantfile
  2. Review this directory's Vagrantfile and note the differences (three machines with their own IP addresses).
  3. Start up the whole cluster of Vagrant VMs in the same manner as if it were just one machine by running vagrant up.
  4. Login to the box web1 (vagrant ssh web1) and ping the box web3 on IP address 192.168.50.30
  5. View the default Apache page on web1, web2 and web3.
  6. Finish up with an exit if necessary and then a vagrant destroy to bring down all three machines.