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

applibotのDevOpsを支える terraform/packer

September 11, 2018

applibotのDevOpsを支える terraform/packer

2018/09/11 DevOpsを支える今話題のHashiCorpツール群について

September 11, 2018
Tweet

More Decks by 遊

Other Decks in Technology

Transcript

  1. applibotͷDevOpsΛࢧ͑Δ
    terraform/packer
    DevOpsΛࢧ͑Δࠓ࿩୊ͷHashiCorpπʔϧ܈ʹ͍ͭͯ
    2018/09/11 ੢ଜ ༡

    View full-size slide

  2. ೋ ࣇ ͷ ෕
    ͤ ͍ ΂ ͭ ɿ ͓ ͱ ͜
    Ϩ ϕ ϧ ɿ
    ) 1
    . 1
    ࣾ ձ ਓ ྺ
    d ɹ % $ Ͱ ؂ ࢹ Φϖ Ϩ ʔ λ ʔ
    d ɹ ג ࣜ ձ ࣾ ɹ ɹ ɹ ɿ 4 : 4 5 & . 0 1 & 3 "50 3
    d / 0 8 ɹ ɹ ג ࣜ ձ ࣾ " 1 1 - * # 05 ɿ 4 : 4 5 & . 0 1 & 3 "50 3
    ͳ · ͑ ɹ ɹ ɿ ੢ ଜ ɹ ༡
    ͭ ͍ ͬ ͨ ʔ ɿ !HBDIBSJPO
    HVNJ
    ࣗ ݾ ঺ հ

    View full-size slide

  3. גࣜձࣾΞϓϦϘοτ
    ೥݄̓೔૑ཱ
    $ZCFS"HFOU
    εϚʔτϑΥϯήʔϜΤϯλʔςΠϝϯτࣄۀ 4(&

    ɹͷ಺ͷࣾ
    ήʔϜ͚ͩͰͳ͘ɺΦϯϥΠϯϓϩάϥϛϯάڭҭαʔϏε΍
    ϝσΟΞܥͷ৘ใൃ৴αΠτ΋ӡӦ
    ձࣾઆ໌

    View full-size slide

  4. ̍ΞϓϦέʔγϣϯຖʹ
    Χϯύχʔ੍

    View full-size slide

  5. ӡ༻தͷΞϓϦ

    View full-size slide

  6. ৽ن։ൃ1+
    /ຊ

    View full-size slide

  7. ͦͷଞαΠτ࡞੒΍Β
    ৽نࣄۀ΍Β
    ΋Ζ΋Ζ΋Ζ

    View full-size slide

  8. 4:401 αʔό؀ڥΛ༻ҙ͢Δਓ


    View full-size slide

  9. ΞϓϦέʔγϣϯνʔϜͷ
    αʔόΤϯδχΞͱ
    ڠྗ͢Δ͜ͱ͕ଟ͍

    View full-size slide

  10. ࿩͢͜ͱ
    w աڈʹ͋ͬͨ՝୊Λ

    UFSSBGPSNQBDLFSΛ࢖ͬͯ

    Ͳ͏ղফ͔ͨ͠ͱ͍͏͓࿩

    View full-size slide

  11. ࿩͞ͳ͍͜ͱ
    w ࡉ͔͍5JQT

    View full-size slide

  12. agenda
    w DBTF*NBHF࡞੒
    w DBTFෛՙࢼݧ؀ڥߏங
    w DBTF৽ن؀ڥߏங
    w ·ͱΊ

    View full-size slide

  13. $BTF
    *NBHF࡞੒

    View full-size slide

  14. º º
    ࢖͏΋ͷ

    View full-size slide

  15. Packer
    • ༻్
    • AMI / GCP Image / Container Imageͷ࡞੒
    • ansibleͱซ༻ͯ͠࢖༻
    • ϝϦοτ
    • Πϝʔδ࡞੒ͷྲྀΕΛςϯϓϨʔτԽ

    View full-size slide

  16. before
    w ྫ"NB[PO".*
    CBTFͱͳΔ".*͔Β&$ىಈ
    "OTJCMFͰߏ੒มߋΛద༻
    Πϝʔδऔಘͨ͠αʔόͷ࡟আ
    Ҏ্ΛશͯखಈͰ࣮ߦ

    View full-size slide

  17. before
    ىಈதʹผͷ࡞ۀ΍Δ
    ˠ๨ΕΔ
    Πϝʔδऔಘޙͷαʔό࡟আ
    ˠ๨ΕΔ
    ͦ΋ͦ΋͜ͷ࡞ۀ
    ˠΊΜͲ͍

    View full-size slide

  18. after
    $ p a c k e r b u i l d p a c k e r . j s o n
    ʊਓਓਓਓਓਓਓਓਓʊ
    ʼɹίϚϯυҰൃʂɹʻ
    ʉ:?:?:?:?:?:?ʉ

    View full-size slide

  19. ৄࡉ͸ϒϩάͰ
    ͯͬ͘΅ͬͱ
    https://blog.applibot.co.jp/
    2018/05/11/how-to-build-
    aws-ami-in-applibot/

    View full-size slide

  20. $BTF
    ෛՙࢼݧߏங

    View full-size slide

  21. ෛՙࢼݧͷ৔߹ͷ໾ׂ
    ෛ ՙ ࢼ ݧ Λ ͔ ͚Δ ਓ
    γ φ Ϧ Φ ࡞ ੒
    ࢼ ݧ ࣮ ߦ
    ݁ Ռ · ͱ Ί
    ෛ ՙ ࢼ ݧ ؀ ڥ Λ ࡞ Δ ਓ
    ؀ ڥ ࡞ ੒
    ߏ ੒ ม ߋ
    Ϩ Ϗϡ ʔ
    αʔόΤϯδχΞ γεΦϖ

    View full-size slide

  22. Α͋͘Δޫܠ
    ৽ Πϕ ϯ τ ։ ൃ ͠ · ͠ ͨ ʂ
    ෛ ՙ ࢼ ݧ ΍ Γ ͨ ͍Ͱ ͢ ʂ
    ͍ ͭ · Ͱ ʹ ༻ ҙ Ͱ ͖ · ͢ ͔ ʂ ʁ
    ͏ ʔ ʔ ʔ ʔ Μ ɻ ࠓ ख ͕ ۭ ͔ ͳ ͍
    ͔ Β ʜ

    ೔ ͙ Β ͍Ͱ ʜ
    ͦ͘ ͓ ͦ ʂ
    ʢ ྃ ղ Ͱ ͢ ʂ
    ΑΖ ͠ ͘ ͓ ئ ͍ ͠ · ͢ ʂ ʣ

    View full-size slide

  23. ӡ༻தͷαʔϏε͕
    ෛՙࢼݧΛ΍Δ࣌
    w ৽نΠϕϯτϦϦʔεલ
    w αʔόଆͷߏ੒มߋΛߦ͏࣌
    w $.౳େن໛13લ

    View full-size slide

  24. ༻ҙ͢Δ΋ͷ ΠϯϑϥपΓ

    w ࠷ऴతʹɺຊ൪ಉ౳ͷ؀ڥ
    w ࢼݧͷλΠϛϯάͰ࡞੒
    w ෛՙΛ͔͚Δαʔό
    w +.FUFSΛ࢖༻ &$
    ɻωοτϫʔΫ͸ผɻΞΧ΢ϯτ෼͚͍ͯΔ
    w ֤छϞχλϦϯά
    w HSBGBOBQSPNFUIFVTLJCBOBFMBTUJDTFBSDI
    w ͜͜͸ࠓճؔ܎ͳ͠

    View full-size slide

  25. ༻ҙ͢Δ΋ͷ ΠϯϑϥपΓ

    w ࠷ऴతʹɺຊ൪ಉ౳ͷ؀ڥ
    w ࠷ॳ͸ಉ͡ߏ੒Ͱখ͍͞ΠϯελϯεΫϥε
    w ຊ൪ಉ౳ͷن໛Ͱৗʹҡ࣋͢Δͱɺ

    අ༻͕ϠόΠͷͰࢼݧͷ౓ʹ࡞੒͢Δ
    w ࢼݧظؒதͰ΋ɺ࢖Θͳ͍ͱ͖͸খ͘͞

    View full-size slide

  26. ༻ҙ͢Δ΋ͷ ΠϯϑϥपΓ

    w ෛՙΛ͔͚Δαʔό
    w ฐࣾͰ͸+.FUFSΛ࢖༻
    w NBTUFSTMBWFߏ੒

    TMBWF͸"VUP4DBMJOHͰεέʔϧ͢Δ
    w ͪ͜Β΋ࢼݧͷ౓ʹ࡞੒
    w ࢖Θͳ͍࣌͸࡟আ

    View full-size slide

  27. before
    ؅ཧΠϯελϯε෼ͷBXTDMJΛΰϦΰϦ
    3%4&MBTUJ$BDIF&$FUDʜ
    ىಈॱ൪͸εΫϦϓτؒͰௐ੔
    ग़དྷ্͕Δൿ఻ͷλϨ
    TZTPQଆͰຖேຖ൩ͷ֦ுॖখରԠ

    View full-size slide

  28. Կ͔໰୊͋Δʁ
    w Ұ౓ಈ͔ͤ͹ྑ͍͚Ͳ
    w ؅ཧ͕େม
    w ଞϓϩδΣΫτͰ͸·ͨΰϦΰϦͱ
    w ҉໧తͳߏ੒ॱ΋͋ͬͨΓͰ

    ֮͑ͯ΋Β͍ͮΒ͍

    View full-size slide

  29. ͦΜͳ࣌ͦ͜

    View full-size slide

  30. Ұ͔ΒUFSSBGPSNͰ࡞੒
    w ߏ੒ཁૉΛશͯUFSSBGPSNԽ
    w جຊతʹ͸࡞Γ௚͠
    w طʹ͋ΔϦιʔε͸

    UFSSBGPSNJNQPSU
    w Ͳ͏ͯ͠΋੔߹ੑ߹Θͳ͍ͱ͖͸

    UGTUBUFΛ௚઀मਖ਼

    View full-size slide

  31. Կ͕ྑ͘ͳͬͨʁ
    w ىಈ؅ཧͷू໿͕Ͱ͖ͨ
    w UFSSBGPSNͷϑΝΠϧΛݟΕ͹ɺߏ੒ཁૉ͕Θ͔Δঢ়ଶ
    w ࡞੒΋εέʔϧ΋UFSSBGPSNBQQMZͰPL
    w ߏ੒ॱং΋ࢦఆ EFQFOET@PO

    w WBSTͷ੾Γସ͑Ͱɺॖখɺ֦ு΋༰қ

    View full-size slide

  32. ॖখ༻
    ##########################
    ## Aurora InstanceClass ##
    ##########################
    rds_master_class = “db.r3.2xlarge"
    #################
    ## Autoscaling ##
    #################
    desired_capacity = “10”
    ֦ு༻
    ##########################
    ## Aurora InstanceClass ##
    ##########################
    rds_master_class = “db.t2.medium”
    #################
    ## Autoscaling ##
    #################
    desired_capacity = “0”
    ֦ுॖখ͸WBSTϑΝΠϧͷ

    ੾Γସ͑ͰରԠ

    View full-size slide

  33. # ద༻͢ΔvarsΛηοτ
    vars_file=vars_file/${OPTARG}_value.tfvars
    # tfstateόοΫΞοϓ
    if [ ${option} == "apply" ]; then
    terraform state pull > backup/terraform.tfstate_backup_`date +"%Y-%m-%d-%H-%M-
    %S"`
    fi
    # terraform࣮ߦ (plan or apply)
    terraform ${tf_option} -var-file=${vars_file}
    ࣮ߦ༻εΫϦϓτ

    View full-size slide

  34. w ઃఆ͕ڞ༗Ͱ͖͓ͯΓ
    w มߋ΋WBSTͷ੾Γସ͑ͰPL
    w ͱͳͬͨΒɺ͋ͱ͸୭͕࣮ߦͯ͠
    ΋Ұॹ

    View full-size slide

  35. after
    ʊਓਓਓਓਓਓਓਓਓʊ
    ʼɹTMBDLҰൃʂʻ
    ʉ:?:?:?:?:?:?ʉ
    hoge
    hoge
    hoge

    View full-size slide

  36. after hoge
    hoge
    hoge
    w εέʔϧͷݖݶΛΞϓϦνʔϜ΁Ҡৡ
    w Ͳ͏͍͏؀ڥͰࢼݧ͍ͯ͠Δ͔ͷ

    ֬ೝ΋༰қ
    w ϘτϧωοΫՕॴͷมߋ΋

    WBSTͷ஋Λ͍͡Δ͚ͩͰมߋͰ͖Δ
    εϐʔυײͷ͋ΔରԠΛ

    ߦ͏͜ͱ͕Ͱ͖Δ

    View full-size slide

  37. $BTF
    ৽ن؀ڥߏங

    View full-size slide

  38. ৽نαʔϏεͷ؀ڥߏஙͷྲྀΕ
    ͦ Ζ ͦ Ζ α ʔ ό ͱ ૄ ௨ ͯ͠ ֬ ೝ ͠ ͨ ͍
    ͍ ͭ · Ͱ ʹ ༻ ҙ Ͱ ͖ · ͢ ͔ ʂ ʁ
    Ξ Χ ΢ ϯ τ ͔ Β ͷ ༻ ҙ ͩ ͔ Β ɺ
    ͋ Ε ͍ Εͯ ͜ Ε ͍ Εͯ ͋ ͹ ͹ ͹ ʜ
    ೔ ͙ Β ͍Ͱ ʜ
    ͦ͘ ͓ ͦ ʂ
    ʢ ྃ ղ Ͱ ͢ ʂ
    ΑΖ ͠ ͘ ͓ ئ ͍ ͠ · ͢ ʂ ʣ

    View full-size slide

  39. before
    SPPUΞΧ΢ϯτͷ෧ҹ
    $POTPMJEBUFE#JMMJOHઃఆ
    *".6TFSͷ࡞੒ɺ(SPVQઃఆɺTXJUDIઃఆ
    $MPVE5SBJMMPH༻4όέοτͷઃఆ
    ωοτϫʔΫઃఆɺ؂ࢹ༻ϙʔτ։͚
    FUD

    View full-size slide

  40. ͦΜͳઃఆ΋

    View full-size slide

  41. Կ͕ྑ͘ͳͬͨʁ
    w ΞΧ΢ϯτࣗମͷઃఆ؅ཧ
    w ແҙࣝʹαʔό؀ڥ ΞϓϦέʔγϣϯؔ࿈
    ͷ
    ؅ཧͱͯ͠࢖༻͍͕ͯͨ͠ɺ

    "84ͷ΄΅શͯͷαʔϏε໢ཏ͍ͯ͠ΔͷͰɺ

    $MPVE5SBJM͔Β*".·Ͱɺ

    ॳظઃఆΛશͯςϯϓϨԽͰ͖ͨɻ

    View full-size slide

  42. BQQMJCPUͷUFSSBGPSN

    ϑΥϧμߏ੒
    • 00_base
    • ΞΧ΢ϯτ࡞੒࣌ʹ࡞੒͢ΔαʔϏε܈
    • CloudTrail / Route53 / keypair / S3(awsؔ࿈ͷϩά༻) / IAMUser
    • 01_common / 02_dev / 03_stg / 04_prd
    • ֤؀ڥ໊ɻιʔτ༻ʹ൪߸ৼ͍ͬͯΔ
    • workspace͸ఘΊ·ͨ͠
    • 99_modules
    • ֤؀ڥ͕ڞ௨Ͱ࢖༻͢Δઃఆ
    • ྫ: VPC Network

    View full-size slide

  43. (JUMBCͰ
    CBTFSFQPTJUPSZ؅ཧ

    View full-size slide

  44. after
    SPPUΞΧ΢ϯτͷ෧ҹ
    $POTPMJEBUFE#JMMJOHઃఆ
    UFSSBGPSN༻*".6TFS࡞੒
    UFSSBGPSN࣮ߦ
    ͰPL

    View full-size slide

  45. hoge
    ઃఆมߋ΋.3Ͱ
    ؅ཧͰ͖Δ

    View full-size slide

  46. ·ͱΊ
    w 1BDLFSUFSSBGPSNͰ؅ཧ͢Δ͜ͱͰ

    ؅ཧ͠΍͘͢ɺڞ༗ՄೳͳΠϯϑϥ΁
    w ߏங෦෼͕ૄ݁߹ʹͳΔ͜ͱͰ

    JOQVUPVUQVUͷ૊Έ߹Θ͕ͤ༰қ
    w ఆܕ࡞ۀ͸ςϯϓϨԽେࣄ

    View full-size slide

  47. ͝੩ௌ
    ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide