ECS/Fargateの活用事例(CakePHP編)

5059d3f370ad7e1f4d8de4be79ae1a2c?s=47 adachin
September 25, 2020

 ECS/Fargateの活用事例(CakePHP編)

https://connehito.connpass.com/event/187729/
【ランサーズ社合同勉強会】コネヒトマルシェオンライン「事業を支えるWeb開発」vol.2

5059d3f370ad7e1f4d8de4be79ae1a2c?s=128

adachin

September 25, 2020
Tweet

Transcript

  1. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ECS/Fargateͷ׆༻ࣄྫ (CakePHPฤ) ϥϯαʔζגࣜձࣾ SRE/҆ୡ ྋ ίωώτϚϧγΣΦϯϥΠϯ ʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2

  2. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ΞδΣϯμ  ɾࣗݾ঺հ ɾݱࡏऔΓ૊ΜͰ͍Δ͜ͱ ɾECS/FargateͷϝϦοτσϝϦοτ ɾ։ൃ؀ڥΛ࡞ΔϙΠϯτ ɾStg/ຊ൪؀ڥͷߏ੒ʹ͍ͭͯ ɾSSH/࡞ۀ༻ίϯςφʮdevopsʯʹ͍ͭͯ

    ɾCircleCIʹΑΔίϯςφσϓϩΠ ɾECS Scheduled TasksʹΑΔόον࣮૷ʹ͍ͭͯ ɾϩά/ίϯςφ؂ࢹʹ͍ͭͯ ɾ·ͱΊ
  3. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ࣗݾ঺հ

  4. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ࣗݾ঺հ  ɾ҆ୡ ྋ(adachin) ɾLancers SRE 2018~ ɾ@adachin0817

    ɾRVIRUS0817 ɾblog.adachin.me ɾwiki.adachin.me ɾOSS : ɾCakePHPͰݸਓαʔϏε։ൃத
  5. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ݱࡏऔΓ૊ΜͰ͍Δ͜ͱ

  6. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ݱࡏऔΓ૊ΜͰ͍Δ͜ͱ  ɾڈ೥ळ ɾάϧʔϓձࣾ(γΫϩϚʔέςΟϯά) ɾॳΊͯECS/FargateʹҠߦνϟϨϯδ ɾRuby on Rails

    ɾࠓ೥4݄ʹҠߦ׬ྃ ɾݱࡏ (5݄͔Β) ɾάϧʔϓձࣾ(Lancers Agency)ɹ ɾECS/FargateͰҠߦத(90%) ɹɾࠓճ͸ͪ͜Βͷ࿩Λ͠·͢!!
  7. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ϥϯαʔζΤʔδΣϯγʔͷ αʔϏεʹ͍ͭͯ

  8. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ϥϯαʔζΤʔδΣϯγʔͷαʔϏεʹ͍ͭͯ  ɾPROsheetɺLancers Agent ɾ͘͞ΒͷΫϥ΢υ ɹɾPHP5.4 CakePHP2.5.4 ɹɾApache,MySQL5.7

    ɾ։ൃ؀ڥ ɹɾVagrant,AnsibleͰ؅ཧ ɾίʔϙϨʔταΠτ ɹɾPHP5.5,MySQL5.7 ɾϝσΟΞαΠτ(WordPress) ɹɾPHP5.5,MySQL5.7
  9. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ։ൃ؀ڥΛ࡞Δ ϙΠϯτ

  10. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ։ൃ؀ڥΛ࡞ΔϙΠϯτ  ɾຊ൪؀ڥͱಉ౳ͷߏ੒Λ࠶ݱ ɾELB(H2O)ίϯςφ(ϦόʔεϓϩΩγ) ɾAppίϯςφ ɹɾPHP5.5-alpine/CakePHP2.5.4/Nginx ɾMySQLίϯςφ ɾSendGridίϯςφ

    ɾphpMyAdminίϯςφ ɾWordPressίϯςφ(PHP7.3-alpine/Nginx) ɾ֤ΞϓϦέʔγϣϯͷىಈ͸SupervisorͰ؅ཧ ɾcomposer install͸AppίϯςφͰϩάΠϯ࣌ʹ࣮ߦ ɹɾsupervisorctlͰΞϓϦΛrestart͢ΔΑ͏ʹ
  11. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 Stg/ຊ൪؀ڥͷ ߏ੒ʹ͍ͭͯ

  12. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 Stg/ຊ൪؀ڥͷߏ੒ʹ͍ͭͯ  ɾTerraformͰશϦιʔείʔυԽ ɾStg؀ڥ͸ۃྗίετΛ཈͑λεΫ਺͸1ͭ ɾdevopsίϯςφͷར༻ ɾDB͸RDS AuroraʹҠߦ ɾϝʔϧૹ৴͸Postfix͔ΒSendGridԽ

    ɾίʔϙϨʔταΠτ͸WordPressԽ ɾը૾౳S3Խ ɾWordPressͷը૾ ɹɾWP Offload Media LiteϓϥάΠϯͰS3ʹ ɾϩά͸CloudWatch Logs ɾόον͸ECS Scheduled Tasks ɾ಺෦؂ࠪ(ϩάͷू໿) ɹɾAmazon Kinesis ɾσϓϩΠ͸CircleCIͷΈͰ࣮૷ ɾ؂ࢹ͸Datadog
  13. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 SSH/࡞ۀ༻ίϯςφ ʮdevopsʯʹ͍ͭͯ

  14. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 SSH/࡞ۀ༻ίϯςφʮdevopsʯʹ͍ͭͯ  ɾAppʹ͸SSH͠ͳ͍ ɾSSH/࡞ۀ༻ίϯςφ(Appͱಉ౳) ɹɾdevopsͱ໋໊ ɾίϚϯυͰRDS΁઀ଓ ɹɾdump΍restore ɾaws

    cliͷར༻ ɾSSMͷSession Manager ΋͋Δ͕ ɹɾखܰʹ౿Έ୆͔ΒSSHΛ͍ͨͨ͠Ί ɾVPC PeeringͰϥϯαʔζͷωοτϫʔΫ͔ΒSSHϩάΠϯ
  15. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 CircleCIʹΑΔ ίϯςφσϓϩΠʹ͍ͭͯ

  16. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 APIܦ༝ͰͷStg؀ڥͷσϓϩΠ  ɾStg؀ڥͷσϓϩΠ ɹɾgithub flow ɹɾγΣϧεΫϦϓτ ɹɹɾ೚ҙͷϒϥϯνΛࢦఆ ɾૉૣ͘σϓϩΠ͕Մೳ

    ɹɾ։ൃޮ཰͕Ξοϓ ɾMasterϚʔδͰຊ൪؀ڥϦϦʔε ɾTerraform CI؀ڥΛ࣮૷ $ sh deploy.sh ԼهͷΑ͏ʹϒϥϯνΛࢦఆ࣮ͯ͠ߦ͍ͯͩ͘͠͞ɻ (ྫ) sh deploy.sh ϒϥϯν໊ $ sh deploy.sh fix-nginx { "number" : 74, "state" : "pending", "id" : "xxxxxx-xxxxx-xxxxx-xxx-xxxxxxxx", "created_at" : "2020-08-27T06:14:43.973Z" } ϒϥϯν໊/ fix-nginx ͷStgσϓϩΠΛ։࢝͠·ͨ͠!
  17. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 όονͷ࣮૷ʹ͍ͭͯ

  18. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ECS Scheduled TasksʹΑΔόον࣮૷ʹ͍ͭͯ  ɾࠓ·Ͱ ɹɾCron ɾݱࡏ ɹɾECS

    Scheduled Tasks ɹɹɾCloudWatchͷΠϕϯτϧʔϧ ɹɹɾTerraformԽ { "containerOverrides": [ { "name": "prosheet-app", "command": [ "php", "/var/www/prosheet/cake/app/Console/cake.php", "-app", "/var/www/prosheet/cake/app", "mail", "exec" ] } ]
  19. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ϩάʗίϯςφ؂ࢹ ʹ͍ͭͯ

  20. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ϩά/ίϯςφ؂ࢹʹ͍ͭͯ  ɾΞΫηε/ΞϓϦϩάɺRDS؂ࠪ/Τϥʔϩά ɹɾCloudWatch Logsʹ ɹɾࠓ·Ͱ ɹɹɾLambdaͰS3ʹγϯΫɹɹ ɹɹɾPythonͰ࣮૷͍ͯͨͨ͠Ίӡ༻ίετΞοϓ

    ɹɾࠓճ ɹɹɾAmazon Kinesis Data FirehoseͰS3ʹू໿ ɹɹɾTerraformԽ ɾ؂ࢹ ɹɾMackerel→DatadogʹҠߦ ɹɾλεΫఆٛʹdatadog-agentΛಈ࡞ͤ͞ΔΑ͏ʹ ɹɾcpu:10ɺmemory:256 TerraformԽ
  21. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ·ͱΊ

  22. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 ·ͱΊ  ɾImmutableͳαʔόʔͷӡ༻ ɹɾ҆ఆӡ༻ɺӡ༻ޮ཰ͷΞοϓ ɾϗετΠϯελϯεͷ؅ཧ͕ল͚Δ ɾλεΫ୯ҐͰࣗ༝ʹϦιʔεมߋՄೳ ɾॳΊͯίϯςφຊ൪ӡ༻͢Δʹ͸͔ͳΓϋϚΔ ɹɾϔϧενΣοΫ͕௨Βͳ͍ͱίϯςφ͕མͪΔ

    ɹɾLinuxྗ͕ࢼ͞ΕΔ ɹɾσϓϩΠͷ࣮૷͕େม͕ͩલͷܦݧ͕͋ͬͨ ɹɹɾ͙͢ʹ࣮૷Ͱ͖ͨ ɾσόοά͕ͮ͠Β͍ ɾλεΫͷىಈ଎౓͕஗Ί ɹɾόον࣮ߦ΋਺෼஗͘ͳΔ ɾྉ͕͓ۚߴΊ ɹɾEC2ΑΓׂߴʢ1ׂʙ2ׂʣ ɾࠓޙ ɹɾ·ͣ͸དྷ݄தʹҠߦ ɹɾPHP/CakePHPͷόʔδϣϯΞοϓ ɹɾίϯςφͷ੬ऑੑݕ஌ (Trivy) ɹɾDatadogͰϩά࿈ܞ ɹɾChatOpsʹΑΔσϓϩΠ ɹɾPHP CI؀ڥͷ࣮૷ ɹɾϥϯαʔζຊՈ΋ίϯςφҠߦʹνϟϨϯδ
  23. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2 SREٻΉʂʂ 

  24. 2020/9/25 ʲϥϯαʔζࣾ߹ಉษڧձʳίωώτϚϧγΣΦϯϥΠϯʮࣄۀΛࢧ͑ΔWeb։ൃʯvol.2