Wouldn’t it be nice to be able to spin up a Chef Server locally using a smaller memory footprint than shown in Chapter 9? On many systems, requiring 2 GB of free memory just to simulate a production Chef Server environment is a lot to ask. The Chef Development Kit and Chef Client just so happen to include a stripped-down version of Chef Server for this very purpose, called 
chef-zero.chef-zero runs comfortably in as little as 20 MB of memory. Because it is small, it also starts up quickly, which is great for testing. In order to fit into such a small memory footprint, chef-zero sacrifices a few things. There is no web UI, nor is there any persistence; once Chef Zero is stopped, all data is lost. Neither of these two things is needed for testing.
Test Kitchen provides built-in support for 
chef-zero. Let’s go through a simple example of how you can use chef-zero with Test Kitchen. It’s great for testing your cookbook in a sandbox environment with chef-client using chef-zero as a simulated Chef Server, so you can test cookbooks that exploit Chef Server-specific features. We’ll be covering more of these server-specific features in the remainder of this book, so having a nimbler test environment available will be handy.Test Kitchen and Chef Zero
Generate a cookbook named 
zero with chef generate cookbook or knife cookbook create, depending on whether you are using the Chef Development Kit or the Chef Client respectively. Also, enable the cookbook to use Test Kitchen. We’re going to go through the cookbook creation steps quickly in this chapter. If you need a refresher on what each of these commands mean and the expected output, refer back to Chapter 7.
Chef Development Kit:
Chef Client:
Edit the 
provisioner: stanza in the generated .kitchen.yml to use chef_zero. As of this writing, chef_zerois not the default provisioner, but it might be by the time you read this. Also, edit the .kitchen.yml file to use the CentOS 6.5 basebox we prepared specifically for this book. Next, assign a private network address like we did in Chapter 7. This time, we’re going to use the IP address 192.168.33.34. If this conflicts with an address already being used on your local network, change it to be a nonconflicting one.
Because we just want to present an overview of what 
chef-zero does, we’re going to use the default-generated cookbook, which does nothing. Perform a kitchen converge to perform a Chef run using chef-zero. You should notice that the output looks a little different than when you used chef-solo:
Here’s an overview of the steps Test Kitchen performed to set up 
chef-zero in the sandbox environment. It:- Installed Chef Client
 - Created fake validation.pem and client.pem keys in /tmp/kitchen
 - Generated client.rb (the configuration file for 
chef-client) in /tmp/kitchen - Generated dna.json file with run list in /tmp/kitchen
 - Synchronized cookbooks on host in /tmp/kitchen/cookbooks
 - Ran 
