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

create_prod_and_dev_vm_image_atst_by_packer

 create_prod_and_dev_vm_image_atst_by_packer

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

Tatsuro Mitsuno

July 07, 2016
Tweet

More Decks by Tatsuro Mitsuno

Other Decks in Technology

Transcript

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

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

    ʜ w NJEEMFXBSF PQT BE
 TPMS SFEJT OBU ʜ (PPHMF$MPVE&OHJOFগ͠ J20/
  3. 7"4*-: *OD ɹɹ1BDLFSCZ)BTIJ$PSQ w )BTIJ$PSQ੡ w ༷ʑͳϓϥοτϑΥʔϜ্ͷϚγϯΠϝʔδΛ࡞ΔͨΊͷπʔϧ w ઃఆϑΝΠϧ͸+40/ w

    ϓϥοτϑΥʔϜͱϓϩϏδϣϯπʔϧͷ૊Έ߹Θͤ͸ࣗ༝ w #VJME 1SPWJTJPOΛҰ࿈ͷྲྀΕͱ࣮ͯ͠ߦ
  4. 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Ͱߏ੒؅ཧ
  5. 7"4*-: *OD SPMFT Ұ෦ w QBDLFSCBTFDPOpHVSFKTPO w QBDLFSBQJCBTFDPOpHVSFKTPO w QBDLFSBQJBQJDPOpHVSFKTPO

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

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

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

    UEBHFOUͷDPOG͸ίϝϯτ Ξ΢τ͞ΕΔ w ϩʔΧϧσϓϩΠ༻ʹ DBQJTUSBOPΛγεςϜʹ௥Ճ QSPEVDUJPO w 伴͸ຊ൪༻ͷൿີ伴
 w ؂ࢹPO w UEBHFOUͷDPOG͕༗ޮͳঢ় ଶͰஔ͔ΕΔ
  9. 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͕Ͱ͖Δ
  10. 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ͰΠϯελϯεΛཱͯΔ
 # => ηΩϡϦςΟάϧʔϓͳͲΛॻ͖ࠐΜͩεΫϦϓτͰΠϯελϯεੜ੒
  11. 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

  12. 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