Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Modernization of Legacy Django Based Applicat...
Search
HayaoSuzuki
May 19, 2018
Programming
1
7.6k
A Modernization of Legacy Django Based Applications
レガシーDjangoアプリケーションの現代化
DjangoCongress JP 2018 at Cybozu, Inc.
HayaoSuzuki
May 19, 2018
Tweet
Share
More Decks by HayaoSuzuki
See All by HayaoSuzuki
Tasting "Python Distilled"
hayaosuzuki
0
240
Let's implement useless Python objects
hayaosuzuki
0
1.7k
How to Write Robust Python Code
hayaosuzuki
5
4k
Unknown Evolution of the Built-in Function pow
hayaosuzuki
0
1.4k
Python for Everyday
hayaosuzuki
1
1.9k
How to Use In-Memory Streams
hayaosuzuki
1
4.3k
Do you know cmath module?
hayaosuzuki
0
3.2k
Elementary Number Theory with Python
hayaosuzuki
1
3.4k
Django QuerySet "ARE" Patterns
hayaosuzuki
0
3.2k
Other Decks in Programming
See All in Programming
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
380
快速入門可觀測性
blueswen
0
500
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
870
rails newと同時に型を書く
aki19035vc
5
710
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
1.1k
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
7
1.4k
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
580
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
160
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
8
1.9k
テストコード書いてみませんか?
onopon
2
330
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
290
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Documentation Writing (for coders)
carmenintech
67
4.5k
A better future with KSS
kneath
238
17k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
How to Ace a Technical Interview
jacobian
276
23k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.7k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
Building Adaptive Systems
keathley
38
2.4k
Faster Mobile Websites
deanohume
305
30k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
570
Transcript
ϨΨγʔ Django ΞϓϦέʔγϣϯͷݱԽ A Modernization of Legacy Django Based Applications
Hayao Suzuki DjangoCongress JP 2018 at Cybozu, Inc. May 19, 2018
Contents 1 ֓ཁ 2 ΞϓϦέʔγϣϯલ࢙ 3 Twelve-Factor App 4 Django
ͷΞοϓσʔτ 5 ΞϓϦέʔγϣϯͷݱԽ 6 ·ͱΊ 2 / 29
Abstract ൃදͷ֓ཁ Ĺ Django 1.7 Ͱॻ͔ΕͨΞϓϦέʔγϣϯ͕ݱͩͬͨ Ĺ ࣍ظϦϦʔεʹ͚ͯ Django 1.11
ʹόʔδϣϯΞοϓͨ͠ Ĺ Twelve-Factor App ͷࢥΛϕʔεʹΞϓϦέʔγϣϯͷݱԽΛ࣮ࢪ ͨ͠ ୭͚ͷൃදʁ Ĺ ݹ͍ΞϓϦέʔγϣϯΛվળ͢Δඞཁ͕͋ΓɺܦݧஊΛฉ͖͍ͨํ Ĺ Twelve-Factor App ͷࢥͷ࣮ફྫ͕Γ͍ͨํ ҙ ࣮ࡍͷडୗҊ݅ͷࣄྫΛϕʔεʹ࠶ߏͨ͠ͷͰ͢ɻ 3 / 29
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
ΞϓϦέʔγϣϯલ࢙ ΞϓϦέʔγϣϯͷ֓ཁ Ĺ εϚʔτϑΥϯΞϓϦͷόοΫΤϯυαʔόʔ Ĺ ෳͷ Django ΞϓϦέʔγϣϯ͔Βߏ͞ΕΔ Ĺ BtoC
͚ʢΞΫηεͷ͕͋Δʣ Ĺ 2017 3 ݄ϦϦʔε Ĺ Django 1.7 Ĺ AWS EC2 ্ʹߏ ϓϩδΣΫτ๊͕͍͑ͯͨ Ĺ Python 2.7.6ʢUbuntu 14.04 ଐʂʣΛͦͷ··͍ଓ͚͍ͯͨ Ĺ Django 1.7 2015 Ͱαϙʔτ͕Ε͍ͯͨ Ĺ Management ίϚϯυαʔόʔϩάΠϯޙʹखಈͰ࣮ߦ Ĺ ϩάௐࠪαʔόʔϩάΠϯ͕ඞཁͱͳΔ Ĺ 2 ߏͰඞཁʹԠͯ͡αʔόʔͷεέʔϧΞοϓɾμϯ͕ඞཁ 5 / 29
ΞϓϦέʔγϣϯલ࢙ ࣍ظେܕΞοϓσʔτ Ĺ 2017 8 ݄ελʔτ ! ͏͙͢ϦϦʔε Ĺ
αʔόʔଆϝϯόʔ 4 ਓ Ĺ ๊͍͑ͯͨΛղܾ͠Α͏ʂ ͳ͕͍ͨͼ͕͡·Δ... 6 / 29
Twelve-Factor App Twelve-Factor App Ĺ Heroku ͷΤϯδχΞ͕ఏএͨ͠ SaaS Λͭ͘Γ্͛ΔͨΊͷํ๏ Ĺ
https://12factor.net/ 7 / 29
Twelve-Factor App ֓ཁ Ĺ ίʔυϕʔε Ĺ ґଘؔ Ĺ ઃఆ Ĺ
όοΫΤϯυαʔϏε Ĺ ϏϧυɺϦϦʔεɺ࣮ߦ Ĺ ϓϩηε Ĺ ϙʔτόΠϯσΟϯά Ĺ ฒߦੑ Ĺ ഇغ༰қੑ Ĺ ։ൃ/ຊ൪Ұக Ĺ ϩά Ĺ ཧϓϩηε 8 / 29
Twelve-Factor App ຬ͍ͨͯͨ͠ཁૉ Ĺ ίʔυϕʔε (ηϧϑϗεςΟϯάͷ GitLab Ͱཧʣ Ĺ όοΫΤϯυαʔϏεʢAWS
RDS ͳͲʣ Ĺ ϓϩηεʢγϯάϧϓϩηεʣ Ĺ ϙʔτόΠϯσΟϯάʢNginx ͱͷΈ߹Θͤʣ Γͳ͍ཁૉ Ĺ ઃఆʢڥ͝ͱʹϑΝΠϧ࡞ɺϋʔυίʔσΟϯάʣ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦʢͰ͖͍ͯͳ͍ʣ Ĺ ґଘؔʢPython requirements.txt ͕ͩɺOS ...ʣ Ĺ ฒߦੑʢ2 ߏɺεέʔϧΞοϓͷΈՄೳʣ Ĺ ഇغ༰қੑʢഇغͰ͖ͳ͍ʣ Ĺ ։ൃ/ຊ൪Ұகʢαʔόʔ͕ผͳͷͰҰக͠ͳ͍ʣ Ĺ ϩάʢαʔόʔʹอଘɺΤϥʔϝʔϧ௨ɺόονܥ CRON Ͱ Slack ʹ௨ʣ Ĺ ཧϓϩηεʢόοναʔόʔͰ࣮ߦɺSSH ϩάΠϯඞཁʣ 9 / 29
Django ͷΞοϓσʔτ ຐ๏ͷΑ͏ͳํ๏ଘࡏ͠ͳ͍ Ĺ Django ͷυΩϡϝϯτʹԊͬͯॗʑͱΔ΄͔ͳ͍ Ξοϓσʔτखॱ Ĺ ϢχοτςετΛॻ͘ʢॏཁʣ Ĺ
Django ͷόʔδϣϯΛ 1 ্ͭͣͭ͛Δ Ĺ python -Wall manage.py test Ͱςετ࣮ߦɺܯࠂΛ 1 ͭͣͭফԽ͢Δ Ĺ Django 1.11 ʹ౸ୡ͢Δ·Ͱ܁Γฦ͢ ࣮ྫ: ৽نࢀըऀʹϢχοτςετॻ͚Δͷ͔ Ĺ ແཧͰͨ͠ɻೖࣾ 1 ϲ݄ͷਓһͰϢχοτςετॻ͚ͳ͔ͬͨɻ Ĺ ·ͣϓϩδΣΫτͷࣝΛਂΊΔ͜ͱ͔Β࢝ΊΔɻ 10 / 29
Django ͷΞοϓσʔτ Django ϛυϧΣΞͷΞοϓσʔτ Ĺ MIDDLEWARE_CLASSES ͔Β MIDDLEWARE มߋ͢Δ͚ͩͰμϝ Ĺ
ϛυϧΣΞΛࣗ࡞͍ͯͨ͠߹खΛՃ͑Δඞཁ͕͋Δ ࣮ྫ: αʔυύʔςΟϛυϧΣΞ͕৽ܗࣜະରԠ Ĺ ݪҼڀ໌ʹ͕͔͔࣌ؒͬͨ Ĺ ϛυϧΣΞ͕ఏڙ͍ͯ͠Δػೳ Django ͕ެࣜʹఏڙ͢ΔΑ͏ʹͳͬͨɻ ࣮ྫ: ࣗ࡞ϛυϧΣΞͷΞοϓσʔτ Ĺ ެࣜυΩϡϝϯτʹҠߦखॱ͋Γ 11 / 29
Django ͷΞοϓσʔτ Figure: Django Middleware Λ৽ܗࣜมߋ͢Δ Commit 12 / 29
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
SQL ΫΤϦνϡʔχϯά Figure: AWS X-Ray ʹΑΔτϨʔεσʔλ 14 / 29
ෛ࠴Λ͏ϦϑΝΫλϦϯά ຐ๏ͷΑ͏ͳํ๏ଘࡏ͠ͳ͍ʢ2 ճʣ Ĺ ϢχοτςετΛॻ͍ͯෆಈΛ࡞͔ͬͯΒɻ Ξοϓσʔτखॱ Ĺ ϢχοτςετΛॻ͘ʢॏཁʣ ʢ2 ճʣ
Ĺ ʮ͋ͬ͜Εʯͱ͍͏෦ΛϦϑΝΫλϦϯά Ĺ python -Wall manage.py test Ͱςετ࣮ߦ Ĺ ݟ௨͕͠Α͘ͳΔ·Ͱܧଓ ࣮ྫ: Django ͷ MVT Ϟσϧʹجׂ͍ͮͨͷ Ĺ ৭ʑͱؤு͍ͬͯΔ View ͷॲཧΛ Model ʹҠৡ͢Δ Ĺ ༗ೳ͗͢Δ utils.py ͷػೳΛ Model ʹҠৡ͢Δ Ĺ ॏෳͨ͠ Model ΛجఈΫϥεͰཧ͢Δ Ĺ σίϨʔλͰڞ௨ॲཧΛҰݩԽ͢Δ 15 / 29
ڥมʹΑΔઃఆཧ III. ઃఆ Ĺ ઃఆΛίʔυ͔Βݫີʹ͢Δ Ĺ ઃఆΛڥมʹ֨ೲ͢Δ ࣮ྫɿdjango-environ ʹΑΔڥมͷཧ Ĺ
https://github.com/joke2k/django-environ Ĺ settings.py ʹϋʔυίʔσΟϯά͍ͯͨ͠ઃఆΛڥมҠৡ Ĺ develop.env ϑΝΠϧͰϩʔΧϧͰڥมΛѻ͑ΔΑ͏ʹ͢Δɻ 16 / 29
ڥมʹΑΔઃఆཧ Figure: ڥมͷΓग़͠Λ࣮ݱͨ͠ Merge Request 17 / 29
ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ VIII. ฒߦੑ Ĺ ϓϩηεୈҰڃࢢຽͰ͋Δ IX. ഇغ༰қੑ Ĺ
ଈ࠲ʹىಈɾऴྃ͢Δ͜ͱ͕Ͱ͖Δ Ĺ ىಈ࣌ؒΛ࠷খԽ͢Δ X. ։ൃ/ຊ൪Ұக Ĺ ܧଓతσϓϩΠ͍͢͠Α͏։ൃڥͱຊ൪ڥͷΪϟοϓΛখ͘͞อͭ 18 / 29
ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ Ĺ ഇغ༰қੑɺ։ൃ/ຊ൪ҰகΛ࣮ݱ͢Δ༗ྗͳख๏ ࣮ྫɿVagrant ͔Β
Docker Ĺ ։ൃɿVagrantɺຊ൪ɿAWS EC2 ͔Βͷ٫ Ĺ ຊ൪ڥΛࢀߟʹ Dockerfile ΛΈཱͯΔ Ĺ ࠷ॳ։ൃڥ͔Βར༻։࢝ɺͦͯ͠ AWS ECS 19 / 29
ΞϓϦέʔγϣϯͷ Docker ίϯςφԽ Figure: AWS ECR ʹϓογϡ͞Εͨ Docker Πϝʔδ 20
/ 29
ܧଓతΠϯςάϨʔγϣϯɺܧଓతσϓϩΠ V. ϏϧυɺϦϦʔεɺ࣮ߦ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦͷ 3 ͭͷεςʔδΛݫີʹ͢Δ ࣮ྫɿGitLab CI Ĺ
ैདྷίϛοτ࣌ʹϢχοτςετΛ࣮ߦ͢Δ͚ͩ Ĺ ίϛοτ࣌ʹςετɺϚʔδޙʹ Docker ΠϝʔδϏϧυɺσϓϩΠΛ࣮ࢪ Ĺ σϓϩΠ AWS ECR/ECS ߦ͍ɺ࣮ߦ ECS Ҡৡ 21 / 29
ܧଓతΠϯςάϨʔγϣϯɺܧଓతσϓϩΠ Figure: GitLab CI ʹΑΔ CI/CD ύΠϓϥΠϯ 22 / 29
ϩΪϯάͱ௨ XI. ϩά Ĺ ϩάΛΠϕϯτετϦʔϜͱͯ͠ѻ͏ ࣮ྫɿDocker ϩάυϥΠόʔͱ AWS CloudWatch Ĺ
ΞϓϦέʔγϣϯ͔Βͷϩάग़ྗͯ͢ඪ४ग़ྗ or ඪ४Τϥʔྲྀ͢ Ĺ ϩάͯ͢ AWS CloudWatch Logs ૹΔ Ĺ Docker ϩάυϥΠόʔʹઃఆ͢Δ͚ͩͳͷͰ؆୯ Ĺ CloudWatch Alarm ΛΈ߹ΘͤͯΤϥʔ௨ 23 / 29
ϩΪϯάͱ௨ Figure: AWS CloudWatch Alarm ͷ௨Λ Slack ʹԼͯ͠ରԠ͢Δ 24 /
29
όονλεΫͷίϚϯυԽ XII. ཧϓϩηε Ĺ ཧλεΫΛ 1 ճݶΓͷϓϩηεͱ࣮ͯ͠ߦ͢Δ ࣮ྫɿDjango ίϚϯυͱ AWS
ECS λεΫ Ĺ Django ίϚϯυ୯ҐͰ ECS λεΫΛ࡞Δ Ĺ ΤϯδχΞҎ֎Ͱόονૢ࡞͕ GUI ্Ͱ࣮ݱͰ͖Δ Ĺ Django ίϚϯυͱ ECS λεΫͷ૬ੑྑ͍ 25 / 29
όονλεΫͷίϚϯυԽ Figure: Django ίϚϯυΛ࣮ߦ͢Δ AWS ECS λεΫ 26 / 29
ڥมࠈ ·͔͞ͷ࣌ͷڥมࠈʂ Ĺ 1 ΞϓϦέʔγϣϯ͋ͨΓेݸͷڥม Ĺ ։ൃڥɺݕূڥɺຊ൪ڥɺCI/CD ͚ Ĺ ෳͷΞϓϦέʔγϣϯ͔ΒͳΔϓϩδΣΫτ
ڥมࠈΛͲ͏Γൈ͚Δ͔ Ĺ ʮϓϥΠϕʔτϨϙδτϦ͔ͩΒ҆৺ʯઆ Ĺ AWS S3 ʹผ్อଘͯ҆͠શʹऔΓग़͢ Ĺ ڥมઐ༻ͷϨϙδτϦΛ࡞ͯ͠ SSH or HTTPS Ͱ҆શʹऔΓग़͢ Ĺ AWS ͷΫϨσϯγϟϧ IAM Role Ͱ ECS λεΫʹׂΓͯͯݮΒ͢ ࣮ྫɿAWS Secrets Manager Ĺ Γʹધ Ĺ ڥมΛڥ͝ͱʹผʹͯ҆͠શʹอଘ͢Δ Ĺ Secrets Manager ͷݤผ్ཧ͢Δඞཁ͕͋Δ 27 / 29
Twelve-Factor App ຬ͍ͨͯͨ͠ཁૉ Ĺ ίʔυϕʔε (ηϧϑϗεςΟϯάͷ GitLab Ͱཧʣ Ĺ όοΫΤϯυαʔϏεʢAWS
RDS ͳͲʣ Ĺ ϓϩηεʢγϯάϧϓϩηεʣ Ĺ ϙʔτόΠϯσΟϯάʢNginx ͱͷΈ߹Θͤʣ վળͨ͠ཁૉ Ĺ ઃఆʢڥมʹΑΔʣ Ĺ ϏϧυɺϦϦʔεɺ࣮ߦʢGitLab CI ʹΑΔ CI/CDʣ Ĺ ґଘؔʢrequirements.txt ͱ dockerfileʣ Ĺ ฒߦੑʢAWS ECS ʹΑΔΦʔέετϨʔγϣϯʣ Ĺ ഇغ༰қੑʢDocker ίϯςφʣ Ĺ ։ൃ/ຊ൪ҰகʢDockerʣ Ĺ ϩάʢAWS CloudWatchʣ Ĺ ཧϓϩηεʢAWS ECS ͷλεΫʣ 28 / 29
·ͱΊ ࣍ظϦϦʔεͷඪ Ĺ Python 3 ͷҠߦ ˍ Django 2.2 LTS
ͷҠߦ ·ͱΊ Ĺ Django ͷΞοϓσʔτʹຐ๏ଘࡏ͠ͳ͍ɻಓͳऔΓΈ͕ॏཁɻ Ĺ ݹ͍ΞϓϦέʔγϣϯͰ Django ϕʔεͳΒվળߦ͍͍͢ɻ Ĺ طଘͷΫϥυͷΤίγεςϜʹ͔ͬΔͱԿ͔ͱҠৡͰָ͖ͯʹͳΔɻ Ĺ ڥมͷཧʹඞͣ໘͢Δɻඋ͑Α͏ɻ 29 / 29