chef-clientin local mode. The full command line used ischef-client --local-mode --config /tmp/kitchen/client.rb --log_level --chef-zero-port 8889 --json-attributes dna.json 
Destroy the sandbox environment; we’re done with it for now:
One important thing to remember is that Test Kitchen runs 
chef-zero in the background during the Chef run, then stops chef-zero once the Chef run is complete. It does not leave chef-zero running, nor does it configure knife to run in your sandbox environment. As discussed in Chapter 9, knife is the primary tool for interacting with a Chef Server. It is handy to be able to simulate knife as well in a test environment. But we need to do a few more things in order to also simulate a Chef Server using knife.Running Chef-Zero on Your Host Using Chef-Playground
You can also run 
chef-zero on your host. The most likely reason you’ll want to do this is to simulate a Chef Server so you can run the knife tool, like we did in Chapter 9. When you want to interact with a Chef Server, you’ll find yourself using knife on your host Development Workstation even when you are using Test Kitchen. Also, some Chef Server features such as data bags or search really benefit from being able to use knife, even during testing.
We’ll be creating a project directory called chef-playground which models the 
chef-repo setup we used in Chapter 9, but uses chef-zero instead. We’ll follow similar steps that we used in Test Kitchen and Chef Zero:- Assume Chef Client or the Chef Development Kit is installed.
 - Create fake validation.pem and client.pem keys.
 - Create knife.rb (the configuration file for 
knife). - Run 
chef-zero. - Synchronize cookbooks with 
chef-zerosimulated Chef Server. - Run 
knife. 
Create a directory called chef-playground, and make it the current working directory:
Then create another subdirectory called .chef (similar to the chef-repo/.chef directory we created in Chapter 9), which will contain our fake keys and configuration files:
Use the 
ssh-keygen tool to generate some client keys. They don’t need to be real keys tied to a user or to Chef, but they do need to contain a readable key. We discussed the purpose of the client.pem file in Chapter 10. In this case, we’ll name the file devhost.pem, which matches the devhost name we’ll be using for our Development Workstation. Enter in the following command lines. (The -P option supplies a passphrase for the key. In this case, we don’t want a passphrase, so we pass in double quotes [""] to supply a blank password.)
Linux/Mac OS X/Windows Command Prompt:
Windows PowerShell:
Create a knife.rb file in the chef-playground/.chef directory as shown in Example 11-2. This is the final configuration file you need to create.
Finally, open up a separate terminal window and run 
chef-zero alongside the command prompt in which you are doing these hands-on-exercises, as shown in Figure 11-1. Run chef-zero as shown in the following code, passing in a port number besides the default port 8889, so you won’t conflict with other Chef tools running on your host in local mode. If you discover a conflict with the suggested port 9501, use another.NOTE
As shown in Figure 11-1, when run on a command line, 
chef-zero will display that it is listening, not returning to the command prompt. This is why we recommend running it in a separate window. Leave chef-zero running for now.
Make sure that the chef-playground directory is the current working directory. Assuming everything is configured properly, when you run 
knife client list, it should return chef-validator and chef-webui as shown in the following code block:
The 
knife tool will look for configuration files and credentials in the $HOME/.chef directory by default. If knife doesn’t find anything in this default location, it will then walk up a directory tree looking for the first .chefdirectory, if it exists. This is a recommended way to arrange your configuration files, if you have to work against multiple Chef servers using the Chef tools in client mode—sprinkle .chef directories in root locations that make sense for the project, such as chef-playground/.chef.NOTE
We won’t be using 
knife --local-mode in this book, but it’s helpful to mention. Similar to chef-client, the knife tool supports a local mode using the --local-mode option. A benefit to using local mode with knife is that it will automatically start chef-zero for you.
You could have run the following to run 
knife in local mode to check clients. A benefit to this approach is that it will automatically start chef-zero for you. This doesn’t conflict with the chef-zero instance you already started because it is running on a different port than the default port 8889.
However, you’ll notice that the output differs compared to when 
knife is running in “client” mode. So we won’t be making use of the local mode feature in this book. Jon Cowie’s Customizing Chef book covers the use of both chef-client and knife using local mode.
Before we finish with our 
chef-playground project, let’s pre-populate chef-zero with some node information so we can get more useful test results back from search queries against Chef Server.
Create a directory called nodes underneath chef-playground, and make it the current working directory:
Within the chef-playground/nodes directory, create three files, as shown in Example 11-3, Example 11-4, and Example 11-5. When you are done, the chef-playground directory structure should resemble the following:
Once you have the files in the nodes/ subdirectory created, make sure that chef-playground is the current working directory. Then, run the 
knife upload command to create the node information on the server. We’ll use this knife upload technique in subsequent chapters of this book to pre-populate the chef-zero server with test data before running other knife commands:
Now if you run the 
knife node list command, you’ll see that chef-zero thinks that there are three nodes being managed:Summary
In this chapter, we showed you how Chef Zero provides a complete, in-memory version of Chef Server that is easy to install and great for checking out features of Chef Server locally without needing to have a full Chef Server setup. We’ll be using this nimbler implementation of Chef Server for the rest of the exercises in this book.

Very nice explanation on chef-zero to get started and explore
ReplyDelete