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
*** 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
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
- Start up the single Vagrant box with a
vagrant up
from thevagrant
directory in the ATB Git repo. - Once the box is up log in with a
vagrant ssh
. - Then delete the entire
/etc
folder of the VM. exit
the VM and get back to your machine's command prompt.vagrant halt
the server and bring it backvagrant up
again. I'll wait.- Now bring up the server the way it was before you destroyed
/etc
. We do this with avagrant destroy
and anothervagrant up
- 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
vagrant up
our single Vagrant box. (Which might already be running from the final step above)- View the VMs default webpage http://localhost:8080.
- Connect via SSH and then stop the Apache server from running
sudo service apache2 stop
. - Reload the web page. Which should no longer load.
- Finish up with an
exit
and the VM andvagrant destroy
of the VM.
Three - run three boxes as a small cluster of machines
- From your machine
cd multi-box
so we can work with a differentVagrantfile
- Review this directory's Vagrantfile and note the differences (three machines with their own IP addresses).
- Start up the whole cluster of Vagrant VMs in the same manner as if it were just one machine by running
vagrant up
. - Login to the box web1 (
vagrant ssh web1
) and ping the box web3 on IP address192.168.50.30
- View the default Apache page on web1, web2 and web3.
- Finish up with an
exit
if necessary and then avagrant destroy
to bring down all three machines.