Using Vagrant to create a reproducible, multi-machine, virtualized dev environment (IaC)

This is another piece of very cool technology that I ran across recently. Imagine that for your software development project, you want to bring up virtual machines on your workstation. For example, image you might use:

  • Name=webserver 2GB of RAM, install Ubuntu Server, install Apache, and PHP
  • Name=databaseserver 4GB of RAM, install Ubuntu Server, install Apache and PHP (for PHPMySQL), and MySQL
  • Name=batchserver 2GB of RAM, install Ubuntu Server

Don’t get bogged down in the operating system – these could be Windows machines, and you can be using any kind of hypervisor, locally.

Now, if you have a few people on your team, this can be time-consuming and tedious. Worse, chances are each of your installs will be slightly different. Even worse, what if some people are using Macs or Linux machines with VirtualBox, and you have a developer using Windows 10 and Hyper-V? Because nowadays, since pretty much every technology runs on every OS, the operating system is starting to simply become a personal preference, right?

Each workstation, with those virtual machines, are going to be very different. What if there was a better way?

Enter Vagrant (https://www.vagrantup.com/):
This is a product that integrates with many virtualization and automation technologies, including VirtualBox, VMWare, Hyper-V, puppet/chef/Ansible, AWS, etc. You basically define a configuration file of what you want for virtual machines, and Vagrant goes and builds them – consistently, every time.

image

To me, the really compelling part is that you literally have one configuration file which describes everything about your machines, including scripts you want to run after they are provisioned (e.g. patching the system, installing software, configuring software, etc). This includes CPU, RAM, Disk, and Network settings – everything. Then, the fact that this equally supports VIrtualBox, Hyper-V, VMWare, etc – is quite amazing.

This means that any developer running on MacOS X, Linux, or Windows, can bring down the file, and build out the virtual machines, using whichever hypervisor they want, and the machines will be identical for each developer!

Certainly, this is somewhat of a niche solution – it is for development areas where the developers bring up servers as VM’s on their workstation. However, Vagrant could be used in all sorts of virtualization automation scenarios – and it does work with popular technologies like puppet, Ancible, and chef too.

The Source:
This concept is called Infrastructure as Code (IaC), where you store what infrastructure you need, in with your source code. You literally check it into source control; it’s a tiny file. So, I put my example up on github, here:

https://github.com/RobSeder/vagrant-demo

In this scenario, you define the definitions of the servers like this:

    {
        :name => "webserver",
        :eth1 => "192.168.100.101",
        :mem => "2048",
        :cpu => "2",
        :postinstallscript => "webserver_post.sh"
    }

In fact, the whole file is short-enough, so here is the entire configuration:

image

This is all using Ruby syntax. The “boxes” value is an array that stores the details of each server. In this case, I’m defining 3 servers I want to set up. On line 31, that is the operating system I want to install. I just chose this for all, for simplicity-sake. This is basically Ubuntu Server, and there is an image available for it for VirtualBox and Hyper-V.

Then, on line 36, we loop through each box in the boxes array and process each one. On line 43 for example, is the VirtualBox specific settings. On line 48 are the Hyper-V specific settings. So, if you download Vagrant (from www.vagrantup.com), on MacOS X, Linux, Windows, or whatever… and if you have either VirtualBox or Hyper-V installed – do the following on your workstation:

  1. Create a directory somewhere called vagrant-demo
  2. Execute: git clone https://github.com/RobSeder/vagrant-demo
  3. Navigate into this inner vagrant-demo folder (still from the command-line)
  4. Type, depending on what you have installed on the local machine:
      vagrant up –provider=virtualbox
    or
      vagrant up –provider=hyperv

And you should see it build out these boxes, exactly the same as I have on my machines here!

On MacOS X:
To test this, I did this on MacOS X and sure-enough, I watch the command-line scroll by and see the 3 machines. Not only did it stand up the servers, it then installed the correct software on each, and fully-patched each server (because of the .sh files which are also part of that github project):

image

On Kubuntu:
O
n Kubuntu (which is Ubuntu with the KDE window manager), same thing – I did a git clone, vagrant up and voila, the same machines got built:

image

On Windows:
And finally on windows, same thing here, except I did a “vagrant up –provider=hyperv” – but aside from that, the new servers were created, and then fleshed-out the same way:

image

Bottom line:
I think it’s important to note that Vagrant didn’t just create the VM’s – but it created them based off the specifications in a small config file that you check in with your development source code. And maybe even bigger than that, after it provisioned it correctly, it automatically updated/upgraded the system and installed the specific server software for each machine (Apache, MySQL, etc) – all automatically! This is defined in the *.sh files which are also checked-in to that github project.

Maybe the reason I find this so compelling is I do prefer this style of development infrastructure. I like for a developer to have a capable laptop where they can bring their “servers” within them. This means the developer can be on a plane, on a park bench, or at their desk – and they don’t need network connectivity to be able to work. Or even better, they don’t have to worry about stepping on the other developers as he/she does work. Instead, they have a complete, and in this case consistent, data center right on their laptop.

Bottom line for me, this is a really cool technology – and a great, lightweight way to manage your per-developer VM’s, by storing the configuration in code. This concept of Infrastructure as Code can only save time because every developer will get exactly the same configuration. Not all shops work this way – many use AWS, Azure, or on-prem servers – but for ones who do per-developer “laptop data centers”, this is a very cool tool!

Posted in Apple Stuff, Cloud Computing, Computers and Internet, Development Tools, General, Infrastructure, Linux, Open Source, Organization will set you free, Professional Development, Security, Uncategorized, Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 5 other followers

%d bloggers like this: