Getting Started with Chef Solo. Part 1
WARNING: This article can be outdated. Better read my book about Chef: Cooking Infrastructure by Chef
Hello my dear friends. Today we will talk about Chef and usage Chef Solo like a Pro. All example code you can find here: github.com/le0pard/chef-solo-example/tree/1.0
What is Chef?
Chef is an open-source systems integration framework built specifically for automating the cloud.
Why you should use Chef?
- Efficiency: It’s more effective to use Chef, which will contain all your servers configuration in one place.
- Scalability: Do you need scale you app? Split your server into cloud (several servers) by using environments, roles and nodes.
- Reusing and Save money: No need 10 times install a same software for your application on server. Just create new node in Chef and after several minutes you will have configured instance.
- Documentation: You Chef is also documentation for your cloud, because Chef recipes contain all information about your environment.
And of course main point is Automate All The Things!!!
What doesn’t Chef do?
- “Magically” configure your server
- Blindly reuse cookbooks and recipes
- Monitor your servers or softwares
- Undoing concept
Chef types and terminology
Exists two types of Chef: Chef Solo and Chef Server. Chef Solo is simple way to begin working with Chef what is why I will show how to use it in articles.
This is list of terminology, which I will use in my articles:
- Node - A host where the Chef client will run (web server, database server or another server). Chef Client always working on server, which it configure.
- Chef Client - a command line tool that configures servers.
- Chef Solo - a version of the Chef client that doesn’t rely to the server for configuration (like Chef server).
- Recipes - a single file of Ruby code that contains commands to run on a node (nginx ssl module, apache php module).
- Resources - a node’s resources include files, directories, users and services.
- Cookbook - a collection of Chef recipes (nginx cookbook, postgresql cookbook).
- Role - reusable configuration for multiple nodes (web role, database role, etc).
- Attribute - variables that are passed through Chef and used in recipes and templates (the version number of nginx to install).
- Template - a file with placeholders for attributes, used to create configuration files (simple Erb file).
Initialize chef project
Let’s create our folder, which will contain all our Chef kitchen:
Next I will use bundler to get some useful gems:
List of the required apps and libs:
- knife-solo - knife is a powerful command-line interface (CLI) that comes with Chef. It is used to control Chef client.
- librarian-chef - is a bundler for your Chef-based infrastructure repositories
- vagrant - create and configure lightweight, reproducible, and portable development environments. For this rubygems need installed VirtualBox. We will use vagrant to test our Chef Solo. WARNING! Right now is not possible use vagrant from rubygems. You should install it separately. Also in this case “multi_json” gem will not available in Vagrant file, but we can use JSON gem, which have vagrant.
Next you need to create a kitchen by knife:
Command “init” (“kitchen”) is used to create a new directory structure that fits with chef’s standard structure and can be used to build and store recipes.
Let’s look at the directory structure:
- cookbooks - directory for Chef cookbooks. This directory will be used for vendor cookbooks
- data_bags - directory for Chef Data Bags
- nodes - directory for Chef nodes
- roles - directory for Chef roles
- site-cookbooks - directory for your custom Chef cookbooks
- solo.rb - file used by Chef Solo with configuration settings
Librarian
Now let’s create librarian Cheffile for manage the cookbooks:
And add to Cheffile nginx cookbook. More cookbooks you can find at community.opscode.com.
Now in folder “cookbooks” you should find nginx cookbook and all it dependens:
First node
Next, create a node file. Chef node file always have name as server host. For create this file automatically and check, what Chef Solo installed on server you can use knife command “prepare”. This command installs Ruby, RubyGems and Chef on a given host. It’s structured to auto-detect the target OS and change the installation process accordingly:
This command apply the same parameters as ssh command. Fox example, executing with ssh key:
Let’s for test call our node file “vagrant”:
“run_list” the main part of node, where you specify roles and/or recipes to add to the node. In our case I add recipe source from nginx cookbook. Also you can see nginx attributes (like version, modules, etc.). All cookbook can have directory “attributes” and this directory contain default attributes for cookbook recipes. But you can redefine this attributes in node file. We are ready to test our kitchen!
Vagrant
For testing Chef Solo kitchen by vagrant we need download vagrant box. List of boxes you can find www.vagrantbox.es.
Next we should edit Vagrantfile for define chef solo:
As you can see “run_list” and json attributes from node “vagrant.json” automatically loaded from file. More information about using Chef Solo with Vagrant you can find by this link.
Next, we can try test Chef Solo with Vagrant:
Next, we can check what nginx successfully installed on vagrant image:
Let’s check what nginx is running. Just add in “Vagrantfile” port forwarding:
Next, reload vagrant instance:
And you should see in your browser:
After change something in your kitchen, you should run command “vagrant provision”:
And Chef Solo will be running again on vagrant server.
The main idea of Chef is idempotence: it can safely be run multiple times. Once you develop your configuration, your machines will apply the configuration and Chef will only make any changes to the system if the system state does not match the configured state. For example, first time chef will compile nginx from source and install it on server. On next run it just check, what nginx already compiled and running (if you will not change attributes).
Cook real server
After fully testing the kitchen you can apply your node configuration on real server. You should rename “vagrant.json” on your server host and run commands:
Your server must have installed Chef client. If no, just before command “cook” run command “prepare”.
Summary
In the current article we have learn usage Chef Solo and test our first kitchen. In the next article we will look at the cookbook structure and will write own cookbook.
All example code you can find here: github.com/le0pard/chef-solo-example/tree/1.0.
That’s all folks! Thank you for reading till the end.