Vagrant is a virtualization tool, created by HashiCorp, to initialize and manage virtual machines (VMs). It is a popular tool and I have been using it for many years. During that time, I have learned a few tips and tricks for managing my VMs. In this post, I would like to share a few of my favorite tips for setting up and maintaining Vagrant boxes.
My Vagrant Background
I have been using Vagrant on the job for almost four years, it is one of the first tools I install on a new work laptop. I have mostly used Vagrant locally to spin up Linux servers for testing, but have also used it in to manage VMs on an OpenStack cloud. My most common use cases have been; creating replica environments to test customer issues and setting up environments with shell scripts to test training content.
Here are roughly a dozen tips!
Installation and Setup Quick Tips
- Vagrantfile: Unless you need a more complicated test environment, the basic Vagrantfile generated on the CLI is fine. The file can be generated with, vagrant init <source/image-name>.
- Images: The bento base images that are maintained by Chef are typically good. They are solid images and they have a large variety of OS options. However, there are also thousands of available images on Vagrant Cloud and it is likely you’ll find a image pre-configured with whatever software or specs you need.
- Logging in: I always change into the root user/root directory with sudo su –. I have no concern about permissions on a test box. This saves a little time when you begin to install software.
- Repeatable, robust test environments:
- Bash Scripts: If you always need x software installed on your VM then a simple bash script might be helpful. For example, you may always want VIM and Go installed.
- YAML definitions: This is great when you are testing different versions of the same software.
- Packer: Is great when you need some additional specs or software that does not change frequently on your VM.
- Combo: Use a combination of some or all of the methods to meet your needs!
VM Maintenance Quick Tips
- Snapshots and Rolling back: Take snapshots often! Typically I will take a snapshot as soon as a VM is running, vagrant snapshot save <vm-name>. Rolling back an image takes less than 30 seconds. Whereas destroying and rebuilding takes 10 – 20 minutes!
- Pausing VMs: Use vagrant halt <vm-name> often too! If you are testing large clusters or have many test cases you can slow down your machine. Halting will save the state, but will free up the resources.
- Keeping tabs on boxes: If you have ever wondered, “How many boxes do I have?”, using `vagrant box list` will provide the answer. This is great while trying to clean up old boxes that are no longer in use. Note, Vagrant will cache copies of boxes locally on your machine.
- Resources: How much RAM, CPU, and Disk Space do you really need? Remember this isn’t production, my rule of thumb is to size down as much as possible. Additional resources can be added later.
Final Thoughts
If you are interested in a couple of examples, check out my Github repo or Vagrant Cloud boxes. Feel free to share additional tips for installing and maintaining a Vagrant environment!