Beginner guides and tips for learning Chef

Posted under Development 5 minute read

This article is a collection of high quality resources for learning chef and will also contain helpful information to clear up common pitfalls beginners may run into. As you progress through this article each section will assume you have been through the previous section.

What is Chef?

Chef is a systems integration framework, built to bring the benefits of configuration management to your entire infrastructure. Chef Wiki

The gist of it is Chef will help you provision and manage your servers. It's extremely useful no matter how big or small your infrastructure is. It's useful for handling 1 server and it's also scalable enough to work for Facebook, Riot Games and other highly successful services that require many servers.

Learn Chef webinar

You will want to start with the learnchef webinar. If nothing else you should watch the screencasts. It is a free resource provided by the company who created Chef.

It is ran by Nathen Harvey and he is great at explaining things. He will guide you step by step to go from no knowledge of Chef to writing your first cookbook and getting familiar with Chef's lingo and standard work flow.

Berkshelf

The standard work flow with Chef is considered to be non-optimal by certain people. Berkshelf is like Bundler for Ruby. Rather than have all of your cookbooks live in one repository the guys over at Riot Games thought it was necessary to have each cookbook as its own separate repository.

This makes a lot of sense because each cookbook is its own thing and they are re-usable. It makes sense to keep them isolated and then include them into what people have dubbed "the application cookbook".

The idea is you have a cookbook that is not re-usable and this would contain your application specific settings and it could also include a collection of re-usable cookbooks that you have developed in isolation or are using from the community site.

Berkshelf was created to handle packaging a bunch of separate cookbooks together and manage all of the dependencies among other things.

Jamie Winsor of Riot Games has given a presentation on Berkshelf and you should absolutely watch it as soon as possible if it sounds like Berkshelf interests you.

The Berkshelf way tutorial

In the presentation Jamie talks about a toy application called myface. He wrote a small tutorial to build it but he never got around to writing the full tutorial.

Luckily someone in the community wrote a very detailed guide going from nothing to building and testing myface. Mischa Taylor's 3 part series will go through everything from getting your development workstation setup for Chef development all the way through using Berkshelf, Knife, Vagrant and Test-kitchen.

You will be able to test your cookbooks and only have to wait seconds to see the results due to a combination of Vagrant and Berkshelf's vagrant plugin.

The great thing is Mischa seems to be dedicated to keeping the article series up to date. It's constantly being updated since the tooling around Chef is moving quite fast.

Part 1:
http://misheska.com/blog/2013/06/16/getting-started-writing-chef-cookbooks-the-berkshelf-way/

Part 2:
http://misheska.com/blog/2013/06/23/getting-started-writing-chef-cookbooks-the-berkshelf-way-part2/

Part 3:
http://misheska.com/blog/2013/08/06/getting-started-writing-chef-cookbooks-the-berkshelf-way-part3/

Using custom unpublished cookbooks

The Berkshelf tutorial is very comprehensive but it misses one topic that might cause you to get hung up as a beginner. What happens when you want to use a cookbook that you have created but have not yet put it on the official opscode community site?

Let's go through a situation where you want to use the opscode nginx cookbook but you want to introduce the idea of virtual hosts in such a way that you can just include a recipe into your application cookbook and create the template file that contains the virtual config file and nothing else.

You can do this very easily with Berkself and you do not have to publish your cookbook anywhere if you don't want to. You can even put it on Github if you want because Berkshself knows how to read in the location of a cookbook from a number of source locations.

Assuming you had a cookbook named nginx_virtualhosts then you would need to set this up in your application cookbook:


Berksfile:

site :opscode

metadata

cookbook 'nginx_virtualhosts', path: '/path/to/nginx_virtualhosts'

This informs Berkshelf where the cookbook is located. We are telling it to read from our file system. You could use Github instead, check the Berkshelf readme for the details.

metadata.rb:

...

depends 'nginx', '~> 2.2.0'
depends 'nginx_virtualhosts', '~> 0.1.0'

This lets Chef know that you are depending on both nginx and your personal nginx_virtualhosts cookbook. It is important to note that each depends call is not pulling in the dependency from any third party source or anything of that nature.

It expects the cookbooks to be available on your Chef server, it does not care if you uploaded them with Knife or Berkshelf.

recipes/default.rb:

include_recipe 'nginx'
include_recipe 'nginx_virtualhosts'

This is standard Chef code here. We are just telling it to include the recipe.


That's all there is to it to use non-official cookbooks in your application cookbook. Remember to keep your version numbers in the metadata.rb up to date.

Using RVM and Chef on your development machine

If you have RVM installed and you installed Chef with the standard installer then you're going to run into some annoying but easily fixable problems.

The knife utility will expect to use the system's version of Ruby but RVM will take over your session, so you have to tell RVM to use the system's version of Ruby every time you want to use Knife.

You can do this by typing rvm system into a terminal. My recommendation is to have 2 terminals open. One of them could have your standard/default RVM Ruby version which is likely where you will have Berkshelf installed to and then the other terminal can have rvm system ran on it so you are free to use Knife.

Now you can happily use Berkshelf and the standard Chef utilities at the same time without having to do anything too annoying.

Other useful resources

Nathen Harvey co-runs a podcast called food fight show. You can listen to each episode on their website or watch them on Youtube.

#learnchef, #chef, #berkshelf and #kitchenci are channels available on freenode (IRC).

Looking to start an on-line business?

Let neblit create your web site or help you setup your server infrastructure. Start now by scheduling a free phone consultation, it takes about 5 seconds.

comments powered by Disqus