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

初めてのChefの教室

Yusuke Ando
February 22, 2013

 初めてのChefの教室

2/22に株式会社Engine Yardで開催した非インフラエンジニア向けChef入門講座です。

Yusuke Ando

February 22, 2013
Tweet

More Decks by Yusuke Ando

Other Decks in Programming

Transcript

  1. ॳΊͯͷ$IFGͷڭࣨ
    &OHJOF:BSE.FFUVQ
    ඇΠϯϑϥΤϯδχΞͰ΋ग़དྷͨ

    View Slide

  2. ZBOEP

    View Slide

  3. "84$IFGαϙʔτ

    View Slide

  4. &OHJOF:BSE-PDBM
    ͋ͳͨͷPC
    Engine Yard
    Local
    Engine Yard Cloud
    Your
    App
    Your
    App
    PC্Ͱ։ൃ͕ແྉ
    Ϋϥ΢υͱಉ͡؀ڥ
    (VagrantΛ಺แ)
    γʔϜϨεʹΞϓϦΛσ
    ϓϩΠʂ

    View Slide

  5. "HFOEB
    $IFGͱ͸
    ηοτΞοϓ
    Ϩγϐͷهड़
    ৘ใͷ୳͠ํ

    View Slide

  6. $IFGͱ͸

    View Slide

  7. $IFG
    3VCZ੡ͷγεςϜ؅ཧπʔϧ
    ઃఆϑΝΠϧΛݩʹαʔόΛࣗಈઃఆ
    6OJY-JOVY.BD8JOEPXTରԠ
    ແঈ൛Ͱ΋ར༻Մೳ
    ࠃ಺֎Ͱٸ଎ʹελϯμʔυʹ

    View Slide

  8. *OGSBTUSVDUVSFBT$PEF
    2011/2
    ΠϯϑϥΛίʔυͰهड़͢Δ
    ࣌୅͕౸དྷͨ͠

    View Slide

  9. View Slide

  10. DPPLCPPLΛݩʹઃఆ
    recipe
    cookbook
    3VCZͰॻ͔ΕͨϨγϐͷू·Γ͕ΫοΫϒοΫ
    $IFG͸ϨγϐΛݩʹϗετΛࣗಈͰઃఆ͢Δ

    View Slide

  11. $IFGͷߏ଄
    ໨తͷϊʔυ܈
    ؅ཧ༻αʔό

    View Slide

  12. όϦΤʔγϣϯ
    )PTUFE$IFG
    0QT$PEF͕ϗεςΟϯά͢Δ
    ϊʔυ਺ʹԠͨ͡՝ۚ
    1SJWBUF$IFG
    'JSFXBMM಺ͰՔಇ͢Δ
    ༗ঈ൛͸αϙʔτ͋Γ

    View Slide

  13. 1SJDF

    View Slide

  14. 8FC6*
    ࣮͸ɺ࢖ͬͨࣄͳ͍Ͱ͢ʜ

    View Slide

  15. DIFGTPMP
    αʔόʔෆཁͷDIFG
    ϩʔΧϧ্ͷΫοΫϒοΫΛ࣮ߦՄೳ
    (6*͸࢖͑ͳ͍
    DIFGTPMPΛΩοΫ͢Δπʔϧ΋ଟ͍
    FY
    7BHSBOU &OHJOF:BSE

    View Slide

  16. ·ͣ͸$IFG4PMPͰ
    ϨγϐΛ࢖ͬͨߏஙʹ
    ׳ΕΔͷ͕٢

    View Slide

  17. ηοτΞοϓ

    View Slide

  18. ηοτΞοϓ
    3VCZ
    3VCZHFNT
    TPMPSC
    OPEFKTPO

    View Slide

  19. 3VCZSVCZHFNT
    $ sudo apt-get install ruby1.8
    $ sudo apt-get install rubygems
    $ sudo gem install chef
    $ chef-solo -v
    Chef: 11.4.0
    DIFGTPMP͸DIFGʹಉࠝ
    DIFGࣗମ΋BQUHFUͰಋೖՄ

    View Slide

  20. 3VCZͷ؅ཧ͸஫ҙ

    View Slide

  21. TPMPSC
    Ұ࣍σΟϨΫτϦͷ৔ॴͷઃఆ
    ΫοΫϒοΫͷύεͷઃఆ
    FUDDIFGTPMPSC
    ࣮ߦ࣌ʹଘࡏ͠ͳ͍৔߹͸ܯࠂ

    View Slide

  22. TPMPSC
    file_cache_path "/var/chef-solo"
    cookbook_path ["/var/chef-solo/cookbooks"]
    file_cache_path "/var/chef-solo"
    cookbook_path ["/var/chef-solo/cookbooks",
    "/var/chef-solo/site-cookbooks"]
    ෳ਺ࢦఆՄ

    View Slide

  23. OPEFKTPO
    $IFGαʔό͔Βͷσʔλͷ୅ସ
    ΫοΫϒοΫ΁౉͢ม਺Λઃఆ
    ࣮ߦ͢ΔϨγϐΛࢦఆ͢Δ

    View Slide

  24. OPEFKTPO
    {
    "resolver": {
    "nameservers": [ "10.0.0.1" ],
    "search":"int.example.com"
    },
    "run_list": [ "recipe[resolver]" ]
    }
    {
    "run_list": [ "recipe[resolver]" ]
    }
    ࣮ߦ͍ͨ͠Ϩγϐ

    View Slide

  25. DIFGTPMP

    View Slide

  26. ͭ·Γ
    ະઃఆͷঢ়ଶ͔ΒͰ΋೚ҙͷΫοΫ
    ϒοΫΛ࣮ߦग़དྷΔ
    WBSDIFGDPPLCPPLTIPHF
    ͷΫοΫϒοΫΛ࣮ߦ͢ΔͳΒ
    $ sudo chef-solo -o hoge

    View Slide

  27. ςετ࣮ߦ

    View Slide

  28. ͜͜·Ͱͷ·ͱΊ
    TVEPBQUHFUVQEBUF

    TVEPBQUHFUSVCZSVCZHFNT
    TVEPHFNJOTUBMMDIFG
    TVEPLOJGFDPPLCPPLDSFBUFIPHF
    TVEPDIFGTPMPPIPHF

    View Slide

  29. Ϩγϐͷهड़

    View Slide

  30. Ϩγϐͷهड़
    LOJGFίϚϯυ
    σΟϨΫτϦߏ଄
    3FTPVSDF
    ެ։͞Ε͍ͯΔΫοΫϒοΫ

    View Slide

  31. LOJGFίϚϯυ
    DIFGαʔόͱͷ௨৴΍֤छ࡞ۀΛߦ͏
    ΫϥΠΞϯτ
    DIFGTPMPͰར༻͢Δ৔߹͸ΫοΫϒο
    ΫͷॳظԽ࡞ۀʹ࢖͏
    ϓϥάΠϯͰαϒίϚϯυΛ֦ுՄ

    View Slide

  32. αϒίϚϯυ
    ߦ͋Δϔϧϓ
    ʢ֮͑ΒΕͳ͍ʣ

    View Slide

  33. ΫοΫϒοΫ࡞੒
    LOJGFDPPLCPPLDSFBUF\OBNF^
    WBSDIFGDPPLCPPLTҎԼʹ࡞੒
    $IFGαʔόΛ࢖Θͳ͍ͷͰΑ͠ͳʹ
    (JU)VC΍4Ͱ؅ཧ

    View Slide

  34. σΟϨΫτϦߏ੒
    CHANGELOG.md
    attributes/
    files/
    metadata.rb
    recipes/
    templates/
    README.md
    definitions/
    libraries/
    providers/
    resources/
    ϨγϐΛஔ͘
    QMBUGPSNຖʹ΋ग़དྷΔ

    ઃఆϑΝΠϧͷͻͳܗ
    ϨγϐͰར༻͍ͨ͠ม਺ͳͲ

    View Slide

  35. SFDJQFTEFGBVMUSC
    log “Hello Chef”
    QMBUGPSNΛؾʹ͠ͳ͍৔߹

    View Slide

  36. Ϩγϐ
    3VCZͰॻ͔ΕͨεΫϦϓτ
    ಺෦%4-Λ࢖ͬͯهड़
    3FTPVSDFͱݺ͹ΕΔจ๏
    ૊ΈࠐΈͷ3FTPVSDF͸04ʹґଘ͠ͳ͍
    ઃఆ࡞ۀΛఏڙ

    View Slide

  37. Α͘࢖͏ػೳ

    View Slide

  38. SFDJQJFTEFGBVMUSC
    log "Hello Chef"
    package "apache2" do
    action :install
    end
    service "apache2" do
    action :start
    end
    ֤؀ڥͷύοέʔδ؅ཧΛ࣮ߦ

    View Slide

  39. BQBDIFೖͬͨ

    View Slide

  40. BQUHFUͰྑ͘Ͷʁ
    ୯७ͳύοέʔδಋೖҎ্ͷॲཧ͕ग़
    དྷΔͷ͕$IFGͷڧΈ
    ݻ༗ͷઃఆϑΝΠϧͷઃஔ
    ύοέʔδؒɺϊʔυؒͷ࿈ಈઃఆ
    ࣮ߦ࣌ʹ৘ใΛऔಘ͢Δػೳ

    View Slide

  41. UFNQMBUFϦιʔε
    &3#Λ࢖ͬͯಈతͳ஋Λૠೖ
    "QBDIFQIQOHJOYͳͲͷઃఆʹ
    ϓϥοτϑΥʔϜຖʹࠩ͠ସ͑΋Մೳ

    View Slide

  42. UFNQMBUFTEFGBVMUUFTUUYUFSC
    This is Sample File.

    <%= node[:platform] %>

    <%= node[:platform_version] %>
    SFDJQFTEFGBVMUSC ଓ

    template "/var/www/test.txt" do
    source "test.txt.erb"
    mode 0644
    end

    View Slide

  43. ಈతʹϑΝΠϧΛ࡞੒

    View Slide

  44. JODMVEF
    ผͷΫοΫϒοΫΛಡΈࠐΉ
    طଘͷ஌ࣝͷ࠶ར༻
    ґଘؔ܎ͷఆٛ
    include_recipe “fuga”

    View Slide

  45. ͦͯ͠
    ࣗྗͰهड़ͨ͠ϨγϐΛஷΊ͍ͯ͘
    ωοτ্ͷϨγϐΛख௚ͯ͠͠ར༻
    *OGSBTUSVDUVSFBTDPEFΛ࣮ݱ

    View Slide

  46. ৘ใͷ୳͠ํ

    View Slide

  47. ެ։͞Ε͍ͯΔϨγϐ
    ͞·͟·ͳํ๏Ͱ͞·͟·ͳஂମ͕
    ΫοΫϒοΫΛެ։͍ͯ͠Δ
    ϨγϐΛ࠶ར༻͢Δ৔߹͸શମͷΞʔ
    ΩςΫνϟʹେ͖ͳӨڹ

    View Slide

  48. DPNNVOJUZPQTDPEFDPN

    View Slide

  49. (JU)VC্ͷϦιʔε
    opscode-cookbooks
    ެࣜΫοΫϒοΫ
    rightscale/cookbooks_public
    engineyard/ey-cloud-recipes
    aws/opsworks-cookbooks
    ֤ϓϥοτϑΥʔϜ޲͚
    jsierles/chef_cookbooks
    ͔ͭͯ͸ 37signalsͷϦϙδτϦͩͬͨ΋ͷ

    View Slide

  50. 4PVSDFΠϯετʔϧΛ࣮૷

    View Slide

  51. View Slide

  52. 3PMF͕࣮૷͞Ε͍ͯΔ

    View Slide

  53. View Slide

  54. View Slide

  55. ͪΐͬͱมΘͬͨ෺
    pivotal/pivotal_workstation
    Pivotalͷ։ൃऀͷPCΛηοτΞοϓ͢Δ

    View Slide

  56. $ISPNF

    View Slide

  57. .BDͷนࢴΛઃఆ

    View Slide

  58. ڭ܇
    ެࣜϨγϐ͸ϚϧνϓϥοτϑΥʔϜ
    1BB4ܥ͸޻෉͕࣮૷͞Ε͍ͯΔ
    ಠࣗʹॻ͘ͳΒܾΊଧͪՄೳ
    ʮखॱॻʯΛࣗಈԽ͢Δهड़ྗ

    View Slide

  59. ࣭໰ʁ

    View Slide