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

Chef & Immutable Infrasturcture

Richard Lee
September 26, 2014

Chef & Immutable Infrasturcture

For Rails Pacific workshop.

Richard Lee

September 26, 2014
Tweet

More Decks by Richard Lee

Other Decks in Programming

Transcript

  1. "Richard Lee".inspect 4 CTO & Cofounder at iCook.tw (Polydice, inc.)

    4 Rails, DevOps & iOS 4 GitHub, Twitter & everywhere: @dlackty 4 Email: [email protected] Feel free to contact me for anything!
  2. Prerequisite 1. Have you install Vagrant yet? 2. Have you

    install ChefDK yet? 4 Not only Chef 4 But many other tools 4 Can be installed via gem install as well though Go download it or ask staff for USB flash drive.
  3. Vagrant installation Go to Vagrant website and download packages. https://www.vagrantup.com/

    and you also need VirtualBox from its website https://www.virtualbox.org/
  4. ChefDK installation Go get SDK on ChefDK website https://downloads.getchef.com/ chef-dk/

    To verify the installation, type following command chef verify
  5. Agenda We'll have five exercises, each for 30 minutes. 1.

    Set up VirtualBox & Chef 2. Create your first cookbook 3. Write test for your cookbook 4. Add cookbook dependencies using Berksfile 5. Prepare environment for your Rails app
  6. Test Kitchen Test Kitchen is an integration tool for developing

    and testing infrastructure code and software on isolated target platforms. http://kitchen.ci/
  7. Let's get started Type this on shell: git init workshop

    cd workshop kitchen init and it will generate .kitchen, .kitchen.yml, and also .gitignore
  8. Check .kitchen.yml 4 Driver is the way to set up

    base instance 4 Vagrant for local testing 4 EC2 / Digital Ocean or others for production 4 Provisioner is the tool for environment setup process 4 Chef / Puppet / Ansible
  9. Check .kitchen.yml (cont’d) 4 Platforms 4 It’s obvious, right? 4

    Use ubuntu-14.04 for this workshop 4 Suites 4 Different set of recipes you want to run 4 e.g. App, Database, Cache, LoadBalancer
  10. So… you’ll need to download VM image The SPoF (Single

    Point of Failure) of this workshop. We’ll use it later (and tell you how to download it) We've downloaded it and put it into USB sticks. Feel free to ask us to install faster. vagrant box add PATH --name opscode-ubuntu-14.04
  11. Then let’s run it up! Just use the command: kitchen

    create ubuntu Argument can be a regex for available instances. You can check all available instance via kitchen list
  12. Vagrant Commonly misunderstood as a command line tool for VirtualBox.

    1. It’s a general tool 2. Many plugins available 3. Support provision tools
  13. Test Kitchen supported platforms kitchen driver discover Just name a

    few: 1. kitchen-{azure, cloudstack, digitalocean, ec2, gce, backspace, openstack, docker} 2. kitchen-ssh for almost every server!
  14. Login to instance kitchen login ubuntu Here you go! and

    you can open VirtualBox.app to see running instances. You can use kitchen destroy to remove it.
  15. Cookbook basics 4 cookbook is a fundamental unit for a

    scenario 4 e.g. nginx / elasticsearch / mysql 4 cookbook has_many recipes 4 e.g. nginx::default, nginx::ssl, nginx::status
  16. Common use case Usually you will have a cookbook representing

    “your app”. e.g. We have our app “icook” cookbook opne sourced on GitHub “polydice/cookbooks”
  17. Define cookbook Like .gemspec is for Ruby gems, metadata.rb is

    for Chef cookbook. Create an metadata.rb with following content: name "workshop" version "0.1.0"
  18. Chef recipe in Ruby Chef is written in Ruby, and

    you can use arbitrary Ruby syntax in recipe. if node["platform"] == "ubuntu" # Do ubuntu thing end
  19. Chef DSLs However, there’re some “Recipe DSL” methods like platform?

    or platform_family? if platform_family? "debian" # Do debian thing end Check http://docs.getchef.com/chef/dsl_recipe.html for more information
  20. Resources You can think of Chef resources as wrapper of…

    system resources. To name a few built-in ones: 4 directory, file, user, group - create things 4 package - for system package 4 bash - to run random shell script (well, be careful) 4 cron - to update crontab
  21. Learn Chef resources This might be the first obstacle for

    Chef beginners. As usual, please check Chef official doc for more info: http://docs.getchef.com/chef/resources.htm
  22. Put something into our recipe For some packages for our

    Rails app, open recipes/ default.rb and put: package "git" package "graphicsmagick" log “OK! We now complete exercise 2”
  23. Run list In Chef, we define run list, which contains

    a series of recipes that will be executed in order. So update .kitchen.yml: suites: - name: default run_list: workshop::default attributes:
  24. Now let’s run again Use the following command: kitchen converge

    This will help you: 1. Install chef on the instance 2. Copy cookbooks to the instance 3. Execute recipes in run list
  25. Verify by hand Login to the instance: kitchen login and

    check: vagrant@default-ubuntu-1404:~$ git --version git version 1.9.1
  26. Immutable infrastructure Immutable means not changeable, and there’re benefits: 1.

    Reduce inconsistency 2. Improve the trust into your deployment process 3. The whole process is repeatable, hence 4 It’s easier to recover, scale 4 It’s testable
  27. Introduce Serverspec Server spec is a set of RSpec matchers

    for infrastructure testing. Again, check the document online for example usages. http://serverspec.org/ resource_types.html
  28. Serverspec examples There’re few examples for Severspec: describe command("whoami") do

    it { should return_stdout "root" } end describe file("/etc/sudoers") do it { should be_readable.by("owner") } it { should be_readable.by("group") } end
  29. Let’s write our specs Create folder and file: mkdir -p

    test/integration/default/serverspec touch test/integration/default/severspec/packages_spec.rb Be careful about spelling. Test Kitchen uses your directory name to select testing framework.
  30. And put something into files Put something like below: require

    'serverspec' include Serverspec::Helper::Exec include Serverspec::Helper::DetectOS describe package("git") do it { should be_installed } end describe package("graphicsmagick") do it { should be_installed } end
  31. Run it! Let’s run it! kitchen verify and as usually,

    Test Kitchen will help you: 1. Set up test framework 2. Copy test files 3. Run the tests
  32. To do a complete test To double confirm your recipe

    and test are in good status, use the following command: kitchen test and it does everything we discussed before: {destroy, create, converge, verify} to make sure it works.
  33. Berksfile is Gemfile for Chef As we need Bundler for

    Ruby Gems, Berkshelf is made for cookbook dependency management. Developed by Riot Games, company behind League of Legends.
  34. 2 ways to add dependencies Again, like Ruby Gems, you

    can add dependencies in Berksfile or metadata.rb. My personal suggestion: 4 Put related and real dependencies in metadata.rb 4 Put something else in Berksfile
  35. Recipe configuration How could we do recipe configuration? Usually there’re

    2 ways: 1. node attributes 2. data bags tl;dr: Most of recipes use node attributes now. Data bags are deprecated.
  36. Reading README When you use a new cookbook, first read

    its README file for: 1. What recipes available 2. What node attributes are able to be used to configure Take nginx as example: https://supermarket.getchef.com/cookbooks/nginx
  37. Be cautious of different version of ! There’re usually several

    versions of “nginx” cookbook available, and you need to be careful the version you use might provides different set of recipes. Use Berksfile to specify: cookbook 'mysql', path: '../mysql-cookbook' cookbook 'mysql', git: 'git://github.com/opscode-cookbooks/mysql.git' cookbook 'nginx', github: 'dlackty/mysql', branch: 'something'
  38. Let’s do this Edit metadata.rb: name "workshop" version "0.1.0" depends

    "nginx", "~> 2.7.0" then you can put nginx::default into run list. Still remember how?
  39. Another way to include recipe To combine few recipes, put

    this into our recipe: include_recipe("nginx::default") then run kitchen converge again.
  40. All right, you have a good start now Now it’s

    your time. 1. Go pick up few cookbook 2. Run it on the machine 3. Try different drivers
  41. Go pick up few cookbook There’re few steps that you

    can consider: 1. Search supermarket.chef.com 2. Search GitHub.com e.g. MySQL, Redis, Elsticsearch and others
  42. A good start point You can find some cookbook is

    designed for Rails, and includes several required dependencies teohm/rackbox-cookbook is a good start point.
  43. Try different drivers Test Kitchen provides a good plugin system:

    1. Drivers - to spin up new instances 2. Bussers - to run test
  44. Take EC2 as example From http://rubydoc.info/gems/kitchen-ec2 driver: name: ec2 aws_access_key_id:

    KAS... aws_secret_access_key: 3UK... aws_ssh_key_id: id_rsa-aws ssh_key: /path/to/id_rsa-aws security_group_ids: ["sg-1a2b3c4d"] region: us-east-1 availability_zone: us-east-1b subnet_id: subnet-6d6...
  45. Deployment Usually for Chef beginners, you will get confused with

    Chef & deployment tools like Capistrano. Don’t panic. 1. Try to avoid using Chef as deployment tool at first 2. Consider using Chef as a way to set up Capistrano friendly environment
  46. How about docker? Just another way to create base instances.

    You still need to “set it up”. It’s can be used with Vagrant & Test Kitchen.
  47. chef-solo and chef-server What we do today is named chef-solo,

    which indicates that it doesn’t require a central server. chef-server isn’t recommended for most people because: 1. The server is complex 2. One more SPoF 3. Many functionalities are duplicate of other tools
  48. Chef in production To adopt Chef successfully in production, usually

    there’re 2 ways. 1. Use Chef-enabled environment like AWS OpsWorks or Engine Yard 2. Use Chef to provision images for later usage
  49. AWS OpsWorks It’s a free platform provided by AWS built

    upon EC2 & related products. 1. It provides a builtin set of cookbook for Rails / PHP / Node.js 2. It has API to run Chef programmatically 3. Deploy vis GitHub