Top Banner
Copyright © 2010 Opscode, Inc - All Rights Reserved 1 Speakers: Joshua Timberman Technical Evangelist Automated Infrastructure is on the Menu with Chef OSCON 2010 Aaron Peterson Technical Evangelist
130

Automated infrastructure is on the menu

Oct 18, 2014

Download

Technology

Presentation from the OSCON 2010 tutorial on Chef
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 1

Speakers:

Joshua Timberman Technical Evangelist

Automated Infrastructure is on the Menu with Chef

OSCON 2010

Aaron Peterson Technical Evangelist

Page 2: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Agenda

Chef 101Getting StartedCooking with Chef

2

Page 3: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Meta

Rate the tutorial and comment‣ http://bit.ly/chef-oscon2010

Twitter IDs, hashtags

‣ #OSCON‣ @opscode, #opschef‣ @jtimberman, @metaxis

Questions

3

Page 4: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 4http://www.flickr.com/photos/anotherphotograph/2100904507/sizes/o/

Joshua Timberman

Aaron Peterson

Page 5: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Developers?Systems Administrators?

http://www.flickr.com/photos/timyates/2854357446/sizes/l/

5

Page 6: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

The Opscode Platform is our Chef Server

6

http://www.opscode.com

Page 7: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 7http://www.brooklynstreetart.com/theBlog/wp-content/uploads/2008/12/swedish_chef_bork-sleeper-cell.jpg

Page 8: Automated infrastructure is on the menu

At a High Level...

‣ A library for configuration management

‣ A configuration management system

‣ A systems integration platform

‣ An API for your entire Infrastructure

http://www.flickr.com/photos/asten/2159525309/sizes/l/

Page 9: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 9

Principles

IdempotentData-drivenSane defaultsHackabilityTMTOWTDI

Page 10: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Multiple applications of an operation do not change the result

10http://www.flickr.com/photos/redjar/360111326/

Page 11: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

We start with APIs, you supply data

11http://www.flickr.com/photos/ninjanoodles/153893226/

Page 12: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

option :json_attribs, :short => "-j JSON_ATTRIBS", :long => "--json-attributes JSON_ATTRIBS", :description => "Load attributes from a JSON file or URL", :proc => nil

option :node_name, :short => "-N NODE_NAME", :long => "--node-name NODE_NAME", :description => "The node name for this client", :proc => nilDefaults are sane, but

easily changed12

Page 13: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Open source and community

13

Page 14: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 14

Page 15: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Tim Toady is a Perl motto

15http://www.flickr.com/photos/lidarose/225156612

Page 16: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

The world moves pretty fast

PrimitivesEnable YOUKnow your systems

16http://www.flickr.com/photos/gi/518613153/sizes/o/

Page 17: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

You better be ready!

Sysadmins are programmers You need a 3GL

17http://www.flickr.com/photos/gi/518613153/sizes/o/

Page 18: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Do I need to know Ruby?

18

A littleSimple syntaxComplex as you scale

Page 19: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

A Tour of Chef

19

Page 20: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef Client runs on your systems

20

Page 21: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Clients talk to a Chef Server

21

Page 22: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Clients authenticate with RSA keys

22http://www.flickr.com/photos/debbcollins/3401944550/

Page 23: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef Server

23

CouchDB

API

SOLRAMQPIndexer

Chef Server

ClientInteraction

Search

Datastore

Page 24: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

RESTful API w/ JSON responses

24

Page 25: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef can also stand alone - Chef Solo

25

Page 26: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved http://www.flickr.com/photos/peterrosbjerg/3913766224/ 26

We call each system you configure a Node

Page 27: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Nodes have Attributes

27

{ "kernel": { "machine": "x86_64", "name": "Darwin", "os": "Darwin", "version": "Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386", "release": "10.4.0" }, "platform_version": "10.6.4", "platform": "mac_os_x", "platform_build": "10F569", "domain": "local", "os": "darwin", "current_user": "jtimberman", "ohai_time": 1278602661.60043, "os_version": "10.4.0", "uptime": "18 days 17 hours 49 minutes 18 seconds", "ipaddress": "10.13.37.116", "hostname": "cider", "fqdn": "cider.local", "uptime_seconds": 1619358 }

Kernel info!

Platform info!

Hostname and IP!

Page 28: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Attributes are Searchable

28

$ knife search node ‘platform:mac_os_x’

search(:node, ‘platform:mac_os_x’)

Page 29: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Nodes have a Run List

29

What Roles or Recipes to applyin Order

Page 30: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Nodes have a Run List

30

% knife node show web01-prod.example.com -r{ "run_list": [ "role[production]", "role[webserver]" ]}

Page 31: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 31http://www.flickr.com/photos/laenulfean/374398044/

Nodes have Roles

Page 32: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Roles

Describe the node

‣ webserver‣ dbserver‣ monitoring‣ ... etc

32

Page 33: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Roles have a Run List

33

What Roles or Recipes to applyin Order

Page 34: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 34

name "webserver"description "Systems that serve HTTP traffic"

run_list( "role[base]", "recipe[apache2]", "recipe[apache2::mod_ssl]")

default_attributes( "apache" => { "listen_ports" => [ "80", "443" ] })

override_attributes( "apache" => { "max_children" => "50" })

Can includeother roles!

Page 35: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 35

% knife role show webserver { "name": "webserver", "default_attributes": { "apache": { "listen_ports": [ "80", "443" ] } }, "json_class": "Chef::Role", "run_list": [ "role[base]", "recipe[apache2]", "recipe[apache2::mod_ssl]" ], "description": "Systems that serve HTTP traffic", "chef_type": "role", "override_attributes": { "apache2": { "max_children": "50" } } }

Uploading roles tothe Chef Server

converts Ruby DSLto JSON!

Page 36: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Roles are Searchable

36

$ knife search role ‘max_children:50’

search(:role, ‘max_children:50’)

Page 37: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef manages Resources on Nodes

37

Page 38: Automated infrastructure is on the menu

Resources...

‣ Have a type

‣ Have a name

‣ Have parameters

‣ Take action to put the resource in the declared state

package "apache2" do version "2.2.11-2ubuntu2.6" action :installend

template "/etc/apache2/apache2.conf" do source "apache2.conf.erb" owner "root" group "root" mode 0644 action :createend

Declare a description of the state a part of the node should be in

Page 39: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Resources take action through Providers

39

Page 40: Automated infrastructure is on the menu

Providers...

Multiple providers per resource type.

Know how to actually perform the actions specified by a resource.

Apt, Yum, Rubygems, Portage, Macports, FreeBSD Ports, etc.

http://www.flickr.com/photos/affableslinky/562950216/

Can be overridden with

the provider parameter on a

resource.

package "apache2" do provider "Chef::Provider::Package::Dpkg" action :installend

Page 41: Automated infrastructure is on the menu

Resources

http://www.flickr.com/photos/acurbelo/2628837104/sizes/o/

Platform

Provider

Page 42: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 42

:ubuntu => { :default => { :package => Chef::Provider::Package::Apt, :service => Chef::Provider::Service::Debian, :cron => Chef::Provider::Cron, :mdadm => Chef::Provider::Mdadm }},

Chef::Platform

Page 43: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reservedhttp://www.flickr.com/photos/roadsidepictures/2478953342/sizes/o/ 43

Recipes are lists of Resources

Page 44: Automated infrastructure is on the menu

Recipes...Apply resources in the order they are specified

http://www.flickr.com/photos/roadsidepictures/2478953342/sizes/o/

package "apache2" do version "2.2.11-2ubuntu2.6" action :installend

template "/etc/apache2/apache2.conf" do source "apache2.conf.erb" owner "root" group "root" mode 0644 action :createend

1

2

‣ Evaluates resources in the order they appear

‣ Adds each resource to the Resource Collection

[ "package[apache2]", "template[/etc/apache2/apache2.conf]"]

1

2

Page 45: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Order Matters

45

Page 46: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Recipes are just Ruby!

46

extra_packages = case node[:platform] when "ubuntu","debian" %w{ ruby1.8 ruby1.8-dev rdoc1.8 ri1.8 libopenssl-ruby } end

extra_packages.each do |pkg| package pkg do action :install endend

Page 47: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cookbooks are packages for Recipes

47

Page 48: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cookbooks

DistributableInfrastructure as CodeVersion control repository

48

Page 49: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 49

Common Cookbook Components

RecipesAssets (files/templates)AttributesMetadata

Page 50: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cookbook assets

Files

‣ Static assets‣ Downloaded via cookbook_file‣ File specificity

Templates

‣ Dynamic assets‣ ERB (erubis)‣ File specificity

50

Page 51: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cookbooks

Attributes

‣ Node attributes‣ default, normal, override

51

default[:apache][:listen_ports] = [ "80","443" ]default[:apache][:keepalive] = "On"default[:apache][:contact] = "[email protected]"default[:apache][:timeout] = 300

set[:apache][:log_dir] = "/var/log/apache2"set[:apache][:user] = "www-data"

override[:apache][:dir] = "/etc/apache2"

Page 52: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Metadatacookbooks/django/metadata.rbmaintainer "Opscode, Inc."maintainer_email "[email protected]"license "Apache 2.0"description "Installs DJango"long_description IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))version "0.8.0"

recipe "django", "Installs django and apache2 with mod_python"

%w{ ubuntu debian }.each do |os| supports osend

%w{ apache2 python }.each do |cb| depends cbend

52

Cookbooks

Page 53: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 53

Cookbooks are shareable!

cookbooks.opscode.com

Page 54: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Data bags store arbitrary data

54

Page 55: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

% knife data bag show users jtimberman{ "comment": "Joshua Timberman", "groups": "sysadmin", "ssh_keys": "ssh-rsa SUPERSEKRATS jtimberman@cider", "files": { ".zshrc": { "mode": "0644", "source": "dot-zshrc" }, ".vimrc": { "mode": "0644", "source": "dot-vimrc" } }, "id": "jtimberman", "uid": 7004, "shell": "/usr/bin/zsh", "openid": "http://jtimberman.myopenid.com/"}

A user data bag item...

Page 56: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Data Bags are Searchable

56

$ knife search users ‘shell:/bin/bash’

search(:users, ‘/bin/bash’)

Page 57: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Data bags make recipes awesome-r (that’s

totally a word)

57

bash_users = search(:users, 'shell:/bin/bash')

bash_users.each do |u| user u['id'] do uid u['id'] shell "/usr/bin/zsh" comment u['comment'] supports :manage_home => true home "/home/#{u['id']}" end

directory "/home/#{u['id']}/.ssh" do owner u['id'] group u['id'] mode 0700 end

template "/home/#{u['id']}/.ssh/authorized_keys" do source "authorized_keys.erb" owner u['id'] group u['id'] mode 0600 variables :ssh_keys => u['ssh_keys'] endend

Page 58: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 58http://www.flickr.com/photos/38299630@N05/3635356091/

Page 59: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Getting Started

59http://www.flickr.com/photos/rowens27/3163470179/

Page 60: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Platforms

60

Debian

Ubuntu

Gentoo

SuSEMac OS X

Solaris

Red Hat Fedora

CentOS

Windows

ArchLinux

Scientific

OpenBSD

FreeBSD

Page 61: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Ruby

61http://www.flickr.com/photos/thisisbossi/3526698689/

Page 62: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Today’s Examples

62

Opscode PlatformMac OS X 10.6.4Ubuntu 10.04RubyGems

Page 63: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

The Opscode Platform is our Chef Server

63

http://www.opscode.com

Page 64: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

RubyGems Installation

64

cd /tmpwget http://production.cf.rubygems.org/\rubygems/rubygems-1.3.7.tgz -O- | tar zxf -cd rubygems-1.3.7 && sudo ruby setup.rbln -svf /usr/bin/gem1.8 /usr/bin/gem

sudo gem install chef

Page 65: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

apt.opscode.com

65

Page 66: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

ELFF Yum Repo

66

Page 67: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 67

Create Chef Repository

% git clone git://github.com/opscode/chef-repo.git % cd chef-repo% ls -ladrwxr-xr-x 13 jtimberman staff 442 Jul 7 16:48 ./drwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 ../drwxr-xr-x 5 jtimberman staff 170 Jul 7 17:55 .chef/drwxr-xr-x 12 jtimberman staff 408 Jul 7 16:48 .git/-rw-r--r-- 1 jtimberman staff 23 Jul 7 16:48 .gitignore-rw-r--r-- 1 jtimberman staff 269 Jul 7 15:54 README-rw-r--r-- 1 jtimberman staff 2171 Jul 7 15:54 Rakefiledrwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 certificates/drwxr-xr-x 7 jtimberman staff 238 Jul 7 17:03 config/drwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 cookbooks/drwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 data_bags/drwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 roles/drwxr-xr-x 3 jtimberman staff 102 Jul 7 15:54 site-cookbooks/

Page 68: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 68

Page 69: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Setup User Environment

69

cp USERNAME.pem ~/chef-repo/.chefcp ORG-validator.pem ~/chef-repo/.chefcp knife.rb ~/chef-repo/.chef

Page 70: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Configure Knife

70

% cat .chef/knife.rbcurrent_dir = File.dirname(__FILE__)log_level :infolog_location STDOUTnode_name "oscon"client_key "#{current_dir}/oscon.pem"validation_client_name "oscon-validator"validation_key "#{current_dir}/oscon-validator.pem"chef_server_url "https://api.opscode.com/organizations/oscon"cache_type 'BasicFile'cache_options( :path => "#{ENV['HOME']}/.chef/checksums" )cookbook_path ["#{current_dir}/../cookbooks"]

Per-directory configuration!

Page 71: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 71

Explore Knife’s sub-commands

Page 72: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Knife Sub-commands

72

knife NOUN verb NOUN (options)

knife client listknife node show cider.localknife role show webserverknife search node “*:*” -iknife --help

Page 73: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Configure Chef on workstation

73

% knife configure client /etc/chefINFO: Creating client configurationINFO: Writing client.rbINFO: Writing validation.pem

% ls -l /etc/chef/total 24-rw-r--r-- 1 jtimberman staff 151 Jul 8 21:29 client.rb-rw-r--r--@ 1 jtimberman staff 1679 Jul 8 21:29 validation.pem

Page 74: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef::Config

74

log_level :infolog_location STDOUTchef_server_url 'https://api.opscode.com/organizations/oscon'validation_client_name 'oscon-validator'

http://wiki.opscode.com/display/chef/Chef+Configuration+Settings

Page 75: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Download getting-started cookbook

75

% knife cookbook site vendor getting-startedINFO: Downloading getting-started from the cookbooks site at version 0.2.0...INFO: Cookbook getting-started version 0.2.0 successfully vendored!

Page 76: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 76

Page 77: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 77

git checkout -b \chef-vendor-#{name_args[0]}

Page 78: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Upload getting-started to Chef Server

78

% knife cookbook upload getting-startedINFO: Saving getting-startedINFO: Validating ruby filesINFO: Validating templatesINFO: Syntax OKINFO: Generating MetadataINFO: Uploading files...INFO: Upload complete!

Page 79: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Apply getting-started Recipe to workstation

79

% knife node run list add cider.local "recipe[getting-started]"{ "run_list": [ "recipe[getting-started]" ]}

Page 80: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Run chef-client!

80

% sudo chef-client[Thu, 08 Jul 2010 21:35:49 -0600] INFO: Starting Chef Run[Thu, 08 Jul 2010 21:35:55 -0600] INFO: Writing updated content for template[/tmp/chef-getting-started.txt] to /tmp/chef-getting-started.txt[Thu, 08 Jul 2010 21:35:56 -0600] INFO: Chef Run complete in 6.650602 seconds

% cat /tmp/chef-getting-started.txtWelcome to Chef!

This is Chef version 0.9.6.Running on mac_os_x.Version 10.6.4.

Page 81: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Inside the getting-started cookbook

81

