At Logentries we use an array of tools for managing, maintaining and monitoring our servers. Using current popular parlance, these could often be referred to as DevOps tools, or tools with DevOps use-cases. We’re going to have a quick look at the automation side of things, more specifically, how we spin up VMs in both development and production using Chef allowing us to have consistent software version, configurations and optimizations.
We are avid users of Vagrant with Chef, an awesome combination for configuration automation & provisioning servers for both production and development. The reason for using Vagrant with Chef is that, regardless of the host machine, you can run a near-production system locally in a matter of minutes.
Vagrant is a lightweight command-line wrapper around VirtualBox which can spin up test or development environments in a single command. This environment is always consistent, can be destroyed and rebuilt at any time, and can be used by any member of your team, on any of the major platforms.
Chef & Librarian-Chef
According to the official GetChef.com, “Chef is an automation platform that transforms infrastructure into code.” What this actually means is that we only need to define the configuration and optimizations for software packages we need in one location and Chef will handle pushing these for both development and production servers.
Finally, we use Librarian-Chef as a management tool for all our open source cookbooks. This helps in versioning but also allows us to write what we call wrapper cookbooks. These cookbooks are based on the open source cookbooks but allow us to implement our own modifications without having to fork it.
How Vagrant, Chef & Librarian-Chef work together
Let’s take Java as an example to piece all these together.
With Librarian-Chef you create a file called Cheffile which looks like this:
#!/usr/bin/env ruby #^syntax detection site 'http://community.opscode.com/api/v1' cookbook ‘java’
Running a Librarian-Chef update will download the Opscode Java cookbook into a folder called cookbooks.
We can then create a cookbook called java_wrapper which includes the original Java cookbook. I like to put these in a separate folder called site-cookbooks to distinguish between public and private cookbooks. A very basic chef cookbook would have the following structure:
- java_wrapper - attributes - default.rb
- recipes - default.rb
In the default recipe we only include one line which is:
Say we need to use the Oracle version of Java in our stack, we would override the default attributes set in cookbooks/java/attributes/default.rb to something like this:
override['java']['oracle']['accept_oracle_download_terms'] = true override['java']['accept_license_agreement'] = true default['java']['install_flavor'] = 'oracle' default['java']['jdk_version'] = '6'
And finally in your metadata don’t forget to add the following line:
Now, when you want to install Java you include java_wrapper instead of the Opscode Java cookbook. This means by default it will install Oracle Java jdk 1.6.
And to place the final piece of the puzzle, we use Vagrant with Chef to create an Ubuntu server with Oracle Java jdk 1.6. Create a file called Vagrantfile at the root of your cookbook directories with the following data:
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure('2') do |config| config.vm.box = 'precise' config.vm.box_url = 'http://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-amd64-vagrant-disk1.box' config.vm.network :private_network, ip: '192.168.33.10' config.vm.provider :virtualbox do |vb| vb.customize ['modifyvm', :id, '--memory', '2048', '--cpus', '2'] end config.vm.provision :chef_solo do |chef| chef.cookbooks_path = ['cookbooks', 'site-cookbooks'] chef.run_list = [ "recipe[java_wrapper::default]" ] end end
Running Vagrant Up will create a new VM running Ubuntu 12.04 and install Java JDK 1.6. Using these tools saves us a lot of time and effort in menial tasks like creating a default user, and installing your default software. Once the vagrant command has complete running java -version will show you that you now have a your specified version of Java.
Using automation tools like Vagrant and Chef not only let developers get a near production system running locally, they also save time and money in getting machines setup, allow easier testing of software upgrades but more importantly allow developers get creative with new software in a local environment without affecting their own machine.