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

A Modernization of Legacy Django Based Applicat...

A Modernization of Legacy Django Based Applications

レガシーDjangoアプリケーションの現代化

DjangoCongress JP 2018 at Cybozu, Inc.

HayaoSuzuki

May 19, 2018
Tweet

More Decks by HayaoSuzuki

Other Decks in Programming

Transcript

  1. ϨΨγʔ Django ΞϓϦέʔγϣϯͷݱ୅Խ A Modernization of Legacy Django Based Applications

    Hayao Suzuki DjangoCongress JP 2018 at Cybozu, Inc. May 19, 2018
  2. Contents 1 ֓ཁ 2 ΞϓϦέʔγϣϯલ࢙ 3 Twelve-Factor App 4 Django

    ͷΞοϓσʔτ 5 ΞϓϦέʔγϣϯͷݱ୅Խ 6 ·ͱΊ 2 / 29
  3. Abstract ൃදͷ֓ཁ Ĺ Django 1.7 Ͱॻ͔ΕͨΞϓϦέʔγϣϯ͕ݱ໾ͩͬͨ Ĺ ࣍ظϦϦʔεʹ޲͚ͯ Django 1.11

    ʹόʔδϣϯΞοϓͨ͠ Ĺ Twelve-Factor App ͷࢥ૝ΛϕʔεʹΞϓϦέʔγϣϯͷݱ୅ԽΛ࣮ࢪ ͨ͠ ୭޲͚ͷൃදʁ Ĺ ݹ͍ΞϓϦέʔγϣϯΛվળ͢Δඞཁ͕͋ΓɺܦݧஊΛฉ͖͍ͨํ Ĺ Twelve-Factor App ͷࢥ૝ͷ࣮ફྫ͕஌Γ͍ͨํ ஫ҙ ࣮ࡍͷडୗҊ݅ͷࣄྫΛϕʔεʹ࠶ߏ੒ͨ͠΋ͷͰ͢ɻ 3 / 29
  4. Who Am I? ͓લ୭Α Name Hayao Suzuki (@CardinalXaro) Blog http://xaro.hatenablog.jp

    Major Mathematics (Combinatorics, Number Theory) Work Python Programmer at iRidge, Inc. ࠪಡͨ͠ຊʢൈਮʣ Ĺ Effective Python (O’Reilly Japan) Ĺ ΞϧΰϦζϜ ΫΠοΫϦϑΝϨϯε ୈ 2 ൛ (O’Reilly Japan) Ĺ ͢Β͢ΒΘ͔Δ Python ʢᠳӭࣾʣ Ĺ Head First Python ୈ 2 ൛ (O’Reilly Japan) Ĺ Python σʔλαΠΤϯεϋϯυϒοΫ (O’Reilly Japan) 4 / 29
  5. ΞϓϦέʔγϣϯલ࢙ ΞϓϦέʔγϣϯͷ֓ཁ Ĺ εϚʔτϑΥϯΞϓϦͷόοΫΤϯυαʔόʔ Ĺ ෳ਺ͷ Django ΞϓϦέʔγϣϯ͔Βߏ੒͞ΕΔ Ĺ BtoC

    ޲͚ʢΞΫηεͷ೾͕͋Δʣ Ĺ 2017 ೥ 3 ݄ϦϦʔε Ĺ Django 1.7 Ĺ AWS EC2 ্ʹߏ੒ ϓϩδΣΫτ๊͕͍͑ͯͨ໰୊ Ĺ Python 2.7.6ʢUbuntu 14.04 ෇ଐʂʣΛͦͷ··࢖͍ଓ͚͍ͯͨ Ĺ Django 1.7 ͸ 2015 ೥Ͱαϙʔτ͕੾Ε͍ͯͨ Ĺ Management ίϚϯυ͸αʔόʔϩάΠϯޙʹखಈͰ࣮ߦ Ĺ ϩάௐࠪ΋αʔόʔϩάΠϯ͕ඞཁͱͳΔ Ĺ 2 ୆ߏ੒ͰඞཁʹԠͯ͡αʔόʔͷεέʔϧΞοϓɾμ΢ϯ͕ඞཁ 5 / 29
  6. ΞϓϦέʔγϣϯલ࢙ ࣍ظେܕΞοϓσʔτ Ĺ 2017 ೥ 8 ݄ελʔτ ! ΋͏͙͢ϦϦʔε Ĺ

    αʔόʔଆϝϯόʔ͸ 4 ਓ Ĺ ௕೥๊͍͑ͯͨ໰୊Λղܾ͠Α͏ʂ ͳ͕͍ͨͼ͕͸͡·Δ... 6 / 29
  7. Twelve-Factor App ֓ཁ Ĺ ίʔυϕʔε Ĺ ґଘؔ܎ Ĺ ઃఆ Ĺ

    όοΫΤϯυαʔϏε Ĺ ϏϧυɺϦϦʔεɺ࣮ߦ Ĺ ϓϩηε Ĺ ϙʔτόΠϯσΟϯά Ĺ ฒߦੑ Ĺ ഇغ༰қੑ Ĺ ։ൃ/ຊ൪Ұக Ĺ ϩά Ĺ ؅ཧϓϩηε 8 / 29
  8. Twelve-Factor App ຬ͍ͨͯͨ͠ཁૉ Ĺ ίʔυϕʔε (ηϧϑϗεςΟϯάͷ GitLab Ͱ؅ཧʣ Ĺ όοΫΤϯυαʔϏεʢAWS

    RDS ͳͲʣ Ĺ ϓϩηεʢγϯάϧϓϩηεʣ Ĺ ϙʔτόΠϯσΟϯάʢNginx ͱͷ૊Έ߹Θͤʣ ଍Γͳ͍ཁૉ Ĺ ઃఆʢ؀ڥ͝ͱʹϑΝΠϧ࡞੒ɺϋʔυίʔσΟϯάʣ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦʢ෼཭Ͱ͖͍ͯͳ͍ʣ Ĺ ґଘؔ܎ʢPython ͸ requirements.txt ͕ͩɺOS ͸...ʣ Ĺ ฒߦੑʢ2 ୆ߏ੒ɺεέʔϧΞοϓͷΈՄೳʣ Ĺ ഇغ༰қੑʢഇغͰ͖ͳ͍ʣ Ĺ ։ൃ/ຊ൪Ұகʢαʔόʔ͕ผͳͷͰҰக͠ͳ͍ʣ Ĺ ϩάʢαʔόʔʹอଘɺΤϥʔ͸ϝʔϧ௨஌ɺόονܥ͸ CRON Ͱ Slack ʹ௨஌ʣ Ĺ ؅ཧϓϩηεʢόοναʔόʔͰ࣮ߦɺSSH ϩάΠϯඞཁʣ 9 / 29
  9. Django ͷΞοϓσʔτ ຐ๏ͷΑ͏ͳํ๏͸ଘࡏ͠ͳ͍ Ĺ Django ͷυΩϡϝϯτʹԊͬͯॗʑͱ΍Δ΄͔ͳ͍ Ξοϓσʔτखॱ Ĺ ϢχοτςετΛॻ͘ʢॏཁʣ Ĺ

    Django ͷόʔδϣϯΛ 1 ্ͭͣͭ͛Δ Ĺ python -Wall manage.py test Ͱςετ࣮ߦɺܯࠂΛ 1 ͭͣͭফԽ͢Δ Ĺ Django 1.11 ʹ౸ୡ͢Δ·Ͱ܁Γฦ͢ ࣮ྫ: ৽نࢀըऀʹϢχοτςετ͸ॻ͚Δͷ͔ Ĺ ແཧͰͨ͠ɻೖࣾ 1 ϲ݄ͷਓһͰ͸Ϣχοτςετ͸ॻ͚ͳ͔ͬͨɻ Ĺ ·ͣ͸ϓϩδΣΫτͷ஌ࣝΛਂΊΔ͜ͱ͔Β࢝ΊΔɻ 10 / 29
  10. Django ͷΞοϓσʔτ Django ϛυϧ΢ΣΞͷΞοϓσʔτ Ĺ MIDDLEWARE_CLASSES ͔Β MIDDLEWARE ΁มߋ͢Δ͚ͩͰ͸μϝ Ĺ

    ϛυϧ΢ΣΞΛࣗ࡞͍ͯͨ͠৔߹͸खΛՃ͑Δඞཁ͕͋Δ ࣮ྫ: αʔυύʔςΟϛυϧ΢ΣΞ͕৽ܗࣜະରԠ Ĺ ݪҼڀ໌ʹ͕͔͔࣌ؒͬͨ Ĺ ϛυϧ΢ΣΞ͕ఏڙ͍ͯ͠Δػೳ͸ Django ͕ެࣜʹఏڙ͢ΔΑ͏ʹͳͬͨɻ ࣮ྫ: ࣗ࡞ϛυϧ΢ΣΞͷΞοϓσʔτ Ĺ ެࣜυΩϡϝϯτʹҠߦखॱ͋Γ 11 / 29
  11. SQL ΫΤϦνϡʔχϯά ڪාͷ N + 1 ໰୊ Ĺ ෛՙςετͰൃ֮ʂ Ĺ

    Django ORM ͸؆୯ɺͦ͏ࢥ͍ͬͯͨ࣌ظ΋͋Γ·ͨ͠ɻ SQL ΫΤϦνϡʔχϯά Ĺ ෛՙςετͰଌఆ͢Δ Ĺ AWS X-Ray ͷτϨʔεσʔλΛݩʹ໰୊ՕॴΛಛఆ͢Δ Ĺ Django ͷυΩϡϝϯτΛಡΈͭͭޮ཰ͷΑ͍ΫΤϦΛ࡞Δ ࣮ྫ: ී௨ɺdjango-debug-toolbar ͡Όͳ͍ͷʁ Ĺ API αʔόʔͩͱ django-debug-toolbar ͸࢖͍ͮΒ͔ͬͨ... Ĺ AWS X-Ray ͳΒ͹։ൃ؀ڥͰ΋ຊ൪؀ڥͰ΋τϨʔεͰ͖Δ ࣮ྫ: AWS X-Ray ͷಋೖ Ĺ Django 1.10 Ҏ্Ͱ͋Δཧ༝: ৽ܗࣜͰ࣮૷͞Ε͍͔ͯͨΒɻ 13 / 29
  12. ෛ࠴Λ෷͏ϦϑΝΫλϦϯά ຐ๏ͷΑ͏ͳํ๏͸ଘࡏ͠ͳ͍ʢ2 ճ໨ʣ Ĺ ϢχοτςετΛॻ͍ͯෆಈ఺Λ࡞͔ͬͯΒɻ Ξοϓσʔτखॱ Ĺ ϢχοτςετΛॻ͘ʢॏཁʣ ʢ2 ճ໨ʣ

    Ĺ ʮ͋ͬ͜Ε͸ʯͱ͍͏෦෼ΛϦϑΝΫλϦϯά Ĺ python -Wall manage.py test Ͱςετ࣮ߦ Ĺ ݟ௨͕͠Α͘ͳΔ·Ͱܧଓ ࣮ྫ: Django ͷ MVT Ϟσϧʹج͍ͮͨ໾ׂͷ෼཭ Ĺ ৭ʑͱؤு͍ͬͯΔ View ͷॲཧΛ Model ʹҠৡ͢Δ Ĺ ༗ೳ͗͢Δ utils.py ͷػೳΛ Model ʹҠৡ͢Δ Ĺ ॏෳͨ͠ Model ΛجఈΫϥεͰ੔ཧ͢Δ Ĺ σίϨʔλͰڞ௨ॲཧΛҰݩԽ͢Δ 15 / 29
  13. ؀ڥม਺ʹΑΔઃఆ؅ཧ III. ઃఆ Ĺ ઃఆΛίʔυ͔Βݫີʹ෼཭͢Δ Ĺ ઃఆΛ؀ڥม਺ʹ֨ೲ͢Δ ࣮ྫɿdjango-environ ʹΑΔ؀ڥม਺ͷ੔ཧ Ĺ

    https://github.com/joke2k/django-environ Ĺ settings.py ʹϋʔυίʔσΟϯά͍ͯͨ͠ઃఆΛ؀ڥม਺΁Ҡৡ Ĺ develop.env ϑΝΠϧͰϩʔΧϧͰ΋؀ڥม਺Λѻ͑ΔΑ͏ʹ͢Δɻ 16 / 29
  14. ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ VIII. ฒߦੑ Ĺ ϓϩηε͸ୈҰڃࢢຽͰ͋Δ IX. ഇغ༰қੑ Ĺ

    ଈ࠲ʹىಈɾऴྃ͢Δ͜ͱ͕Ͱ͖Δ Ĺ ىಈ࣌ؒΛ࠷খԽ͢Δ X. ։ൃ/ຊ൪Ұக Ĺ ܧଓతσϓϩΠ͠΍͍͢Α͏։ൃ؀ڥͱຊ൪؀ڥͷΪϟοϓΛখ͘͞อͭ 18 / 29
  15. ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ Ĺ ഇغ༰қੑɺ։ൃ/ຊ൪ҰகΛ࣮ݱ͢Δ༗ྗͳख๏ ࣮ྫɿVagrant ͔Β

    Docker ΁ Ĺ ։ൃɿVagrantɺຊ൪ɿAWS EC2 ͔Βͷ୤٫ Ĺ ຊ൪؀ڥΛࢀߟʹ Dockerfile Λ૊ΈཱͯΔ Ĺ ࠷ॳ͸։ൃ؀ڥ͔Βར༻։࢝ɺͦͯ͠ AWS ECS ΁ 19 / 29
  16. ܧଓతΠϯςάϨʔγϣϯɺܧଓతσϓϩΠ V. ϏϧυɺϦϦʔεɺ࣮ߦ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦͷ 3 ͭͷεςʔδΛݫີʹ෼཭͢Δ ࣮ྫɿGitLab CI Ĺ

    ैདྷ͸ίϛοτ࣌ʹϢχοτςετΛ࣮ߦ͢Δ͚ͩ Ĺ ίϛοτ࣌ʹςετɺϚʔδޙʹ Docker ΠϝʔδϏϧυɺσϓϩΠΛ࣮ࢪ Ĺ σϓϩΠ͸ AWS ECR/ECS ΁ߦ͍ɺ࣮ߦ͸ ECS ΁Ҡৡ 21 / 29
  17. ϩΪϯάͱ௨஌ XI. ϩά Ĺ ϩάΛΠϕϯτετϦʔϜͱͯ͠ѻ͏ ࣮ྫɿDocker ϩάυϥΠόʔͱ AWS CloudWatch Ĺ

    ΞϓϦέʔγϣϯ͔Βͷϩάग़ྗ͸͢΂ͯඪ४ग़ྗ or ඪ४Τϥʔ΁ྲྀ͢ Ĺ ϩά͸͢΂ͯ AWS CloudWatch Logs ΁ૹΔ Ĺ Docker ϩάυϥΠόʔʹઃఆ͢Δ͚ͩͳͷͰ؆୯ Ĺ CloudWatch Alarm Λ૊Έ߹ΘͤͯΤϥʔ௨஌ 23 / 29
  18. όονλεΫͷίϚϯυԽ XII. ؅ཧϓϩηε Ĺ ؅ཧλεΫΛ 1 ճݶΓͷϓϩηεͱ࣮ͯ͠ߦ͢Δ ࣮ྫɿDjango ίϚϯυͱ AWS

    ECS λεΫ Ĺ Django ίϚϯυ୯ҐͰ ECS λεΫΛ࡞Δ Ĺ ΤϯδχΞҎ֎Ͱ΋όονૢ࡞͕ GUI ্Ͱ࣮ݱͰ͖Δ Ĺ Django ίϚϯυͱ ECS λεΫͷ૬ੑ͸ྑ͍ 25 / 29
  19. ؀ڥม਺஍ࠈ ·͔͞ͷ࣌ͷ؀ڥม਺஍ࠈʂ Ĺ 1 ΞϓϦέʔγϣϯ͋ͨΓ਺ेݸͷ؀ڥม਺ Ĺ ։ൃ؀ڥɺݕূ؀ڥɺຊ൪؀ڥɺCI/CD ޲͚ Ĺ ෳ਺ͷΞϓϦέʔγϣϯ͔ΒͳΔϓϩδΣΫτ

    ؀ڥม਺஍ࠈΛͲ͏੾Γൈ͚Δ͔ Ĺ ʮϓϥΠϕʔτϨϙδτϦ͔ͩΒ҆৺ʯઆ Ĺ AWS S3 ʹผ్อଘͯ҆͠શʹऔΓग़͢ Ĺ ؀ڥม਺ઐ༻ͷϨϙδτϦΛ࡞੒ͯ͠ SSH or HTTPS Ͱ҆શʹऔΓग़͢ Ĺ AWS ͷΫϨσϯγϟϧ͸ IAM Role Ͱ ECS λεΫʹׂΓ౰ͯͯݮΒ͢ ࣮ྫɿAWS Secrets Manager Ĺ ౉Γʹધ Ĺ ؀ڥม਺Λ؀ڥ͝ͱʹผʹͯ҆͠શʹอଘ͢Δ Ĺ Secrets Manager ͷݤ͸ผ్؅ཧ͢Δඞཁ͕͋Δ 27 / 29
  20. Twelve-Factor App ຬ͍ͨͯͨ͠ཁૉ Ĺ ίʔυϕʔε (ηϧϑϗεςΟϯάͷ GitLab Ͱ؅ཧʣ Ĺ όοΫΤϯυαʔϏεʢAWS

    RDS ͳͲʣ Ĺ ϓϩηεʢγϯάϧϓϩηεʣ Ĺ ϙʔτόΠϯσΟϯάʢNginx ͱͷ૊Έ߹Θͤʣ վળͨ͠ཁૉ Ĺ ઃఆʢ؀ڥม਺ʹΑΔ෼཭ʣ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦʢGitLab CI ʹΑΔ CI/CDʣ Ĺ ґଘؔ܎ʢrequirements.txt ͱ dockerfileʣ Ĺ ฒߦੑʢAWS ECS ʹΑΔΦʔέετϨʔγϣϯʣ Ĺ ഇغ༰қੑʢDocker ίϯςφʣ Ĺ ։ൃ/ຊ൪ҰகʢDockerʣ Ĺ ϩάʢAWS CloudWatchʣ Ĺ ؅ཧϓϩηεʢAWS ECS ͷλεΫʣ 28 / 29
  21. ·ͱΊ ࣍ظϦϦʔεͷ໨ඪ Ĺ Python 3 ΁ͷҠߦ ˍ Django 2.2 LTS

    ΁ͷҠߦ ·ͱΊ Ĺ Django ͷΞοϓσʔτʹຐ๏͸ଘࡏ͠ͳ͍ɻ஍ಓͳऔΓ૊Έ͕ॏཁɻ Ĺ ݹ͍ΞϓϦέʔγϣϯͰ΋ Django ϕʔεͳΒ͹վળ͸ߦ͍΍͍͢ɻ Ĺ طଘͷΫϥ΢υͷΤίγεςϜʹ৐͔ͬΔͱԿ͔ͱҠৡͰָ͖ͯʹͳΔɻ Ĺ ؀ڥม਺ͷ؅ཧʹ͸ඞͣ௚໘͢Δɻඋ͑Α͏ɻ 29 / 29