19 May 2014

chef quick start

by mo

Create a free account on get chef. This will create a new organization and an account for a hosted chef server. This will be the server that your nodes will connect to for instructions. This is also where you will upload your cookbooks, specify environments and roles.

After you login you will be given a starter kit with configuration for your organization. You will want to install the chef command line tools as well. You will be given a tool called knife which is used for connfiguring your chef server and new nodes.

Next you need to create a new node. For this you can stand up a new instance using vagrant, Joyent SDC command line tools AWS EC2 or whatever you want. A node is a server or machine that you want to manage using chef.

Then you need to bootstrap your new node.

  $ knife bootstrap 192.168.xxx.xxx --x vagrant -P vagrant --sudo

Then you can ssh on to the new node and tell it to run the chef-client. You can specify a default role and environment as part of the bootstrap step but I am going to skip that for now.

  $ ssh vagrant@192.168.xxx.xxx
  $ sudo chef-client

This will connect your new node to your chef server. If you log in to the chef server dashboard you will now see a new host ready to be managed. You can specify the role you want to this new node to take and the environment. Before you do that you need to upload all cookbooks, roles and environments.

The easiest way to manage cookbooks is to use the Berkshelf gem. Berkshelf is to chef what bundler is to rubygems. It allows you to configure a Berksfile that will tell berkshelf which community cookbooks to download. It also makes uploading a batch of cookbooks to the chef server much easier.

  $ gem install berkshelf
  $ berks install
  $ berks upload

Next we can upload our roles.

  $ knife role from file roles/base.rb
  Updated Role base!

Next we can upload our environments. I prefer to make the changes to the environments file located in environments/ and upload them all instead of editing them one at a time. This ensures that the configuration in version control matches the configuration uploaded to the chef server.

  $ knife environment from file --all

Now that your roles, environments and cookbooks have been uploaded to the chef server, you can now specifiy which roles and environment that your new node should belong to. Log in to the chef server and specify the environment for your node. Then edit the run list and choose the roles that you wish to apply.

Now you just need to re-run the chef-client.

  $ ssh vagrant@192.168.xxx.xxx
  $ sudo chef-client

This time when you run chef-client, your node should have the cookbooks defined for the role in it’s run list. If you want the chef-client to run automatically you can use the chef-client cookbook which will configure your node to run chef-client every 30 minutes. This ensures that your node adheres to policy defined on the chef server.

devops 💎