Save 37% off PRO during our Black Friday Sale! »

EC2 & 手動デプロイから ECS& CodePipeline に変えてみた

5379fb7cef39d8915ffcae2f41e3ff91?s=47 IshinShibata
July 13, 2021
410

EC2 & 手動デプロイから ECS& CodePipeline に変えてみた

5379fb7cef39d8915ffcae2f41e3ff91?s=128

IshinShibata

July 13, 2021
Tweet

Transcript

  1. &$खಈσϓϩΠ͔Β &$4ˍ $PEF1JQFMJOFʹม͑ͯΈͨ גࣜձࣾ".%MBC ࣲాҡ৽

  2. ࣗݾ঺հ ࣲాҡ৽ͱਃ͠·͢ɻ ஍ݩͷதখاۀʹͯӦۀ৬ʢ̑೥ʣ ˣ גࣜձࣾώτΫηʢΤϯδχΞͱͯ͠స৬ʣ ˣ גࣜձࣾ".%MBC ݱࡏ ͓স͍ΤϯδχΞͱͯ͠஍ҐΛཱ͍֬ͨ͠ɻɻ

  3. ΞδΣϯμ ❏ ܦҢ ❏ &$4ͷ֓ཁ ❏ &$4࡞੒·Ͱͷ͋Ε͜Ε ❏ ٧·ͬͨͱ͜Ζᶃ ❏

    ٧·ͬͨͱ͜Ζᶄ ❏ $PEFQJQFMJOFಋೖ ❏ ·ͱΊ d&$खಈσϓϩΠ͔Β &$4ˍ $PEF1JQFMJOFʹม͑ͯΈͨ d
  4. ܦҢ

  5. ౎౓TTIͰೖͬͯखಈͰૢ࡞͸ةݥͩ͠ɺԿΑΓखؒɻɻɻ &$4৮ͬͯΈ͍ͨʂ ͦΖͦΖखಈσϓϩΠ͔Β͞ΑͳΒ͍ͨ͠ɻɻ

  6. 何よりイケてない気がする!!

  7. ݱঢ়ͷ௒ΞφϩάσϓϩΠखॱ TTIͰFDʹೖΔ HJUQVMMPSJHJONBTUFS ECʹมߋ͕͋Ε͹ɺNJHSBUF EPDLFSJNBHFΛCVJME EPDLFSDPNQPTFVQ

  8. None
  9. &$4ͷ֓ཁ

  10. "84্ͰEPDLFSίϯςφΛ؆୯ʹ࣮ߦͰ͖ΔαʔϏε λΠϓ͸&$ͱ'"3("5& &$ จࣈ௨ΓFDͷதʹίϯςφΛݐͯΔ TTIͰೖͬͯԿ͔ૢ࡞͍ͨ࣌͠ʹศརɻ GBSHBUF FDͷ؅ཧΛ͠ͳͯ͘΋͍͍ εέʔϧ͕༰қ ECS

  11. ͜ΕΛ"84্Ͱ΍Δͷ͕ɺ&$4 ϩʔΧϧͰEPDLFSΛىಈ͢ΔྲྀΕͱͯ͠ɻɻ  %PDLFSGJMFͷ༻ҙ  EPDLFSCVJMEUOHJOYGEPDLFSOHJOY  EPDLFSSVOOHJOY

  12. UBTLEFGJOJUJPO TFSWJDF 5BTL 5BTL DMVTUFS &$4 EPDLFSDPNQPTFZNMΈ͍ͨͳ΋ͷ ࢖༻͢ΔEPDLFSJNBHF͸ʁ QPSUͷࢦఆ ؀ڥม਺ͷઃఆ

    ϘϦϡʔϜͷઃఆ UBTLEFGJOJUJPOʁ
  13. UBTLEFGJOJUJPO TFSWJDF 5BTL 5BTL DMVTUFS &$4 &$4Λ࡞੒͢Δ্ͰશମΛ·ͱΊΔ ശΈ͍ͨͳ΋ͷ DMVTUFSʁ

  14. 5BTLΛ·ͱΊΔ΋ͷ UBTLEFGJOJUJPOΛ΋ͱʹ5BTLΛ্ཱͪ͛Δ ࢖༻͢ΔλεΫͷ਺͸ʁ ϩʔυόϥϯαʔͷ࢖༻༗ແ "1*ɺ$MJFOUΈ͍ͨͳΠϝʔδ UBTLEFGJOJUJPO TFSWJDF 5BTL 5BTL DMVTUFS

    &$4 TFSWJDFʁ
  15. UBTLEFGJOJUJPO TFSWJDF 5BTL 5BTL DMVTUFS &$4 EPDLFSίϯςφΛ·ͱΊͨ΋ͷ &$λΠϓͰ͋Ε͹ɺ5BTL&$ͱ͍͏ ೝࣝͰྑ͍͔΋͠Εͳ͍ 5BTLʁ

  16. &$4࡞੒·Ͱͷ͋Ε͜Ε

  17. ˔ DMVTUFS࡞੒ &$4ϓϩδΣΫτͷശΈ͍ͨͳ΋ͷ ˔ UBTLEFGJOJUJPOͷ࡞੒ ίϯςφͷىಈํ๏ EPDLFSDPNQPTFZNMΈ͍ͨͳ΋ͷ ˔ TFSWJDFͷ࡞੒ λεΫΛ·ͱΊΔ΋ͷ

    ˔ EPDLFSJNBHFͷొ࿥
  18. EPDLFSIVCͱಉ͡Α͏ͳαʔϏεͰJNBHFΛొ࿥Ͱ͖Δ ΋ͪΖΜEPDLFSQVMM΋Ͱ͖Δ l BXTFDSHFUMPHJOQBTTXPSESFHJPOࢦఆϦʔδϣϯ cEPDLFSMPHJOVTFSOBNF"84 QBTTXPSETUEJOBXTΞΧ΢ϯτELSFDSࢦఆϦʔδϣϯBNB[POBXTDPN l EPDLFSCVJMEUOHJOY l EPDLFSUBHOHJOYMBUFTUBXTΞΧ΢ϯτELSFDSࢦఆϦʔδϣ

    ϯBNB[POBXTDPNOHJOYMBUFTU l EPDLFSQVTIBXTΞΧ΢ϯτELSFDSBQOPSUIFBTUBNB[POBXTDPNOHJOYMBUFTU &$3ʹEPDLFSΠϝʔδΛొ࿥
  19. ٧·ͬͨͱ͜Ζᶃ

  20. BXTDMJΛීஈ࢖Θͳ͍͔ΒɺJBNͷ੾Γସ͑ํͰ೰Ήɻɻɻ BXTFDSHFUMPHJOQBTTXPSESFHJPOࢦఆϦʔδϣϯ cEPDLFSMPHJOVTFSOBNF"84 QBTTXPSETUEJOBXTΞΧ΢ϯτELSFDSࢦఆϦʔδϣϯBNB[POBXTDPN &$3ʹQVTI͢ΔBXTΞΧ΢ϯτͱผͷΞΧ΢ϯτͰϩάΠϯͨ͠৔߹ɺQVTI͕Ͱ͖ͳ͍ɻ BXTΞΧ΢ϯτ͕ෳ਺͋Δ৔߹͸FYQPSU"84@%&'"6-5@130'*-&QSPGJMFOBNF Ͱ࢖༻͢ΔΞΧ΢ϯτΛ੾Γସ͔͑ͯΒɺ&$3ʹϩάΠϯ͢Δ QVTI͕Ͱ͖ͳ͍ɻɻɻɻɻ

  21. ٧·ͬͨͱ͜Ζᶄ

  22. nginx Rails ιέοτ௨৴Λߦ͏ʹ͸ 3BJMTͷιέοτϑΝΠϧΛڞ༗͢Δඞཁ͕͋Δ

  23. volume task GBSHBUF OHJOYίϯςφ͔Β ϘϦϡʔϜιʔεΛSBJMTίϯςφʹࢦఆ &$ ϘϦϡʔϜΛ࡞੒͠ɺίϯςφಉ࢜Ͱڞ༗͢Δ ソケット通信 ソケット通信 WPMVNFͷڞ༗

    socketファイル &'4Λ࢖͑͹ଞʹ΍Γํ͕͋Δ͔΋ɻɻɻ volume task ソケット通信
  24. OHJOYίϯςφ͔ΒϘϦϡʔϜΛಡΈࠐΉ railsのDockerfileにて VOLUMEを作成 OHJOYɺSBJMTίϯςφ͔Βڞ༗ͷϘϦϡʔϜΛϚ΢ϯτ͢Δ ボリュームの作成 &$̎λΠϓͷ৔߹ 'BSHBUFͷ৔߹

  25. $PEFQJQFMJOFಋೖ

  26. DPEFQJQFMJOFͰ$*$%ͷύΠϓΛ࡞ΓɺύΠϓ Λͦͷ΄͔ͷαʔϏεͰͭͳ͘Πϝʔδ

  27. ࠓճ࢖༻ͨ͠αʔϏε

  28. ソース ビルド テスト デプロイ

  29. Pipeline Transition Stage Action Action Stage Action Action Action 1JQFMJOF

    $PEFQJQFMJOFͷ࠷্Ґ֓೦ɻ֤ϓϩηε Λ·ͱΊͨ΋ͷ 4UBHF 1JQFMJOFͷҰ࿈ͷϓϩηεɻϏϧυɺςε τɺσϓϩΠ͕͜Εʹ͋ͨΔ "DUJPO 4UBHF্ͷ۩ମతͳλεΫɻ৽͍͠ίϯ ςφͷλεΫʹஔ͖׵͑Δͱ͔ɺ%PDLFS CVJMEɺ%PDLFSQVTIͳͲ 5SBOTJUJPO ࣍ͷεςʔδʹҠΔॲཧ
  30. Action Category(アクション内容) Action Providers(どのサービスが行うか) Souce Amazon S3, Amazon ECR, CodeCommit,

    Github, Bitbucket Build CodeBuild, Jenkins Test CodeBuild, Jenkins Deploy Amazon S3, Amazon ECS, CodeDeploy
  31. Pipeline Souce Deploy Build docker build, ECRへの docker-push git pushをトリガーにpipeline

    の実行し、ソースを取得 古いコンテナを新しいコンテ に差し替える Transition Transition buildspec.yml imagedefinitions .json version: 0.2 phases: pre_build: commands: - $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email) 環境変数定義ができる build: commands: - docker build -t $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/rails:latest . post_build: commands: - docker push $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/rails:latest - echo Writing image definitions file... - printf '[{"name":"rails","imageUri":"%s"}]' - $AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/rails:latest > imagedefinitions.json artifacts: files: imagedefinitions.json [ {"name":"rails","imageUri":"AWS_ACCOUNT_ID.dkr.ecr.ap-northeast- 1.amazonaws.com/rails:latest"} ]
  32. None
  33. ·ͱΊ

  34. ˔ ӡ༻ίετ͕ܹݮͨ͠ɻ ˔ "84৮Δͷָ͕͘͠ͳΔɻ ˔ $PEFQJQFMJOF࢖͍΍͍͢ʂ ˔ جຊతͳ%PDLFSͷཧղେࣄʂ ͞ΑͳΒखಈσϓϩΠ $MPVE'PSNBUJPOͰߏஙͯ͠Έ͍ͨ

    ॳΊͯͷ$*$%͸$PEFQJQFMJOF͓͢͢Ί %PDLFSͷཧղ͕͋ͬͯͦ͜࢖༻Ͱ͖ΔαʔϏε
  35. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ

  36. $PEFγϦʔζͰ࢝ΊΔ͸͡Ίͯͷ$*$% IUUQTEFWDMBTTNFUIPEKQBSUJDMFTDJDEXJUIDPEFTFSJFTBUIJHPCBTIJBXT ϙʔτϑΥϦΦΛ&$4Ͱʂʳ3BJMTº/HJOYΞϓϦΛ'BSHBUFʹσϓϩΠ͢Δ·ͰΛஸೡʹઆ໌ͯ͠Έͨ 71$࡞੒ʙ$JSDMF$*ʹΑΔࣗಈσϓϩΠ·Ͱ લฤ IUUQTRJJUBDPNNBSVJUFNTFEBCBEFEBEC ʲ"84ೖ໳ʳ&$4 'BSHBUF ͱ&$3Ͱָʑίϯςφ͔Β)FMMP8PSME͠Α͏ʂ IUUQTXXXZPVUVCFDPNXBUDI

    W@'Y-QYHNP ॳ৺ऀͰ΋Ͱ͖Δʂ &$4º &$3º $JSDMF$*Ͱ3BJMTΞϓϦέʔγϣϯΛίϯςφσϓϩΠ IUUQTRJJUBDPNTBPOHUYJUFNTGEEC ࢀߟࢿྉ "84$PEF1JQFMJOFͰ $*$%ͷ࢓૊ΈΛߏங͓ͨ͠࿩ IUUQTCMPHTQBDFNBSLFUDPNDPEFDJDEDPEFQJQFMJOF "NB[PO&$$POUBJOFS4FSWJDF &$4 ͷ֓೦੔ཧ IUUQTRJJUBDPN/FX(ZVJUFNTFEFEBCEE