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

Automating your Infrastructure with Chef

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.

Jennifer Davis

September 19, 2014
Tweet

More Decks by Jennifer Davis

Other Decks in Technology

Transcript

  1. Twitter: @sigje Email: [email protected] Introductions Name Current Job Role Previous

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

  3. Twitter: @sigje Email: [email protected] Application of Learning Knowledge of your

    environment, business needs Chef - Framework to solve infrastructure automation 9
  4. Twitter: @sigje Email: [email protected] 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: [email protected] 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: [email protected] Agenda Overview of Infrastructure Automation Intro

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

    Infrastructure Automation. Describe how Chef treats Infrastructure Automation. Define Node. 17
  8. Twitter: @sigje Email: [email protected] Complexity in Applications 22 Single service

    == multiple applications. Single application == a component of service.
  9. Twitter: @sigje Email: [email protected] 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: [email protected] Complexity in Access Control 26 Different

    users access to different systems. External versus internal. Compliance.
  11. Twitter: @sigje Email: [email protected] 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/
  12. 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
  13. Twitter: @sigje Email: [email protected] Infrastructure as Code 41 Reconstruct business

    from code repository, data backups, and compute resources.
  14. Twitter: @sigje Email: [email protected] 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.
  15. Twitter: @sigje Email: [email protected] Nodes 44 any physical, virtual, or

    cloud machine that is configured to be maintained by a chef-client.
  16. Twitter: @sigje Email: [email protected] Physical Node 45 server virtual machine

    any active device attached to a network that can run a chef-client.
  17. Twitter: @sigje Email: [email protected] Network Node 46 any active network

    device attached to a network that can run a chef-client.
  18. Twitter: @sigje Email: [email protected] Cloud Node 47 Amazon Virtual Private

    Cloud OpenStack Rackspace Google Compute Engine Linode Microsoft Azure
  19. Twitter: @sigje Email: [email protected] Virtual Node 48 a machine that

    runs only as a software implementation, but otherwise behaves much like a physical machine.
  20. Twitter: @sigje Email: [email protected] 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
  21. Twitter: @sigje Email: [email protected] hostname vs node name 51 host

    name - network addressable label assigned to a server node name - unique identifier RECOMMENDATION: hostname == node name
  22. Twitter: @sigje Email: [email protected] Lesson 1: Set up your cloud

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

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

    node ! Wait until you see “Environment is Ready” Click on “Start Using This Environment” 54
  25. Twitter: @sigje Email: [email protected] Lesson 1: Set up your cloud

    node 55 ! SUCCESS! Server has been created.
  26. Twitter: @sigje Email: [email protected] Lesson 2: Identify your cloud node

    ! Click on “show password” Password will be in the clear. Note it down. 59
  27. Twitter: @sigje Email: [email protected] Lesson 3: Connect to your cloud

    node ! 2 choices. ssh available? GUI (may be a little slower) 60
  28. Twitter: @sigje Email: [email protected] 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
  29. Twitter: @sigje Email: [email protected] Lesson 3: Connect to your cloud

    node with SSH ! at command prompt type cd chef-repo 62
  30. Twitter: @sigje Email: [email protected] Lesson 3: Connect to your cloud

    node with GUI ! Click anywhere in the white box. 64
  31. Twitter: @sigje Email: [email protected] Chef Development Kit chef Berkshelf 3.0

    Test Kitchen ChefSpec Foodcritic chef-client knife ohai Chef Zero 66
  32. Twitter: @sigje Email: [email protected] Objectives: Intro to Resources, Recipes, Cookbooks

    Define resources. Explain what a recipe is. Define cookbooks. 70
  33. Twitter: @sigje Email: [email protected] Infrastructure Elements files directories symlinks mounts

    users groups software packages external services filesystems 75
  34. Twitter: @sigje Email: [email protected] Lesson 5: Use docs to find

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

    info about resources Click on “Search the Docs” 79
  36. Twitter: @sigje Email: [email protected] Lesson 5: Use docs to find

    info about resources Click on “Search the Docs” 80
  37. Twitter: @sigje Email: [email protected] Lesson 5: Use docs to find

    info about resources enter the word resources into the search box 81
  38. Twitter: @sigje Email: [email protected] Lesson 5: Use docs to find

    info about resources click on “Resources and Providers Reference - Chef Single-page Topics” 82
  39. Twitter: @sigje Email: [email protected] 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
  40. Twitter: @sigje Email: [email protected] Lesson 6: Create a file resource.

    GUI users: if the node isn’t available, click on “Reconnect” 85
  41. Twitter: @sigje Email: [email protected] 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
  42. Twitter: @sigje Email: [email protected] 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
  43. Twitter: @sigje Email: [email protected] Lesson 6: Create a file resource.

    create a file named hello.rb. add content. GUI gedit users Click on “File” 90
  44. Twitter: @sigje Email: [email protected] Lesson 6: Create a file resource.

    create a file named hello.rb. add content. GUI gedit users Click on “Save” 91
  45. Twitter: @sigje Email: [email protected] 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
  46. Twitter: @sigje Email: [email protected] 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
  47. Twitter: @sigje Email: [email protected] Lesson 6: Create a file resource.

    create a file named hello.rb. add content. save the file. 94
  48. Twitter: @sigje Email: [email protected] Lesson 8: Verify that the file

    was created. At command prompt type: cat motd 96
  49. Twitter: @sigje Email: [email protected] Lesson 10: Modify the resource. Edit

    your resource to make your message personal! Edit the file. Modify ‘hello world’. 98
  50. Twitter: @sigje Email: [email protected] Lesson 13: Modify the file by

    hand. Edit the motd file to have a new message. 102
  51. Twitter: @sigje Email: [email protected] Lesson 15: Delete a file resource.

    create a file named goodbye.rb. add content. file ‘motd’ do action :delete end save file. 106
  52. Twitter: @sigje Email: [email protected] Lesson 16: Verify the file was

    deleted. 110 At command prompt type: cat motd
  53. 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
  54. Twitter: @sigje Email: [email protected] Review Created hello.rb and goodbye.rb Created

    file resource. Ran chef commands to create and update resource. 112
  55. Twitter: @sigje Email: [email protected] There are a number of different

    resources that are defined in the documentation. package "apache2" Review 114 Package Resource
  56. Twitter: @sigje Email: [email protected] 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
  57. Twitter: @sigje Email: [email protected] 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
  58. Twitter: @sigje Email: [email protected] goodbye recipe should contain all the

    resources required to clean up any resources we created for hello. Review 122
  59. Twitter: @sigje Email: [email protected] Lesson 17: Create a webserver recipe.

    create a file named webserver.rb. add content. save file. 124
  60. Twitter: @sigje Email: [email protected] Lesson 17: Create a webserver recipe.

    create a file named webserver.rb. add content. package resource install apache server save file. 125 package 'httpd'
  61. Twitter: @sigje Email: [email protected] 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
  62. Twitter: @sigje Email: [email protected] Review 133 We created a web

    server recipe to install, start, and enable Apache on boot.
  63. Twitter: @sigje Email: [email protected] Lesson 22: Verify webserver running. Open

    a browser. 134 Click on the Firefox Icon to open the browser.
  64. Twitter: @sigje Email: [email protected] Lesson 22: Verify webserver running. Open

    a browser. localhost in the URL field. 135 localhost
  65. Twitter: @sigje Email: [email protected] 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
  66. Twitter: @sigje Email: [email protected] Lesson 25: Reload webpage. Open a

    browser. Shift + Click on reload. 139 Shift +Click
  67. Twitter: @sigje Email: [email protected] Review 140 We created a personalized

    index.html. HTML code is embedded in the recipe, our infrastructure code.
  68. Twitter: @sigje Email: [email protected] Lesson 26: Create a working directory.

    142 At command prompt type: mkdir cookbooks cd cookbooks
  69. Twitter: @sigje Email: [email protected] Lesson 27: Create a webserver cookbook.

    145 At command prompt type: chef generate cookbook webserver
  70. Twitter: @sigje Email: [email protected] Lesson 29: Create template file. 147

    At command prompt type: chef generate template webserver index.html
  71. Twitter: @sigje Email: [email protected] 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>
  72. Twitter: @sigje Email: [email protected] 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
  73. Twitter: @sigje Email: [email protected] 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
  74. Twitter: @sigje Email: [email protected] Lesson 31: Use our cookbook. 154

    At command prompt type: chef-client --local-mode --runlist webserver
  75. Twitter: @sigje Email: [email protected] Lesson 32: Reload webpage. Open a

    browser. Shift + Click on reload. 155 Shift +Click
  76. Twitter: @sigje Email: [email protected] 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.
  77. Twitter: @sigje Email: [email protected] Objectives: Working with the Node Object

    Explain what the node object is in Chef. Show details about a node. Describe node attributes. 160
  78. Twitter: @sigje Email: [email protected] 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.
  79. Twitter: @sigje Email: [email protected] Lesson 33: Set PATH variable. 162

    At command prompt type: export PATH=$PATH:/opt/chef/embedded/bin:/opt/chefdk/embedded/bin
  80. Twitter: @sigje Email: [email protected] Lesson 34: Run Ohai 163 At

    command prompt type: ohai To page through the output: ohai | less
  81. Twitter: @sigje Email: [email protected] Node Attributes 165 Ability to programmatically

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

    At command prompt type: chef generate attribute webserver default 166
  83. Twitter: @sigje Email: [email protected] Lesson 37: Edit the attribute file.

    168 edit default.rb EDITOR webserver/attributes/default.rb add content. save file. default["apache"]["indexfile"] = "index.html"
  84. Twitter: @sigje Email: [email protected] 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
  85. Twitter: @sigje Email: [email protected] Lesson 40: Search Supermarket from command

    line 181 At command prompt type: knife cookbook site search chef-client
  86. Twitter: @sigje Email: [email protected] Lesson 40: Search Supermarket from command

    line 182 At command prompt type: knife cookbook site show chef-client
  87. Twitter: @sigje Email: [email protected] Lesson 40: Search Supermarket from command

    line 183 At command prompt type: knife cookbook site show chef-client
  88. Twitter: @sigje Email: [email protected] Lesson 41: Download chef-client cookbook. 184

    At command prompt type: knife cookbook site download chef-client
  89. Twitter: @sigje Email: [email protected] Lesson 42: Extract chef-client cookbook. 187

    At command prompt type: tar zxvf chef-client-3.8.0.tar.gz
  90. Twitter: @sigje Email: [email protected] Lesson 42: Read the chef-client cookbook.

    189 Examine the README.md Expectations of the cookbook, platform, data Recipes Usage Notes
  91. Twitter: @sigje Email: [email protected] 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.
  92. Twitter: @sigje Email: [email protected] Review Overview of Infrastructure Automation Intro

    to Resources, Recipes, Cookbooks Working with the Node Object Introduction to Community Cookbooks 192
  93. Twitter: @sigje Email: [email protected] 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
  94. Twitter: @sigje Email: [email protected] Additional Resources 195 Fundamentals Training, Intermediate

    Training Learn Chef Fundamentals videos Google Forum Sign up for Virtual Office Hours
  95. Twitter: @sigje Email: [email protected] 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