configuration management with chef - o'reilly mediaassets.en.oreilly.com/1/event/24/running...

100
Configuration management with Chef Edd Dumbill [email protected] RailsConf 2009 1 Monday, 4 May 2009

Upload: others

Post on 21-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Configuration management with Chef

Edd Dumbill [email protected] 2009

1Monday, 4 May 2009

Page 2: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

About me

• Created Expectnation, event software that runs O’Reilly Conferences

• Co-author of “Learning Rails”

• Perennial tinkerer and author

2Monday, 4 May 2009

Page 3: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Today’s tutorial

• Overview of Chef

• Learn by example

• Common usage patterns

• Moving on

3Monday, 4 May 2009

Page 4: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Meta

• Please rate this talk and leavecomments

• If you’re twittering

• I’m @edd

• Hashtag is #railsconf

• Asking questions

4Monday, 4 May 2009

Page 5: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

About you

5Monday, 4 May 2009

Page 6: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Overview

6Monday, 4 May 2009

Page 7: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Configuration management

• Creating and maintaining consistency

• Installing, updating, reporting

• Rich history in open source tools

• cfengine through to Puppet

7Monday, 4 May 2009

Page 8: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Today’s needs

• Developers are becoming ops people

• Web architectures and cloud computing

• Agile sysadmin should complement agile development

8Monday, 4 May 2009

Page 9: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Developers want

• Don’t Repeat Yourself

• Revision control

9Monday, 4 May 2009

Page 10: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef

• Client-server architecture

• Embraces modern web technologies

• Written in Ruby

10Monday, 4 May 2009

Page 11: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef

• Cleeent-serfer ercheetectoore-a

• Imbreces mudern veb technulugeees

• Vreettee in Rooby

• Bork bork bork

11Monday, 4 May 2009

Page 12: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef

• Has revision control at its core

• Doesn’t make you learn a new language

• Comes from a culture of testability and predictability

12Monday, 4 May 2009

Page 13: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef vs Puppet

13Monday, 4 May 2009

Page 14: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef vs Puppet

• Because we needed another open source war

• Objective differences

• Subjective differences

• Chef has had chance to learn from several years of Puppet

14Monday, 4 May 2009

Page 15: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Architecture

Chef-client

Ohai

Node

Chef ServerChef-client

Ohai

Node

Chef-client

Ohai

Node

Chef-client

Ohai

Client

Chef Indexer

15Monday, 4 May 2009

Page 16: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Getting started

16Monday, 4 May 2009

Page 17: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Assemble your victims

• Use VMs for testing environment

• Ubuntu 8.10 or newer is the sweet spot

• VirtualBox is a free virtualization tool

• Identify a server and one or more clients

17Monday, 4 May 2009

Page 18: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Prerequisites

• Two stage install: basics & bootstrap

• Minimal prerequisites: Ruby & RubyGems

• Install via Gems: ohai and chef

• Bootstrap differs for server and client

18Monday, 4 May 2009

Page 19: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Server

• Apache + Passenger

• Provides administrative Web UI

• Users identified by OpenID

• Recipes defined by your chef repository

19Monday, 4 May 2009

Page 20: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Client

• Invocation of chef-client

• One-time

• As a daemon

chef-client -i 3600 -s 600

20Monday, 4 May 2009

Page 21: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef repository

• Contains configuration and cookbooks

• Clone the Opscode template to start

• Copy your configuration

21Monday, 4 May 2009

Page 22: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

First look at the server

22Monday, 4 May 2009

Page 23: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

First client run

23Monday, 4 May 2009

Page 24: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Node attributes

• Explore with Web UI

• OS attributes provided by ohai

• Other attributes are configured by the installed cookbooks

• Attributes are mutable

24Monday, 4 May 2009

Page 25: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Making a cookbook

• Cookbook is the unit of reuse in Chef

• Unsurprisingly, it contains recipes

• Generate one withrake new_cookbook COOKBOOK=hello_world

25Monday, 4 May 2009

Page 26: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Inside the cookbook

• attributes — variables

• recipes — list of instructions (“resources”)

• files — files used by resources

• templates — ERB templates

• definitions — macros of resources

• libraries — Ruby to extend Chef DSL

26Monday, 4 May 2009

Page 27: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Define an attribute

• Simple attributeattributes/my_name.rb

my_name “John Henry”

27Monday, 4 May 2009

Page 28: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

A simple recipe

template “/tmp/hello_world.txt” do source “hello_world.txt.erb” variables :my_name => node[:my_name] mode 00664 action :createend

• recipes/default.rb

28Monday, 4 May 2009

Page 29: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

The template

• templates/default/hello_world.txt.erb

Hello, <%= @my_name %>, how are you today?

29Monday, 4 May 2009

Page 30: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Running the recipe

• Add the recipe to the node’s recipe list

• Invoke chef-client

• Default chef-client setup has client invoked periodically

30Monday, 4 May 2009

Page 31: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

When chef-client runs

• Node authenticates with server

• Libraries, attributes, definitions & recipes are synchronized

• Libraries, attributes, definitions & recipes compiled

• Node state is converged

• Everything happens on the node

31Monday, 4 May 2009

Page 32: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Attributes & resources

32Monday, 4 May 2009

Page 33: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Attributes

• May be simply defined, e.g. my_name “John Henry”

• Allow overriding, e.g. my_name “John Henry” unless attribute?(“my_name”)

• List values are regular arrays[“foo”, “bar”, “whizz”]

33Monday, 4 May 2009

Page 34: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Attribute hashes

• Logical groupings of configuration information, e.g. Apache settings, network interface properties

• Class used is Mash (from extlib)

• so you can use :foo or ‘foo’ as a key

34Monday, 4 May 2009

Page 35: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Advanced attributes

• Methods: attribute?() & recipe?()

• Access to recipes arrayrecipes << “hello_world” unless recipe?(“hello_world”)

35Monday, 4 May 2009

Page 36: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Resources

• The steps that make up a recipe

package “git-core” do action :installend

• Resources are implemented via Providers

36Monday, 4 May 2009

Page 37: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Package

package "tar" do version "1.16.1-1" action :installend

• Action can be install, upgrade, remove, purge

• Version is optional

37Monday, 4 May 2009

Page 38: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Ruby gems

• Install gems with package toopackage “capistrano” do provider Chef::Provider::Package::Rubygemsend

• Easier:gem_package “capistrano”

• Can use source attribute for gem source

38Monday, 4 May 2009

Page 39: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Remote files

• Copying remote files is easy

remote_file “/tmp/foo.png” do source “foo.png” owner “root” group “root” mode 0444 action :createend

• Where does the file live?

39Monday, 4 May 2009

Page 40: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Search path

• Files and templates are searched for in the following order: FQDN, platform-version, platform, default

• For Ubuntu 9.04: myhost.example.com ubuntu-9.04 ubuntu default

40Monday, 4 May 2009

Page 41: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

More remote file fun

• File source can be a URL

source “http://warez.com/thing.tgz”

• Provide SHA256 hash to prevent needless downloading from chef-server each time

checksum “08da0021”

41Monday, 4 May 2009

Page 42: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Links

• Symbolic or hard links

link “/usr/bin/randomthing1.8” do to “/usr/bin/randomthing”end

• Use link_type :hard or :symbolic (default)

42Monday, 4 May 2009

Page 43: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

File

• Control existence and attributes of a file, not its contents

file “/tmp/whatever” do owner “root” group “root” mode “0644” action :createend

• Other actions are touch, delete

43Monday, 4 May 2009

Page 44: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Other FS resources

• directory — analog of the File resource

• remote_directory — recursive remote copy

44Monday, 4 May 2009

Page 45: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Service

• Control system services from /etc/init.d and friends

• We can en/disable, start, stop & restart

service “my_daemon” do supports :restart => true action [ :enable, :start ]end

45Monday, 4 May 2009

Page 46: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Other resources

• User

• Group

• Cron

• Route

• Mount

46Monday, 4 May 2009

Page 47: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Execute

• Execute arbitrary command

command “mysql-stuff” do execute “/usr/bin/mysql </tmp/foo.sql” creates “/tmp/outfile.sql” environment {‘FOO’ => “bar”} action :run

end

47Monday, 4 May 2009

Page 48: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Script

• bash, perl, python, ruby, csh

bash “install_foo” do user “root” cwd “/tmp” code <<-EOC wget http://example.org/foo.tgz tar xvf foo.tgz && cd foo ./configure && make install EOC

end

48Monday, 4 May 2009

Page 49: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

HTTP Request

• Useful for connecting to existing services

http_request “say_hello” do url “http://myserv.local/check_in” message :node => node[:fqdn] action :postend

• Posts a JSON payload

• GET by default

49Monday, 4 May 2009

Page 50: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Resource tricks

50Monday, 4 May 2009

Page 51: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Notifies

• Chain actionstemplate “/etc/my_daemon/my.cnf” do source “my.cnf.erb” notifies :restart, resources(:service => “my_daemon”)end

• By default, notification postponed until end of run, add :immediately as final argument to override

51Monday, 4 May 2009

Page 52: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Action :nothing

• If you want a resource to run only on a notify, specify action :nothing

execute "index-gem-repository" do command "gem generate_index -d /srv/gems" action :nothingend

52Monday, 4 May 2009

Page 53: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Conditional resources

• Use only_if and not_if to control resource execution

• Takes either shell commands or Ruby blocks, e.g.

only_if do IO.read(“/tmp/foo”).chomp == ‘bar’end

53Monday, 4 May 2009

Page 54: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Platform specifics

• Selective resource executiononly_if do platform?(“ubuntu”) end

• Alter package namepackage "libwww-perl" do case node[:platform] when "centos" name "perl-libwww-perl" end action :upgradeend

54Monday, 4 May 2009

Page 55: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

OpsCode Cookbook

55Monday, 4 May 2009

Page 56: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Opscode cookbooks

• http://github.com/opscode/cookbooks

• Integral part of the Chef project

• If you want it, it’s probably already there

• common configurations

• smoothing over platform specifics

56Monday, 4 May 2009

Page 57: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Using the cookbooks

• Keep your own stuff in site-cookbooks

• Use git to add cookbooks as a submodule

git submodule add git://github.com/opscode/cookbooks.git cookbooksgit submodule initgit submodule update

57Monday, 4 May 2009

Page 58: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

3rd party cookbooks

• The cookbook_path from the server config specifies precedence

• By default site-cookbooks overrides cookbooks

• You can adapt recipes simply by replacing the parts you wish

58Monday, 4 May 2009

Page 59: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

apache2 cookbook

• Attributes configure basic preferences (ports, timeout, keepalive)

• Default recipe sets up sane configuration

• apache2:: namespace includes recipes for common modules

59Monday, 4 May 2009

Page 60: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Overriding attributes

• If you control cookbook, easy enough to set a default

• Per-node customizations can be made in the UI

• To set new defaults, override selectively in site-cookbooks

60Monday, 4 May 2009

Page 61: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

apache2 definitions

• Macro for a2ensite & friends

apache_site “my_app” :enable => trueend

• web_app — wraps most of the common configuration for a web app (e.g. Rails)

61Monday, 4 May 2009

Page 62: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

mysql cookbook

• mysql::client, mysql::server

• EC2-aware

62Monday, 4 May 2009

Page 63: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Rails cookbook

• Provides installation recipe and attributes for tuning

• rails[:version]

• rails[:environment]

• rails[:max_pool_size]

• Provides web_app template you can copy

63Monday, 4 May 2009

Page 64: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef and Rails

64Monday, 4 May 2009

Page 65: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

How Chef can help

• Configuration

• Deployment

• Configuration is the better trodden path

65Monday, 4 May 2009

Page 66: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Example configuration

• Naive Chef recipe to get all the prequisites in place for an instance of Expectnation

66Monday, 4 May 2009

Page 67: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Worked example

• Create and deploy a basic Rails app

67Monday, 4 May 2009

Page 68: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

chef-deploy

• A resource that implements Rails application deployment

• Models Capistrano’s cached_deploy

• In rapid development, used at EngineYard

• http://github.com/ezmobius/chef-deploy

68Monday, 4 May 2009

Page 69: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

deploy "/data/#{app}" do repo "git://server/path/app.git" branch "HEAD" user "myuser" enable_submodules true migrate true migration_command "rake db:migrate" environment "production" shallow_clone true revision '5DE77F8ADC' restart_command “...” role “myrole” action :deployend

69Monday, 4 May 2009

Page 70: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Callbacks

• Ruby scripts in your app’s deploy/

• before_migrate, before_symlink, before_restart, after_restart

• Rails environment and ‘role’ passed as arguments to callback

• Could control this via role node[:myapp][:role]

70Monday, 4 May 2009

Page 71: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Single source for gem dependencies

• Specify gems in gems.yml in your app’s root

- :name: foo :version: "1.3"- :name: bar :version: "2.0.1"

71Monday, 4 May 2009

Page 72: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Deployment strategy

• Unlikely you want deploy to be attemped with the default chef-client behavior

• chef-deploy developed against a Chef Solo world view: explicit execution

• Use attribute to control deployment

• Work in progress

72Monday, 4 May 2009

Page 73: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Gotchas

• Chef-deploy assumes shared config/database.yml

• Usual package/gem conflicts

• Don’t install rake from packages!

73Monday, 4 May 2009

Page 74: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef Solo

74Monday, 4 May 2009

Page 75: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Server-less operation

• Bundle up the cookbooks in a tarball

• Set attributes in a JSON file

• Good to go!

75Monday, 4 May 2009

Page 76: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Deploying with solo

• Tar up your cookbooks

• Create a solo.rbfile_cache_path “/tmp/chef-solo”cookbook_path “/tmp/chef-solo/cookbooks”

• Currently, must have unified cookbook tree

76Monday, 4 May 2009

Page 77: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Deploying with solo (2)

• Create your JSON, e.g.{ “recipes”: “chef-server”, “myvar”: “foo” }

• Executechef-solo -c solo.rb -j chef.json -r http://path/to/tarball.tgz

• JSON path can be URL too

77Monday, 4 May 2009

Page 78: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Why Chef Solo?

• When you don’t or can’t control access to the server

• When clients aren’t in the same security zone

• When you care about installation rather than long-term maintenance

78Monday, 4 May 2009

Page 79: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Development patterns

79Monday, 4 May 2009

Page 80: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Git strategy

• Use submodules to bring in 3rd party cookbooks

• Develop against testbed, push to shared repository

• Server install rule does a git pull

80Monday, 4 May 2009

Page 81: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

VM testbed

• Use a VM tool that supports snapshotting

• VirtualBox is free

• VMware good, supported by Poolparty

• Use Avahi/Bonjour for convenience

81Monday, 4 May 2009

Page 82: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Attribute control

• Particularly useful with chef-solo, transfer the onus of control over to the attributes

• Control recipe execution via, eg. a ‘role’ attribute

• Help DRY by listing packages, etc, in attributes

82Monday, 4 May 2009

Page 83: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Refactor into definitions & attributes

• For maintainability, consider refactoring obvious components into definitions

• e.g. the directory creation stage of a Rails app (what cap deploy:setup does)

83Monday, 4 May 2009

Page 84: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

REST API

84Monday, 4 May 2009

Page 85: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef’s REST API

• Chef’s REST API is pretty mature

• Reused a lot internally

• Best way to programmatically integrate

• Documentation scarce for now

85Monday, 4 May 2009

Page 86: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

What can you do with the API?

• Programmatic access to the server

• Add remove/recipes from nodes

• Interrogate and set attributes

• Perform searches

86Monday, 4 May 2009

Page 87: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

API authentication

• Register in the same way a node does

Chef::Config.from_file( “/etc/chef/server.rb”)@rest = Chef::REST.new( Chef::Config[:registration_url])@rest.register(user, password)

• Thereafter, [email protected](user, password)

87Monday, 4 May 2009

Page 88: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Manipulating nodesnode = @rest.get_rest(“nodes/foo_example_com”)

puts node.recipes.inspectnode.recipes << “apache2”

puts node[:myattr].inspectnode[:myattr] = { :foo => “bar” }

@rest.put_rest(“nodes/foo_example_com”, node)

88Monday, 4 May 2009

Page 89: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Knife

• Basic command line interface to the server

• For now, get from http://gist.github.com/104080

89Monday, 4 May 2009

Page 90: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Searching

90Monday, 4 May 2009

Page 91: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Searching the server

• Powerful feature

• Not that mature yet

• Ferret indexes the Chef Server database

• Queries expressed in FQL

91Monday, 4 May 2009

Page 92: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Access from recipes

• search(INDEX, QUERY)

• search(:node, “*”) reports every node in the DB

• Find the IP of every node running Apachesearch(:node, “recipe:apache2”).collect {|n| n[‘ipaddress’]}

92Monday, 4 May 2009

Page 93: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Access from REST API

• As implemented in the Web UI

@rest.get_rest( "search/node?q=recipe:apache2")

93Monday, 4 May 2009

Page 94: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Chef & EC2

94Monday, 4 May 2009

Page 95: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

In OpsCode cookbooks

• ec2 cookbook

• EC2 awareness in, e.g. mysql recipes

• Bunch of handy EC2 attributes exposed

95Monday, 4 May 2009

Page 96: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Poolparty

• Configure and deploy to the cloud

• Uses Chef

• http://poolpartyrb.com/

96Monday, 4 May 2009

Page 97: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

What Poolparty does

• Launches VM (EC2 or VMware), waits for IP and ssh

• Bootstrap: rsyncs dependencies and installs

• Configure: compile cookbooks, rsyncs, executes Chef Solo

• Verifies installation

97Monday, 4 May 2009

Page 98: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

Community resources

• Wiki is a great and ever-improving reference http://wiki.opscode.com/display/chef/Home

• IRCirc://irc.freenode.net/chef

• Mailing list

98Monday, 4 May 2009

Page 99: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

The future

• Chef is evolving rapidly

• Platform support improving through contributions

• Opscode-agent

• nanite

• selective resource execution

99Monday, 4 May 2009

Page 100: Configuration management with Chef - O'Reilly Mediaassets.en.oreilly.com/1/event/24/Running the Show_ Configuration... · Configuration management with Chef Edd Dumbill edd@oreilly.com

In conclusion

• Please rate this tutorial and leave comments http://bit.ly/chef-rails

• Q&A

• Thank you!

100Monday, 4 May 2009