Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499

Follow up: Tomcat HA Cluster Automation using Ansible and Vagrant

In one of my previous posts I presented a solution to automate a Tomcat cluster installation using Ansible. One of the assumptions I made at the time was that the actual machines (physical or virtual) were already running and that Ansible could access them via SSH. Well, that might be true for a real production-like environment but what if you wanted to test this out on your local machine? Wouldn’t it be cool to spin up some virtual machines first and then use Ansible to provision them with the click of a button? Vagrant to the rescue!

Vagrant’s motto is “Development environments made easy” and in this post I plan to prove that and show how we can improve our existing  sample project. I am not going to spend time going through the installation steps since I believe the official Vagrant installation guide is pretty clear already.

The code for the sample project can be downloaded from here.

Prerequisites

To be able to run the project you’ll need to install the following software:

*This is needed since Vagrant doesn’t provide the virtualization environment. Vagrant also supports other virtualization providers such as VMWare or AWS but VirtualBox is the default free option…and we like free. You can read more about virtualization providers here.

Project Goal

Before we dive into the sample project let me give you a high level overview of what our goal is. In a nutshell, we want to use Vagrant to spin up a bunch of virtual machines and then run an Ansible playbook to provision the VMs with the software we want.

We only need three VMs in our case: two for the Tomcat containers and another one for the HAProxy.

The sequence of events will look like this:

vagrant_tomcat_ha

Sample Project

If you download the project you will see that the only major addition is a new folder called “vagrant” that contains two files: Vagrantfile and nodes.json.

If you are already familiar with the standard Vagrant configuration you will know that all the meta-data is kept in a file called Vagrantfile. Our Vagrantfile looks like this:

The first thing you’ll notice is that it creates multiple virtual machines based on the contents of another file called nodes.json. Why bother creating another file? The idea is that you can keep all the VM specific settings such as the VM name, IP Address, hostname, and memory capacity into the json file whereas the Vagrantfile remains pretty much static, even if you decide to add more VMs later on. It also provides and easier way of reading and managing your VM settings. This is what my nodes.json file looks like:

We also prevent Vagrant from automatically generating a new random SSH key pair for each VM. Instead we make sure that Vagrant provisions the VMs with the same default SSH public key. This would make things a little easier for us since we can use the same key to connect to all the VMs. The default private key is located inside you vagrant.d directory and it’s called “insecure_private_key”. In a Unix-like system the default location should be:

As you can probably imagine this key should never be used in a real environment (in which case you should use you own key pairs) but it will do the trick for a development environment like the one we are trying to build.

You might also notice that Vagrant will assign a set of private static IPs to our VMs so that we can easily access them just like if they were running on a remote location. Now it’s a good time to edit your hosts file and map those IP addresses to the corresponding FQDNs as defined in our Ansible’s host file:

Vagrant will then run the Ansible playbook after all the VMs are functioning. Sometimes you can chose to provision the VMs one at a time but our current playbook requires us to run it only after all the VMs are up.

And that’s it! Just run the following command from inside the vagrant folder to create and provision your VMs:

The process might take some time to finish but at the end you should be able to access you application by going to this url:

http://ansible.groman.com/snoop

Feel free to ask questions and leave some feedback.
Happy automation!


Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499