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
230
Let's implement useless Python objects
hayaosuzuki
0
1.6k
How to Write Robust Python Code
hayaosuzuki
5
3.9k
Unknown Evolution of the Built-in Function pow
hayaosuzuki
0
1.3k
Python for Everyday
hayaosuzuki
1
1.9k
How to Use In-Memory Streams
hayaosuzuki
1
4.2k
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
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
事業成長を爆速で進めてきたプロダクトエンジニアたちの成功談・失敗談
nealle
3
1.4k
CSC305 Lecture 25
javiergs
PRO
0
130
CSC305 Lecture 26
javiergs
PRO
0
140
103 Early Hints
sugi_0000
1
220
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
350
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
200
快速入門可觀測性
blueswen
0
310
Zoneless Testing
rainerhahnekamp
0
120
선언형 UI에서의 상태관리
l2hyunwoo
0
130
Go の GC の不得意な部分を克服したい
taiyow
2
760
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
Site-Speed That Sticks
csswizardry
2
190
How STYLIGHT went responsive
nonsquared
95
5.2k
It's Worth the Effort
3n
183
28k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Fireside Chat
paigeccino
34
3.1k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Automating Front-end Workflow
addyosmani
1366
200k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
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