Page 82: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 82http://www.flickr.com/photos/38299630@N05/3635356091/

Page 83: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cooking with Chef

83http://www.flickr.com/photos/mr_t_in_dc/3305638738/

Page 84: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef Resources in Depth

84

cookbook_file

template

service

packagedeploy

git

http_request

link

ruby_block

logbash

execute

remote_file

user

Page 85: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Resources have parameters and actions

85

Page 86: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Resource Parameters and Actions

86

Most resources have defaultsDefaults are sane

‣ http://wiki.opscode.com/display/chef/Resources

Page 87: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

packages

87

package "apache2" do action :installend

Actions: install, upgrade, remove, purge

Page 88: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Package shortcuts

88

Map to providersgem_packagedpkg_packagerpm_packageAnd more!

Page 89: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

services

89

Actions: enable, disable, start, stop, restart, reload

service "apache2" do action [ :enable, :start ]end

Page 90: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

init script capabilities

90

service "apache2" do supports( :status => true, :restart => true, :reload => true ) action [ :enable, :start ]end

Page 91: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

files

91

Actions: create, delete, touch

file "/etc/chef/client.rb" do owner "root" group "root" mode 0644 action :createend

Page 92: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

file content!

92

file "/tmp/example" do content "This is a file!"end

file "/tmp/example2" do content IO.read("/etc/hosts")end

Content is a string

Page 93: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

remote_file

93

Actions: create, create_if_missing

remote_file "/tmp/nginx-0.7.67.tar.gz" do source "http://sysoev.ru/nginx/nginx-0.7.67.tar.gz" action :create_if_missingend

Page 94: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

cookbook_file

94

cookbook_file "/etc/perl/CPAN/Config.pm" do source "Config-5.10.1.pm" owner "root" group "root" mode 0644end

Actions: create, create_if_missing, delete

Page 95: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

template

95

template "/etc/apache2/ports.conf" do source "ports.conf.erb" owner "root" group "root" mode 0644end

Actions: create

Page 96: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

local templates

96

template "/tmp/config.conf" do local true source "/tmp/config.conf.erb"end

Page 97: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

templates are ERB

97

<% node[:apache][:listen_ports].each do |port| -%>Listen <%= port %>NameVirtualHost *:<%= port %>

<% end -%>

Page 98: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Cookbook and Template File Specificity

98

Page 99: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 99

preferences = [ File.join("host-#{fqdn}", "#{file_name}"), File.join("#{platform}-#{version}", "#{file_name}"), File.join("#{platform}", "#{file_name}"), File.join("default", "#{file_name}")]

host-node[:fqdn] node[:platform]-node[:version] node[:platform] default

files/web1prod.example.com files/ubuntu-9.10 files/ubuntu files/default

Page 100: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

execute

100

execute "apt-get update" do action :runend

Actions: run

Page 101: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 101

bash "compile_nginx_source" do cwd "/tmp" code <<-EOH tar zxf nginx-0.7.67.tar.gz cd nginx-0.7.67 && ./configure make && make install EOHend

Interpreters: bash, ruby, python, perl, csh

script

Page 102: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

ruby_block

102

Action: create

ruby_block "save the node" do block do node.save endend

Page 103: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

scm: git...

103

git "/srv/apps/chef" do repository "git://github.com/opscode/chef.git" reference "0.9.6" action :checkoutend

Actions: sync, checkout, export

Page 104: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 104

...and subversionsubversion "/srv/couchdb" do repository "http://svn.apache.org/repos/asf/couchdb/trunk" revision "HEAD" action :syncend

Actions: sync, checkout, export

Page 105: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

deploy

105

http://wiki.opscode.com/display/chef/Deploy+Resource

Actions: deploy, force_deploy, rollback

Page 106: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 106

deploy "/srv/radiant" do repo "git://github.com/radiant/radiant.git" revision "HEAD" user "railsdev" migrate true migration_command "rake db:migrate" environment "production" restart_command "touch tmp/restart.txt" action :deployend

Page 107: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Meta-parameter madness!

107

Page 108: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

action :nothing

108

