create_prod_and_dev_vm_image_atst_by_packer

 create_prod_and_dev_vm_image_atst_by_packer

UZABASE Meetup#4 〜大規模サービスを支えるインフラ〜
#UzabaseMeetup
1コマンドで本番サーと開発サーバのVMイメージを作る話

231dbe4867c139c325b61e808b757750?s=128

Tatsuro Mitsuno

July 07, 2016
Tweet

Transcript

  1. 7"4*-: *OD ίϚϯυͰ ຊ൪αʔόͱ։ൃαʔό ͷ7.Πϝʔδ Λ࡞Δ࿩ 6;"#"4&.FFUVQ
 ʙେن໛αʔϏεΛࢧ͑ΔΠϯϑϥʙ

  2. 7"4*-: *OD ࣗݾ঺հ w ޫ໺ୡ࿕5BUTVSP.JUTVOP !LPUBUTV  w 7"4*-: *OD*OGSB&OHJOFFS

    w ʙ w ҆ͯ͘଎ͯ҆͘શͳΠϯϑϥΛ࡞Δ w ੜ࢈ੑΛ޲্ͤ͞ΔऔΓ૊Έ झຯ *DPOJMMVTUSBUFECZ:04)* IUUQTXXXGBDFCPPLDPNZPTIJZPOF
  3. 7"4*-: *OD

  4. 7"4*-: *OD &$*OTUBODF  w BQQMJDBUJPO
 XFC BQJ CBUDI DSBXMFS

    ʜ w NJEEMFXBSF PQT BE
 TPMS SFEJT OBU ʜ (PPHMF$MPVE&OHJOFগ͠ J20/
  5. 7"4*-: *OD ίϚϯυͰຊ൪ˍ։ൃαʔόΛ࡞Δ J20/αʔόͷ࡞Γํ
 
 QBDLFSCVJMECBTFKTPO WBHSBOUCPY
 BXTFDBNJ
 HDFJNBHF
 


    શ෦Ͱ͖Δ
  6. 7"4*-: *OD ܦҢɿΞϓϦέʔγϣϯαʔόͷҠߦ w ʙ w $FOU046CVOUV w 3VCZʢҰ෦ 

    w DIFGͱTQFD͸͋ͬͨ w ʮखݩͷ7.Ͱݕূͯͦ͠ͷ7.Λຊ൪Ͱͦͷ··࢖͑ͨΒ͍͍ͷʹʯ
  7. 7"4*-: *OD ઓུ w ݱঢ়ͷࢿ࢈Λ༗ޮ׆༻
 ߏ੒؅ཧ͸$IFG w ຊ൪΋։ൃ΋ಉ͡ϨγϐͰߏ੒؅ཧ͢Δ
 ࠩҟ͸$IFGͷ&OWJSPONFOUTͰԿͱ͔͢Δ w

    ࠓޙͷߋ৽͕༰қɾຊ൪ͱ։ൃͷဃ཭Λى͜͞ͳ͍
 ΞΫγϣϯͰߋ৽Ͱ͖ΔૉఢͳԿ͔Λ࢖͏
  8. 7"4*-: *OD ɹɹ1BDLFSCZ)BTIJ$PSQ w )BTIJ$PSQ੡ w ༷ʑͳϓϥοτϑΥʔϜ্ͷϚγϯΠϝʔδΛ࡞ΔͨΊͷπʔϧ w ઃఆϑΝΠϧ͸+40/ w

    ϓϥοτϑΥʔϜͱϓϩϏδϣϯπʔϧͷ૊Έ߹Θͤ͸ࣗ༝ w #VJME 1SPWJTJPOΛҰ࿈ͷྲྀΕͱ࣮ͯ͠ߦ
  9. 7"4*-: *OD ઃఆϑΝΠϧ Ұ෦ $ cat base.json
 {
 "builders":[
 {"type":

    "virtualbox-ovf"},
 {"type": "amazon-ebs" },
 {"type": "googlecompute" }
 ],
 "provisioners": [
 { "type": "chef-client" }
 ]
 } ˞࣮ࡍʹ͸ඞਢͷύϥϝʔλ͕ଞʹ΋͋Γ·͢ Vagrant BoxͷݩʹͳΔVirtualBox Amazon EC2 AMI Google Compute Engine Image VMʹchef-clientͰߏ੒؅ཧ
  10. 7"4*-: *OD ઓུ w ✅ݱঢ়ͷࢿ࢈Λ༗ޮ׆༻
 ϓϩϏδϣχϯά͸$IFG w ຊ൪΋։ൃ΋ಉ͡ϨγϐͰߏ੒؅ཧ͢Δ
 ࠩҟ͸$IFGͷ&OWJSPONFOUTͰԿͱ͔͢Δ w

    ✅ࠓޙͷߋ৽͕༰қɾຊ൪ͱ։ൃͷဃ཭Λى͜͞ͳ͍
 ΞΫγϣϯͰߋ৽Ͱ͖ΔૉఢͳԿ͔Λ࢖͏
  11. 7"4*-: *OD $IFGͷߏ੒ w ਌ࢠؔ܎Λ࣋ͬͨSPMFʷFOWJSPONFOUͰදݱ

  12. 7"4*-: *OD SPMFT Ұ෦ w QBDLFSCBTFDPOpHVSFKTPO w QBDLFSBQJCBTFDPOpHVSFKTPO w QBDLFSBQJBQJDPOpHVSFKTPO

    w QBDLFSBQJCBUDIDPOpHVSFKTPO w QBDLFSXFCDPOpHVSFKTPO ֤ϊʔυ͸຤୺ͷϩʔϧΛಡΈࠐΉ
 ਌ͷमਖ਼΋ඞͣऔΓࠐΉ
  13. 7"4*-: *OD Ұྫ QBDLFSBQJCBTF DPOpHVSFKTPO QBDLFSBQJBQJ DPOpHVSFKTPO "run_list": [ "role[packer-base-configure]",

    "recipe[fluentd]", ... "run_list": [ "role[packer-api-base-configure]", "recipe[nginx]" ]
  14. 7"4*-: *OD &OWJSPONFOUT w QBDLFS w WBHSBOU w TUBHJOH w

    QSPEVDUJPO w 44)ͷઃఆɾ伴 w ؂ࢹͷ༗ແ w %/4पΓͷઃఆ w ϩάίϨΫλʔͷઃఆ ؅ཧ
  15. 7"4*-: *OD Ұྫ WBHSBOU w 伴͸WBHSBOUͷ JOTFDVSF@QSJWBUF@LFZ w ؂ࢹPGG w

    UEBHFOUͷDPOG͸ίϝϯτ Ξ΢τ͞ΕΔ w ϩʔΧϧσϓϩΠ༻ʹ DBQJTUSBOPΛγεςϜʹ௥Ճ QSPEVDUJPO w 伴͸ຊ൪༻ͷൿີ伴
 w ؂ࢹPO w UEBHFOUͷDPOG͕༗ޮͳঢ় ଶͰஔ͔ΕΔ
  16. 7"4*-: *OD "84ʹαʔόΛཱͯΔ༷ࢠ $ cat /path/to/chef-repo/nodes/packer-base.json
 {
 "name": "packer-base",
 "chef_environment"

    : "packer",
 "run_list": [ 
 "role[packer-base-configure]" 
 ]
 }
 
 $ packer build base.json
 
 # => ruby2.3.1ΛೖΕͨΓmackerel-agentΛೖΕͨΓ
 # => packer-ubuntu1604-ruby231-{{timestamp}}ͱ͍͏໊લͷAMI͕Ͱ͖Δ
  17. 7"4*-: *OD "84ʹαʔόΛཱͯΔ༷ࢠ # packerͰ࡞ͬͨ࠷৽ͷAMI-IDΛऔͬͯ͘ΔϫϯϥΠφʔ
 
 $ aws ec2 describe-images

    --owners self \ | jq '.Images[]|select((select(.Name != null)|.Name|contains("packer-ubuntu")))' \ | jq 'sort_by(.CreationDate)|reverse|.[0].ImageId' --slurp -r
 
 ami-12345678 
 
 
 # => ͜ͷAMIͰΠϯελϯεΛཱͯΔ
 # => ηΩϡϦςΟάϧʔϓͳͲΛॻ͖ࠐΜͩεΫϦϓτͰΠϯελϯεੜ੒
  18. 7"4*-: *OD "84ʹαʔόΛཱͯΔ༷ࢠ $ cat /path/to/chef-repo/nodes/api-stg.json
 {
 "name": "api-stg",
 "automatic":

    { "fqdn": "api-stg" },
 "chef_environment" : "staging",
 "run_list": [ 
 “role[packer-api-api-configure]" 
 ]
 }
 
 $ pwd
 /path/to/chef-repo
 
 $ knife zero bootstrap <ΠϯελϯεͷIP> --sudo -N api-stg --no-converge
 $ knife zero converge ‘name:api-stg’ --sudo

  19. 7"4*-: *OD ϩʔΧϧʹαʔόΛཱͯΔ༷ࢠ $ cat Vagrantfile
 config.vm.define 'api' do |api|


    ...
 api.vm.provision 'chef_zero' do |chef|
 chef_zero_conf(chef, run_list: 'role[packer-api-api-configure]')
 end
 ...
 
 def chef_zero_conf(chef, run_list:)
 ...
 chef.run_list = run_list
 chef.environment = 'vagrant'
 ...
 $ vagrant up api
  20. 7"4*-: *OD ઓུ w ✅ݱঢ়ͷࢿ࢈Λ༗ޮ׆༻
 ϓϩϏδϣχϯά͸$IFG w ✅ຊ൪΋։ൃ΋ಉ͡ϨγϐͰߏ੒؅ཧ͢Δ
 ࠩҟ͸$IFGͷ&OWJSPONFOUTͰԿͱ͔͢Δ w

    ✅ࠓޙͷߋ৽͕༰қɾຊ൪ͱ։ൃͷဃ཭Λى͜͞ͳ͍
 ΞΫγϣϯͰߋ৽Ͱ͖ΔૉఢͳԿ͔Λ࢖͏
  21. 7"4*-: *OD 1SPT w ݶΓͳ͘ຊ൪ʹ͍ۙ؀ڥͰύοέʔδͷݕূ w DIFGSFDJQFͷݕূ

  22. 7"4*-: *OD $POT w Πϝʔδͷੈ୅؅ཧ w MBUFTUΛ࢖͏ͱܾΊͯ͠·͑͹ෆཁ

  23. 7"4*-: *OD 5JQT w QBDLFSEFCVHNPEF͸ศརͳͷͰͲΜͲΜ࢖͏΂͖
 QBDLFS͕ϓϩϏδϣχϯά͍ͯ͠Δαʔό΁44)͢Δ৘ใ͕΋Β͑Δ w BQUHFUͷBVUPVQEBUF͸ཁ஫ҙ
 6CVOUV͸ىಈ࣌ʹABQUHFUVQEBUFA͕ࣗಈͰ૸ΓɺDIFGͱͿ͔ͭΔ
 EJTBCMFʹ͢Δ͔TMFFQͰִؒௐ੔

  24. 7"4*-: *OD ·ͱΊ w QBDLFSͰෳ਺ͷ7.ΠϝʔδΛಉ࣌ʹੜ੒
 ݱࡏͷQBDLFSҠߦ཰͸͘Β͍ w DIFGͰߏ੒؅ཧ w ຊ൪ͱ։ൃͰಉ͡಺༰ͷ7.Λ࢖͑Δ؀ڥΛߏங

  25. 7"4*-: *OD 8FBSFIJSJOH IUUQTXXXXBOUFEMZDPNDPNQBOJFTWBTJMZJOGP ˜PLBEBQBO