Chef Solo, Knife, Vagrant and other tools to simulate a production environment with system monitoring

System configuration can be very boring and subjective, a sysadmin cooks a machine with same "secret sauces" and "magic HOWTOsor a cloud PaaS Service can do it for you (but limiting the platform tools you can use) like Heroku or OpenShift. The third solution is to move towards a DevOps technique and automatize as much as possible the system configuration. A little effort is required to learn a knew tool but the advantages over the manual system managing are huge!

  • Easy production server replication and synchronization
  • Easy system-level feature sharing and reuse
  • Separation between boilerplate configurations, default settings an YOUR settings (tm)
  • Easy bootstrap for new developers (vm config sharing) or testing new features without pollute a real production environment

Getting Started

The Ruby on Rails team released recently the "automated instructions" to build a their production system, in this post we'll use a similar tool to do the configuration managing, the aim is to do an overview of several tools to create a virtual production environment for testing.

Here are the tools:

Chef Solo: It's the standalone version of Chef (which is a configuration management tool). If You never read about Cookbooks, Recipes and Nodes applied to Computers start from here.
Knife Solo: It's a plugin for Knife which allow to manage Chef via CLI
Vagrant: It's command line tool to control the Virtual Machines, many . It depends on VirtualBox and some minimal boxes are already available via Vagrantbox.es
Librarian: It's Cookbook manager, useful to import and keep up-to-date external recipes.

Let's install the tools on the development computer

gem install knife-solo chef vagrant librarian

Building the project folder

(the result of this example structure is available on github)
Prepare the project folder "mykitchen" to manage your production server/s

knife kitchen mykitchen
cd mykitchen

Initialize a VM with vagrant

vagrant box add base http://files.vagrantup.com/precise64.box #will fill the vm template in $HOME/.vagrant.d/boxes/base/
vagrant init

Check your Vagrantfile and specify an ip, in my case: "10.0.0.10" and I'll refer to this vm as "alpha.virt"

vagrant up
# Add "10.0.0.10 alpha.virt" in /etc/hosts

Now with you should be able to log in

ssh vagrant@alpha.virt
# password vagrant

We don't want to type the password every time so exit and add the demo private key, to your keychain:

wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant && chmod 600 vagrant && ssh-add vagrant

Install Chef in the guest vm, alpha.virt

knife prepare  vagrant@alpha.virt

Managing external recipes via Cheffile

librarian-chef init # it creates the Cheffile
librarian-chef install # sync recipes in the Cookbooks folder

Our project specific cookbooks are kept separate in the site-cookbook folder

knife cookbook create alpha-tweaks -o site-cookbooks

The different servers init files can be collected in nodes/

# alpha.virt.json
{
  "nginx": {
     "install_method": "ppa"
    ,"init_style"    : "bluepill"
  }
  ,"postgresql": {
    "version": "9.1"
    ,"ssl"   : "false"
  }
  ,"run_list": [
    "recipe[users::sysadmins]" //will use the databag to set up the user deployer
    ,"recipe[nginx]"
    ,"recipe[postgresql]"
    ,"recipe[postgresql::server]"
    ,"recipe[postgresql::postgis]"
    ,"recipe[alpha-tweaks]"
     
  ]
}

Finally

knife cook vagrant@alpha.virt

The cookbook will be copied in the guest vm and nodes/alpha.virt.json will be executed, as you can see most of the values are kept default and I override only the values I wanted. When You need particular configurations is possible to create a custom recipe in the Chef DSL (see alpha-tweaks).
The best way to understand what is going on and how can you configure data bag or attributes is to read the recipes by yourself. It is also a good source of inspiration for your configs even if you decide to not use Chef because many big companies and startups already use Chef.

Don't consider it a silver bullet, because some recipes don't work out of the box with chef solo and sometimes it could be overkill. The original OpsCode recipes are supported on different platforms like Centos, Freebsd, Debian, Fedora,.. but many bleeding-edge software is supported only via unofficial recipes for Ubuntu 12.04 LTS and PPA (which will save you a lot of compiling time).

Conclusion

System Configuration Management is cool, sometimes it overlaps to Application Management (Capistrano) but most recipes could be very different, specially if you have a server with multiple webapps or a webapp across multiple servers.
However with a server configured in minutes and not days You can dedicate more time to set up your web servers or System Monitoring tools like Munin, Amon, Sentry or Ajenti, sometimes there's already a recipe for that.

Enjoy experimenting :P