running the show: configuration management with chef

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

Upload: best-tech-videos

Post on 10-Apr-2015

3.128 views

Category:

Documents


4 download

DESCRIPTION

Few completed Rails apps are architecturally simple. As soon as you grow, you find yourself using multiple subsystems and machines to scale, creating new headaches in configuration management. Help is at hand! This tutorial introduces Chef, a modern Ruby-based open source approach to systems integration. Chef lets you manage your servers by writing code, not running commands.

TRANSCRIPT

Page 1: Running the Show: Configuration Management with Chef

Configuration management with Chef

Edd Dumbill [email protected] 2009

1Monday, 4 May 2009

Page 2: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Today’s tutorial

• Overview of Chef

• Learn by example

• Common usage patterns

• Moving on

3Monday, 4 May 2009

Page 4: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

About you

5Monday, 4 May 2009

Page 6: Running the Show: Configuration Management with Chef

Overview

6Monday, 4 May 2009

Page 7: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Developers want

• Don’t Repeat Yourself

• Revision control

9Monday, 4 May 2009

Page 10: Running the Show: Configuration Management with Chef

Chef

• Client-server architecture

• Embraces modern web technologies

• Written in Ruby

10Monday, 4 May 2009

Page 11: Running the Show: Configuration Management with Chef

Chef

• Cleeent-serfer ercheetectoore-a

• Imbreces mudern veb technulugeees

• Vreettee in Rooby

• Bork bork bork

11Monday, 4 May 2009

Page 12: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Chef vs Puppet

13Monday, 4 May 2009

Page 14: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Getting started

16Monday, 4 May 2009

Page 17: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Server

• Apache + Passenger

• Provides administrative Web UI

• Users identified by OpenID

• Recipes defined by your chef repository

19Monday, 4 May 2009

Page 20: Running the Show: Configuration Management with Chef

Client

• Invocation of chef-client

• One-time

• As a daemon

chef-client -i 3600 -s 600

20Monday, 4 May 2009

Page 21: Running the Show: Configuration Management with Chef

Chef repository

• Contains configuration and cookbooks

• Clone the Opscode template to start

• Copy your configuration

21Monday, 4 May 2009

Page 22: Running the Show: Configuration Management with Chef

First look at the server

22Monday, 4 May 2009

Page 23: Running the Show: Configuration Management with Chef

First client run

23Monday, 4 May 2009

Page 24: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Define an attribute

• Simple attributeattributes/my_name.rb

my_name “John Henry”

27Monday, 4 May 2009

Page 28: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

The template

• templates/default/hello_world.txt.erb

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

29Monday, 4 May 2009

Page 30: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Attributes & resources

32Monday, 4 May 2009

Page 33: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Advanced attributes

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

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

35Monday, 4 May 2009

Page 36: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Other FS resources

• directory — analog of the File resource

• remote_directory — recursive remote copy

44Monday, 4 May 2009

Page 45: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Other resources

• User

• Group

• Cron

• Route

• Mount

46Monday, 4 May 2009

Page 47: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Resource tricks

50Monday, 4 May 2009

Page 51: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

OpsCode Cookbook

55Monday, 4 May 2009

Page 56: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

mysql cookbook

• mysql::client, mysql::server

• EC2-aware

62Monday, 4 May 2009

Page 63: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Chef and Rails

64Monday, 4 May 2009

Page 65: Running the Show: Configuration Management with Chef

How Chef can help

• Configuration

• Deployment

• Configuration is the better trodden path

65Monday, 4 May 2009

Page 66: Running the Show: Configuration Management with Chef

Example configuration

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

66Monday, 4 May 2009

Page 67: Running the Show: Configuration Management with Chef

Worked example

• Create and deploy a basic Rails app

67Monday, 4 May 2009

Page 68: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Gotchas

• Chef-deploy assumes shared config/database.yml

• Usual package/gem conflicts

• Don’t install rake from packages!

73Monday, 4 May 2009

Page 74: Running the Show: Configuration Management with Chef

Chef Solo

74Monday, 4 May 2009

Page 75: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Development patterns

79Monday, 4 May 2009

Page 80: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

REST API

84Monday, 4 May 2009

Page 85: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Knife

• Basic command line interface to the server

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

89Monday, 4 May 2009

Page 90: Running the Show: Configuration Management with Chef

Searching

90Monday, 4 May 2009

Page 91: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

Access from REST API

• As implemented in the Web UI

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

93Monday, 4 May 2009

Page 94: Running the Show: Configuration Management with Chef

Chef & EC2

94Monday, 4 May 2009

Page 95: Running the Show: Configuration Management with Chef

In OpsCode cookbooks

• ec2 cookbook

• EC2 awareness in, e.g. mysql recipes

• Bunch of handy EC2 attributes exposed

95Monday, 4 May 2009

Page 96: Running the Show: Configuration Management with Chef

Poolparty

• Configure and deploy to the cloud

• Uses Chef

• http://poolpartyrb.com/

96Monday, 4 May 2009

Page 97: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

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: Running the Show: Configuration Management with Chef

The future

• Chef is evolving rapidly

• Platform support improving through contributions

• Opscode-agent

• nanite

• selective resource execution

99Monday, 4 May 2009

Page 100: Running the Show: Configuration Management with Chef

In conclusion

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

• Q&A

• Thank you!

100Monday, 4 May 2009