Page 109: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

not_if & only_if

109

Page 110: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 110

execute "runit-hup-init" do command "telinit q" only_if "grep ^SV /etc/inittab" action :nothingend

execute "rabbitmqctl add_vhost /chef" do not_if "rabbitmqctl list_vhosts| grep /chef"end

Enclose in quotes for shell commands or use a do..end or { } style ruby block

Page 111: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 111

Resource notificationtemplate "nginx.conf" do path "/etc/nginx/nginx.conf" source "nginx.conf.erb" owner "root" group "root" mode "0644" notifies :restart, resources(:service => "nginx")end

Page 112: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

supports

112

Page 113: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Anatomy of a Chef Run

113

Page 114: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Anatomy of a Chef Run

114

Node discoverySet the node nameRegister with server

Page 115: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Anatomy of a Chef Run

Build node object‣ node.save

Synchronize cookbooks‣ node.save

Converge‣ node.save

115

Page 116: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef Run Convergence

CompileExecute

116

Page 117: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Development workflow with Chef

117http://www.flickr.com/photos/kylemay/1393258810/

Page 118: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Development workflow

Gather requirementsWrite recipesCommit to repositoryRun Chef in testingRun Chef in production

118

Page 119: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 119http://www.flickr.com/photos/38299630@N05/3635356091/

Page 120: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Automating the Cloud with Chef

120http://www.flickr.com/photos/46183897@N00/3442880227/sizes/l/

Page 121: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 121

GoTime

Page 122: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Add your Cloud credentials to knife.rb

122

vi ~/chef-repo/.chef/knife.rb

# Cloud credentialsknife[:aws_access_key_id] = ENV['AWS_ACCESS_KEY_ID']knife[:aws_secret_access_key] = ENV['AWS_SECRET_ACCESS_KEY']

Page 123: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 123

Download some cookbooks

Uses the “vendor branch” pattern, so you can make changes and track the upstream

% knife cookbook site vendor nagios -dINFO: Downloading nagios from the cookbooks site at version 0.3.3...INFO: Cookbook nagios version 0.3.3 successfully vendored!INFO: Downloading apache2 from the cookbooks site at version 0.12.0INFO: Cookbook apache2 version 0.12.0 successfully vendored!

Page 124: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 124

Upload Cookbooks!

knife cookbook upload -a

These run as root, kids.Let’s not blindly trust the upstream too much!

Page 125: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 125

Build some roles

% vi roles/monitoring.rb

name "monitoring"description "Nagios monitoring server"

run_list( "role[base]”, “recipe[nagios::server]")

override_attributes( "apache" => { "allowed_openids" => "http://jtimberman.myopenid.com/" })

Page 126: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved 126

Upload Roles

% knife role from file roles/monitoring.rbWARN: HTTP Request Returned 404 Not Found: Cannot load role monitoringWARN: Updated Role monitoring!% ls rolesREADME base.rb monitoring.rb production.rb webserver.rb% rake roles(in /Users/jtimberman/Development/oscon/chef-repo)WARN: HTTP Request Returned 404 Not Found: Cannot load role baseWARN: Updated Role base!WARN: Updated Role monitoring!WARN: HTTP Request Returned 404 Not Found: Cannot load role productionWARN: Updated Role production!WARN: HTTP Request Returned 404 Not Found: Cannot load role webserverWARN: Updated Role webserver!

Page 127: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Launch a new Monitoring Server

127

knife ec2 server create ‘role[monitoring]’

Page 128: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Chef runs on your new server

128

sudo chef-client

INFO: Starting Chef Run...INFO: Chef Run complete in 211.852033 seconds

Automatically.

Page 129: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

Shef is Chef in IRB

129

Page 130: Automated infrastructure is on the menu

Copyright © 2010 Opscode, Inc - All Rights Reserved

www.opscode.comIRC and Mailing lists

‣ irc.freenode.net #chef‣ lists.opscode.com

Twitter:‣ @opscode, #opschef‣ @jtimberman, @metaxis

Questions?http://bit.ly/chef-oscon2010

Resources/Questions

130