Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automating your Infrastructure with Chef

Avatar for Jennifer Davis Jennifer Davis
September 19, 2014

Automating your Infrastructure with Chef

Interested in speeding up time to production when developing an application? Want to understand how to minimize risk associated with changes? Come learn about infrastructure automation with Chef. In this beginner level workshop, I will teach you the core set of skills needed to implement Chef in your environment whether for work or personal projects. I will cover the basic architecture of Chef and the associated tools that will help you improve your application workflow from design to production.

Avatar for Jennifer Davis

Jennifer Davis

September 19, 2014
Tweet

More Decks by Jennifer Davis

Other Decks in Technology

Transcript

  1. Twitter: @sigje Email: sigje@getchef.com Introductions Name Current Job Role Previous

    Job Experience Experience with Infrastructure Automation Favorite Text Editor 4
  2. 6

  3. Twitter: @sigje Email: sigje@getchef.com Application of Learning Knowledge of your

    environment, business needs Chef - Framework to solve infrastructure automation 9
  4. Twitter: @sigje Email: sigje@getchef.com Workshop Fundamentals Objectives at the beginning.

    Ask questions. Ask for help. Answer questions. Slides will be available after the class. 12
  5. Twitter: @sigje Email: sigje@getchef.com Setting Expectations Experience Level: Beginner Flexible

    content based on class It’s OK to leave (and come back). Minimize disruptions. 13
  6. Twitter: @sigje Email: sigje@getchef.com Agenda Overview of Infrastructure Automation Intro

    to Resources, Recipes, Cookbooks Working with the Node Object Introduction to Community Cookbooks 15
  7. Twitter: @sigje Email: sigje@getchef.com Objectives: Overview of Infrastructure Automation Describe

    Infrastructure Automation. Describe how Chef treats Infrastructure Automation. Define Node. 17
  8. Twitter: @sigje Email: sigje@getchef.com Complexity in Applications 22 Single service

    == multiple applications. Single application == a component of service.
  9. Twitter: @sigje Email: sigje@getchef.com Complexity in Configurations 24 Multiple services

    using same software with different configurations. Different Operating Systems requiring same software (and user experience).
  10. Twitter: @sigje Email: sigje@getchef.com Complexity in Access Control 26 Different

    users access to different systems. External versus internal. Compliance.
  11. Twitter: @sigje Email: sigje@getchef.com Servers 28 physical hardware cloud services

    AWS Digital Ocean virtual VMWare OpenStack VirtualBox
  12. Twitter: @sigje Email: sigje@getchef.com Automation 34 automaton acting of one’s

    own will Antikythera mechanism 150-100BC calculate positions of astronomical objects "NAMA Machine d'Anticythère 1". Licensed under Creative Commons Attribution 2.5 via Wikimedia Commons - http://commons.wikimedia.org/wiki/
  13. Infrastructure Automation is creating control systems that reduce the burden

    on people to manage services and increase the quality, accuracy and precision of a service to the consumers of the service. 37
  14. Twitter: @sigje Email: sigje@getchef.com Infrastructure as Code 41 Reconstruct business

    from code repository, data backups, and compute resources.
  15. Twitter: @sigje Email: sigje@getchef.com Chef Tenets of Infrastructure as Code

    42 1. Programmatically provision and configure components. 2. Treat infrastructure code like the rest of the code. 3. Reconstruct business from code repository, data backups, and compute resources.
  16. Twitter: @sigje Email: sigje@getchef.com Nodes 44 any physical, virtual, or

    cloud machine that is configured to be maintained by a chef-client.
  17. Twitter: @sigje Email: sigje@getchef.com Physical Node 45 server virtual machine

    any active device attached to a network that can run a chef-client.
  18. Twitter: @sigje Email: sigje@getchef.com Network Node 46 any active network

    device attached to a network that can run a chef-client.
  19. Twitter: @sigje Email: sigje@getchef.com Cloud Node 47 Amazon Virtual Private

    Cloud OpenStack Rackspace Google Compute Engine Linode Microsoft Azure
  20. Twitter: @sigje Email: sigje@getchef.com Virtual Node 48 a machine that

    runs only as a software implementation, but otherwise behaves much like a physical machine.
  21. Twitter: @sigje Email: sigje@getchef.com Containers 49 a single operating system

    to host many working configurations, with each working configuration running as a process that is isolated from all other processes, including even the host operating system
  22. Twitter: @sigje Email: sigje@getchef.com hostname vs node name 51 host

    name - network addressable label assigned to a server node name - unique identifier RECOMMENDATION: hostname == node name
  23. Twitter: @sigje Email: sigje@getchef.com Lesson 1: Set up your cloud

    node http://opscode- cheflab.herokuapp.com/labs/ chefdk/centos/attend/ Fill in the form! 52
  24. Twitter: @sigje Email: sigje@getchef.com Lesson 1: Set up your cloud

    node http://opscode- cheflab.herokuapp.com/labs/ chefdk/centos/attend/ Click on “Create user” 53
  25. Twitter: @sigje Email: sigje@getchef.com Lesson 1: Set up your cloud

    node ! Wait until you see “Environment is Ready” Click on “Start Using This Environment” 54
  26. Twitter: @sigje Email: sigje@getchef.com Lesson 1: Set up your cloud

    node 55 ! SUCCESS! Server has been created.
  27. Twitter: @sigje Email: sigje@getchef.com Lesson 2: Identify your cloud node

    ! Click on “show password” Password will be in the clear. Note it down. 59
  28. Twitter: @sigje Email: sigje@getchef.com Lesson 3: Connect to your cloud

    node ! 2 choices. ssh available? GUI (may be a little slower) 60
  29. Twitter: @sigje Email: sigje@getchef.com Lesson 3: Connect to your cloud

    node with SSH ! Remember this is YOUR hostname Remember this is YOUR root password ssh $HOSTNAME -l root 61
  30. Twitter: @sigje Email: sigje@getchef.com Lesson 3: Connect to your cloud

    node with SSH ! at command prompt type cd chef-repo 62
  31. Twitter: @sigje Email: sigje@getchef.com Lesson 3: Connect to your cloud

    node with GUI ! Click anywhere in the white box. 64
  32. Twitter: @sigje Email: sigje@getchef.com Chef Development Kit chef Berkshelf 3.0

    Test Kitchen ChefSpec Foodcritic chef-client knife ohai Chef Zero 66
  33. Twitter: @sigje Email: sigje@getchef.com Verify Installation $ chef verify verifies

    that all applications are installed as appropriate 68
  34. Twitter: @sigje Email: sigje@getchef.com Objectives: Intro to Resources, Recipes, Cookbooks

    Define resources. Explain what a recipe is. Define cookbooks. 70
  35. Twitter: @sigje Email: sigje@getchef.com Infrastructure Elements files directories symlinks mounts

    users groups software packages external services filesystems 75
  36. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources Open up a browser window to http://docs.getchef.com/ 78
  37. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources Click on “Search the Docs” 79
  38. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources Click on “Search the Docs” 80
  39. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources enter the word resources into the search box 81
  40. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources click on “Resources and Providers Reference - Chef Single-page Topics” 82
  41. Twitter: @sigje Email: sigje@getchef.com Lesson 5: Use docs to find

    info about resources Scroll down the “Table of Contents” on the right hand side Find and click on “Resources” 83
  42. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    GUI users: if the node isn’t available, click on “Reconnect” 85
  43. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. SSH users vi hello.rb GUI vi hello.rb or gedit hello.rb & 86
  44. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. GUI gedit users Opens a graphical window Click on the window to type in it. 87
  45. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. add content. GUI gedit users Click on “File” 90
  46. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. add content. GUI gedit users Click on “Save” 91
  47. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. add content. SSH and GUI - vi users type “i” to go into insert mode enter text 92
  48. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. add content. vi users Click “esc” key (Command mode) Enter “:x” and click return 93
  49. Twitter: @sigje Email: sigje@getchef.com Lesson 6: Create a file resource.

    create a file named hello.rb. add content. save the file. 94
  50. Twitter: @sigje Email: sigje@getchef.com Lesson 8: Verify that the file

    was created. At command prompt type: cat motd 96
  51. Twitter: @sigje Email: sigje@getchef.com Lesson 10: Modify the resource. Edit

    your resource to make your message personal! Edit the file. Modify ‘hello world’. 98
  52. Twitter: @sigje Email: sigje@getchef.com Lesson 13: Modify the file by

    hand. Edit the motd file to have a new message. 102
  53. Twitter: @sigje Email: sigje@getchef.com Lesson 15: Delete a file resource.

    create a file named goodbye.rb. add content. file ‘motd’ do action :delete end save file. 106
  54. Twitter: @sigje Email: sigje@getchef.com Lesson 16: Verify the file was

    deleted. 110 At command prompt type: cat motd
  55. A resource represents an element or component of the system.

    A resource describes what the element should be not how to achieve that state of being. 111
  56. Twitter: @sigje Email: sigje@getchef.com Review Created hello.rb and goodbye.rb Created

    file resource. Ran chef commands to create and update resource. 112
  57. Twitter: @sigje Email: sigje@getchef.com There are a number of different

    resources that are defined in the documentation. package "apache2" Review 114 Package Resource
  58. Twitter: @sigje Email: sigje@getchef.com There are a number of different

    resources that are defined in the documentation. Review 115 Template Resource template "/etc/apache2/apache2.conf" do source "apache2.conf.erb" owner "root" group "root" mode "0644" variables(:allow_override => "All") notifies :reload, "service[apache2]" end
  59. Twitter: @sigje Email: sigje@getchef.com There are a number of different

    resources that are defined in the documentation. Review 116 service "apache2" do action [:enable,:start] supports :reload => true end Service Resource
  60. Twitter: @sigje Email: sigje@getchef.com goodbye recipe should contain all the

    resources required to clean up any resources we created for hello. Review 122
  61. Twitter: @sigje Email: sigje@getchef.com Lesson 17: Create a webserver recipe.

    create a file named webserver.rb. add content. save file. 124
  62. Twitter: @sigje Email: sigje@getchef.com Lesson 17: Create a webserver recipe.

    create a file named webserver.rb. add content. package resource install apache server save file. 125 package 'httpd'
  63. Twitter: @sigje Email: sigje@getchef.com Lesson 18: Apply the resource. 127

    At command prompt type: chef-apply webserver.rb
  64. Twitter: @sigje Email: sigje@getchef.com Lesson 18: Apply the resource. 128

    At command prompt type: chef-apply webserver.rb
  65. Twitter: @sigje Email: sigje@getchef.com Lesson 20: Modify recipe to start

    Apache. edit webserver.rb. add content. service resource starts and enables httpd service save file. 130 package ‘httpd’ ! service 'httpd' do action [:start, :enable] end
  66. Twitter: @sigje Email: sigje@getchef.com Lesson 21: Apply the resource. 132

    At command prompt type: chef-apply webserver.rb
  67. Twitter: @sigje Email: sigje@getchef.com Review 133 We created a web

    server recipe to install, start, and enable Apache on boot.
  68. Twitter: @sigje Email: sigje@getchef.com Lesson 22: Verify webserver running. Open

    a browser. 134 Click on the Firefox Icon to open the browser.
  69. Twitter: @sigje Email: sigje@getchef.com Lesson 22: Verify webserver running. Open

    a browser. localhost in the URL field. 135 localhost
  70. Twitter: @sigje Email: sigje@getchef.com Lesson 23: Modify recipe to add

    index.html. edit webserver.rb. add content. service file save file. 136 file '/var/www/html/index.html' do content '<html> <body> <h1>hello world</h1> </body> </html>' end
  71. Twitter: @sigje Email: sigje@getchef.com Lesson 24: Apply the resource. 138

    At command prompt type: chef-apply webserver.rb
  72. Twitter: @sigje Email: sigje@getchef.com Lesson 25: Reload webpage. Open a

    browser. Shift + Click on reload. 139 Shift +Click
  73. Twitter: @sigje Email: sigje@getchef.com Review 140 We created a personalized

    index.html. HTML code is embedded in the recipe, our infrastructure code.
  74. Twitter: @sigje Email: sigje@getchef.com Lesson 26: Create a working directory.

    142 At command prompt type: mkdir cookbooks cd cookbooks
  75. Twitter: @sigje Email: sigje@getchef.com Lesson 27: Create a webserver cookbook.

    145 At command prompt type: chef generate cookbook webserver
  76. Twitter: @sigje Email: sigje@getchef.com Lesson 29: Create template file. 147

    At command prompt type: chef generate template webserver index.html
  77. Twitter: @sigje Email: sigje@getchef.com Lesson 29: Edit index.html.erb file. 149

    edit index.html.erb EDITOR webserver/templates/default/ index.html.erb add content. save file. <html> <body> <h1>hello world</h1> </body> </html>
  78. Twitter: @sigje Email: sigje@getchef.com Lesson 30: Edit default recipe. 150

    edit default.rb EDITOR webserver/recipes/default.rb add content. package, service, template resources save file. package 'httpd' ! service 'httpd' do action [:start, :enable] end ! template '/var/www/html/index.html' do source 'index.html.erb' end
  79. Twitter: @sigje Email: sigje@getchef.com Lesson 30: Edit default recipe. 151

    edit default.rb EDITOR webserver/recipes/default.rb add content. package, service, template resources save file. package 'httpd' ! service 'httpd' do action [:start, :enable] end ! template '/var/www/html/index.html' do source 'index.html.erb' end
  80. Twitter: @sigje Email: sigje@getchef.com Lesson 31: Use our cookbook. 154

    At command prompt type: chef-client --local-mode --runlist webserver
  81. Twitter: @sigje Email: sigje@getchef.com Lesson 32: Reload webpage. Open a

    browser. Shift + Click on reload. 155 Shift +Click
  82. Twitter: @sigje Email: sigje@getchef.com Review 157 chef-apply - single recipe

    chef-client - multiple recipes local-mode — not using chef server runlist — specify the ordered set of recipes to run.
  83. Twitter: @sigje Email: sigje@getchef.com Objectives: Working with the Node Object

    Explain what the node object is in Chef. Show details about a node. Describe node attributes. 160
  84. Twitter: @sigje Email: sigje@getchef.com Node Object 161 A node is

    any physical, virtual, or cloud machine that is configured to be maintained by a chef-client. The node object is representation of that node within chef (e.g. in JSON). Available when you are writing recipes.
  85. Twitter: @sigje Email: sigje@getchef.com Lesson 33: Set PATH variable. 162

    At command prompt type: export PATH=$PATH:/opt/chef/embedded/bin:/opt/chefdk/embedded/bin
  86. Twitter: @sigje Email: sigje@getchef.com Lesson 34: Run Ohai 163 At

    command prompt type: ohai To page through the output: ohai | less
  87. Twitter: @sigje Email: sigje@getchef.com Node Attributes 165 Ability to programmatically

    add additional attributes in recipes and cookbooks node.default["apache"]["dir"] = "/etc/apache2" attribute name attribute value
  88. Twitter: @sigje Email: sigje@getchef.com Lesson 35: Create the attribute file.

    At command prompt type: chef generate attribute webserver default 166
  89. Twitter: @sigje Email: sigje@getchef.com Lesson 37: Edit the attribute file.

    168 edit default.rb EDITOR webserver/attributes/default.rb add content. save file. default["apache"]["indexfile"] = "index.html"
  90. Twitter: @sigje Email: sigje@getchef.com Objectives: Intro to Community Cookbooks Find,

    preview, download cookbooks from the community. Use knife to work with the Supermarket API. Download, extract, examine, and implement cookbooks from the Supermarket. 170
  91. Twitter: @sigje Email: sigje@getchef.com Lesson 40: Search Supermarket from command

    line 181 At command prompt type: knife cookbook site search chef-client
  92. Twitter: @sigje Email: sigje@getchef.com Lesson 40: Search Supermarket from command

    line 182 At command prompt type: knife cookbook site show chef-client
  93. Twitter: @sigje Email: sigje@getchef.com Lesson 40: Search Supermarket from command

    line 183 At command prompt type: knife cookbook site show chef-client
  94. Twitter: @sigje Email: sigje@getchef.com Lesson 41: Download chef-client cookbook. 184

    At command prompt type: knife cookbook site download chef-client
  95. Twitter: @sigje Email: sigje@getchef.com Lesson 42: Extract chef-client cookbook. 187

    At command prompt type: tar zxvf chef-client-3.8.0.tar.gz
  96. Twitter: @sigje Email: sigje@getchef.com Lesson 42: Read the chef-client cookbook.

    189 Examine the README.md Expectations of the cookbook, platform, data Recipes Usage Notes
  97. Twitter: @sigje Email: sigje@getchef.com Lesson 43: Use the chef-client cookbook.

    190 At command prompt type: chef-client --local-mode --runlist webserver,chef-client This will fail because we haven’t set up an org in hosted chef.
  98. Twitter: @sigje Email: sigje@getchef.com Review Overview of Infrastructure Automation Intro

    to Resources, Recipes, Cookbooks Working with the Node Object Introduction to Community Cookbooks 192
  99. Twitter: @sigje Email: sigje@getchef.com Resources: O’Reilly Books Learning Chef -

    Mischa Taylor and Seth Vargo Experience Level: Beginner to Chef Customizing Chef - Jon Cowie Experience Level: Intermediate to Chef Sekret code: (ONLY AVAILABLE TO ATTENDEES) 50% print discount, 40% ebook discount 194
  100. Twitter: @sigje Email: sigje@getchef.com Additional Resources 195 Fundamentals Training, Intermediate

    Training Learn Chef Fundamentals videos Google Forum Sign up for Virtual Office Hours
  101. Twitter: @sigje Email: sigje@getchef.com THANK YOU Shirts Knife Reference Guides

    Give me your business card (or email me contact details) and I’ll send you DRAFT version of Learning Chef ebook (potential spelling mistakes..) 196