Getting started with Chef
Table of Contents
Getting Started #
To begin, install Chef’s omnibus installer: Chef DK
Chef uses the chef
tool for creating cookbooks, downloading cookbooks, and many other things.
chef generate repo chef-repo
This creates a chef-repo for connecting to Chef Server.
While this is great for getting started with Chef, creating cookbooks and keeping them in version control gets weird, since this is a single repo.
Cookbooks #
Chef Cookbooks are composed of recipes written in Ruby plus a Chef Ruby sugar for automating installing an application like Redis, Postgres, Solr, etc. Take a look at some of the community cookbooks:
Lets say you want to modify a cookbook (these are called wrapper cookbooks)
Where do we keep track of our changes? Right in the chef repo?
There must be a better way for managing cookbooks, Berkshelf to the rescue.
Berkshelf? #
Included in the Chef DK, Berkshelf solves the problem of creating Chef cookbooks. This lets us version individual cooksbooks and their dependencies.
Jamie Winsor gives a great overview of what Berkshelf brings to the table: The Berkshelf Way (you’ll also find out why that meme is important…)
berks init .
for an existing cookbook.
or
berks cookbook test_cookbook
to create a new cookbook.
Write a Cookbook #
Here’s the full cookbook for a Solr instance: Apache Solr Cookbook
The salient parts are these three files:
- metadata.rb : tells Chef about the cookbooks we depend on.
- attributes/default.rb : configurable attributes for others to override.
- recipes/default.rb : default recipe for running this cookbook.
The default.rb
can be broken down into three easy steps:
1. Install Java (if configured, defaults to yes)
2. Download Solr and run install_solr_service.sh
3. Setup logs and start
Deploying with Vagrant #
vagrant plugin install vagrant-berkshelf
Vagrantfile #
# Solr uses port 8983 by default
config.vm.network "forwarded_port", guest: 8983, host: 8983
config.vm.provision :chef_solo do |chef|
chef.run_list = [
'recipe[apache_solr::default]'
]
end
vagrant up; vagrant provision
If all went according to plan you’ll have a Solr instance at localhost:8983