$30 off During Our Annual Pro Sale. View Details »

The Joy of Cooking: Whip up a Rails Environment with Chef

The Joy of Cooking: Whip up a Rails Environment with Chef

You've heard of Chef, Puppet, and other frameworks that can help you build out your infrastructure. You've been meaning to play around with one or more of them for some time now. Now's your chance; Start cooking up on your own servers!

In this tutorial, we'll provide an introduction to Chef with a focus on what you'll need to know to get a Rails application up and running.

Topics include:

* Introduction to Chef
* Provisioning an environment for a Rails application
* Deploying with Capistrano

You won't be ready to compete in Iron Chef, but you will be ready to serve up your own Rails environment in no time.

Nathen Harvey

April 03, 2012
Tweet

More Decks by Nathen Harvey

Other Decks in Technology

Transcript

  1. The Joy of Cooking Whip up a Rails Environment with

    Chef Nathen Harvey, CustomInk.com @nathenharvey https://github.com/nathenharvey/cooking-with-chef @nathenharvey 1
  2. Agenda •  Infrastructure as Code •  Introduction to Chef • 

    Building a project in Chef •  Provision a server for your Rails App •  Additional resources @nathenharvey 2
  3. Infrastructure as Code •  Enable the reconstruction of the business

    from nothing but –  a source code repository –  an application data backup –  and bare metal resources –  Jesse Robins, Opscode @nathenharvey 3
  4. Disposable Servers @nathenharvey 4

  5. Evolution of Server Provisioning •  Just build it •  Keep

    notes in server.txt •  Migrate notes to wiki •  Custom shell scripts (in git) •  Systems integration framework @nathenharvey 5
  6. When should I use a systems integration framework? •  After

    you outgrow Heroku @nathenharvey 6
  7. Which framework? •  CFEngine? •  Puppet? •  Chef? @nathenharvey 7

  8. Wrong question! •  YES – use a systems integration framework

    •  YES – use one that works for your team •  YES – this is a talk about Chef @nathenharvey 8
  9. Chef •  Declarative – What, not how •  Idempotent –

    Only take action if required •  Convergent – Takes care of itself @nathenharvey 9
  10. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec @nathenharvey 10
  11. Resources @nathenharvey 11

  12. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec •  Write recipes @nathenharvey 12
  13. Recipes @nathenharvey 13

  14. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec •  Write recipes •  Package recipes in cookbooks @nathenharvey 14
  15. Cookbooks @nathenharvey 15

  16. Cookbooks @nathenharvey 16

  17. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec •  Write recipes •  Package recipes in cookbooks •  Apply recipes to nodes @nathenharvey 17
  18. Nodes •  Representation of a host –  runs the Chef

    client –  has attributes –  has a list of recipes to be applied @nathenharvey 18
  19. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec •  Write recipes •  Package recipes in cookbooks •  Apply recipes to nodes •  Group things into roles @nathenharvey 19
  20. Roles •  mechanism for easily composing sets of functionality • 

    have attributes and a list of recipes to be applied @nathenharvey 20
  21. Roles @nathenharvey 21

  22. Building a Chef Project •  First, come up with your

    policy / specification •  Abstract the resources in your spec •  Write recipes •  Package recipes in cookbooks •  Apply recipes to nodes •  Group things into roles @nathenharvey 22
  23. What is Chef? @nathenharvey 23

  24. What is Chef? •  Server - API, search, Web UI

    •  Client - chef-client •  Command line tool - knife @nathenharvey 24
  25. knife @nathenharvey 25

  26. What is Chef? •  Server - API, search, Web UI

    •  Client - chef-client •  Command line tool - knife •  Inspection library - ohai @nathenharvey 26
  27. ohai •  Collects detailed, extensible information about a host. @nathenharvey

    27
  28. What is Chef? •  Server - API, search, Web UI

    •  Client - chef-client •  Command line tool - knife •  Inspection library - ohai •  REPL - shef @nathenharvey 28
  29. shef •  The (poorly named) Chef REPL @nathenharvey 29

  30. shef @nathenharvey 30

  31. shef @nathenharvey 31

  32. shef @nathenharvey 32

  33. shef @nathenharvey 33

  34. What is Chef? •  Server - API, search, Web UI

    •  Client - chef-client •  Command line tool - knife •  Inspection library - ohai •  REPL - shef •  Community @nathenharvey 34
  35. community.opscode.com @nathenharvey 35

  36. Community Site •  Publish and share cookbooks @nathenharvey 36

  37. Community Site •  Publish and share plugins for Chef, Knife,

    and Ohai @nathenharvey 37
  38. Chef Deployment Options •  chef-solo •  Chef Server •  Hosted

    Chef •  Private Chef @nathenharvey 38
  39. chef-solo •  What you don't get: –  central server – 

    authentication –  authorization –  search indexes –  persistent attributes @nathenharvey 39
  40. chef-solo •  Execute cookbooks that are stored on disk or

    available at at URL @nathenharvey 40
  41. Chef Server •  Open source •  Run it yourself, wherever

    you like •  Complicated to set-up and manage @nathenharvey 41
  42. Hosted Chef •  Best way to get started @nathenharvey 42

  43. Private Chef •  Commercial offering •  Managed by Opscode • 

    Pricing –  installation fee –  service contract @nathenharvey 43
  44. Get started with Hosted Chef 1.  Create a Hosted Chef

    account 2.  Install and Update dependencies - ruby, ruby gems, ruby-dev and git-core 3.  Install Chef and create directories needed 4.  Connect to Hosted Chef @nathenharvey 44
  45. Client configuration •  Get organization validation key •  Generate knife

    config •  Get a private key •  Set-up chef-repo directory •  Copy validation files and knife config to .chef @nathenharvey 45
  46. chef-repo directory @nathenharvey 46

  47. Verify setup @nathenharvey 47

  48. Provision a server •  knife ec2 server create •  knife

    rackspace server create •  Vagrant @nathenharvey 48
  49. Vagrantfile @nathenharvey 49

  50. Launch Vagrant & Check In @nathenharvey 50

  51. Initial set-up steps •  Register with hosted chef •  Create

    a chef-repo •  Install chef •  Configure knife.rb •  Configure Vagrant file •  Register Vagrant instance with Chef hosted @nathenharvey 51
  52. Provision for Rails •  Apache •  Passenger •  MySQL • 

    Rails application @nathenharvey 52
  53. Add passenger_apache2 cookbook @nathenharvey 53

  54. Cookbook site install 1.  A new "pristine copy" branch is

    created in git for tracking the upstream 2.  All existing cookbooks are removed from the branch 3.  The cookbook is downloaded from the cookbook site in tarball form 4.  The downloaded cookbook is untarred, and its contents commited via git 5.  The pristine copy branch is merged into the master branch @nathenharvey 54
  55. Add mysql cookbook @nathenharvey 55

  56. Create a Cookbook @nathenharvey 56

  57. Write our recipes •  default.rb •  web.rb •  db.rb @nathenharvey

    57
  58. Web Recipe •  Set-up some directories @nathenharvey 58

  59. Web Recipe •  Configure Apache / Passenger @nathenharvey 59

  60. Database Recipe •  Create the database @nathenharvey 60

  61. Database Recipe •  Create the database user @nathenharvey 61

  62. Upload cookbooks to Chef server @nathenharvey 62

  63. Create some roles •  Group recipes together using roles • 

    Apply roles to nodes •  Our roles: –  base_ubuntu –  rubynation_web –  rubynation_db @nathenharvey 63
  64. base_ubuntu Role @nathenharvey 64

  65. rubynation_web Role @nathenharvey 65

  66. rubynation_db Role @nathenharvey 66

  67. Upload the roles to the server @nathenharvey 67

  68. Assign the roles to our nodes @nathenharvey 68

  69. Run chef-client •  Automatically •  knife ssh •  vagrant provision

    @nathenharvey 69
  70. Review •  Server provisioned and communicating with the Chef API

    •  Apache and Passenger installed with a default configuration •  MySQL installed and running @nathenharvey 70
  71. Deploying with Capistrano •  Without Chef: @nathenharvey 71

  72. Deploying with Capistrano •  With Chef search @nathenharvey 72

  73. But wait, there's more! •  Encrypted databags •  Environments • 

    Lightweight Resources and Providers (LWRP) •  Exception and report handlers @nathenharvey 73
  74. Want more? •  http://community.opscode.com •  http://wiki.opscode.com •  Opscode Training Materials

    •  #chef on irc.freenode.net @nathenharvey 74
  75. Want even more? •  http://foodfightshow.org –  Episode 5: Getting Started

    with Chef •  ChefConf - May 15-17 in San Francisco @nathenharvey 75
  76. Shameless Plugs •  DevOpsDC •  Washington DC MongoDB Users Group

    •  CustomInk Friday Tech Lunches @nathenharvey 76
  77. Find Me •  @nathenharvey •  http://nathenharvey.com •  nharvey@customink.com @nathenharvey 77