Head first Chef

Posted by Unknown on

As a beginner it is quite challenging to understand where to begin with Chef, In this blog series on Chef I attempt to share my learning path and hopefully this will help others as well, Chef has evolved considerably in the last two years and so has the the tools surrounding this technology, not until recently the folks at chef did a great job of putting together best of breed tools and technologies and have created at SDK referred to as ChefDK. In the first series we will walk through the ChefDK installation, creation of simple cookbooks and spinning up a virtual machine in under 5 minutes.. Yeah that's what it takes to get a head start with ChefDK.

Installation

Usage

Chef Client can operate in two modes.

Local Mode – it simulates full chef server instance in memory, any data that would have been saved on server is saved locally, and this mode is used for rapid chef recipe development. We will be using this mode for our chef learning.

Client Mode – In this mode it is assumed that we have a Chef server running on another system, this mode is used in production or production like environment.

Exercise 1: Automating VM Creation 

"chef" utility was released along with the ChefDK and this streamlines various workflows associated with chef development. We will be using this utility for our cookbook generation.

>> chef generate app test_vm

This command will generate the basic template for the cookbook development, it will create a folder names hello-chef with quite a few files and sub folders in it. As a beginner we can focus on couple of files.

Take a look at the generated .kitchen.yml file and edit as follows, we are going to spin up a Cent OS VM and hence the name "centos-7.0"
Refer the Chef docs at https://docs.getchef.com/config_yml_kitchen.html for more details on the various parameters used in the kitchen.yml file

---
driver:
  name: vagrant

provisioner:
  name: chef_zero

platforms:
  - name: centos-7.0

suites:
  - name: default
    run_list:
      - recipe[test_vm::default]
    attributes:

Now Type following command and it should list various virtual machines supported by this cookbook, in this case you should get centos listed in the output

>> kitchen list
Instance          Driver   Provisioner  Last Action
default-centos70  Vagrant  ChefZero     <Not Created>

Create the virtual machine
>> kitchen converge
Once the machine is created you should be able to login from the command prompt
>> kitchen login


Exercise 2: Automating VM Creation + Apache Server Setup

In the previous exercise we did not really write a cookbook, all we did was to tweak the generated kitchen.yml file to spin up a virtual machine.
We will now edit the recipes/default.rb file to write our first cookbook to install an Apache Webserver.


execute "update Packages" do
  command 'yum list updates'
end

package 'httpd' do
  action :install
end

service 'httpd' do
  action [ :enable, :start ]
end

cookbook_file "/var/www/html/index.html" do
  source 'index.erb'
  mode '0644'
end

Create "index.erb" under templates/default folder, we will use this as the welcome page for the Apache server.

Hello World!! from <%= node['hostname'] %>
Create the virtual machine

>> kitchen converge
Access the http server running inside the newly created virtual machine, since we created a port forwarder we should be able to access it from the host machine as follows. http://localhost:8080/
you should be able to see the page with the following content
"Hello World!! from default-centos-70"

In this blog we looked at basics of ChefDK setup and few basic examples of Cookbook creation, In the next set of articles we will look at more advanced cookbook authoring such as using a community cookbook and creating custom cookbooks.


Reference

ChefDK - https://docs.getchef.com/#chef-dk-title
Test Kitchen - http://kitchen.ci/



4 comments: