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
モバイルアプリにおける自動テストの導入戦略
ostk0069
0
110
testcontainers のススメ
sgash708
1
120
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
110
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
快速入門可觀測性
blueswen
0
350
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
930
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
270
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
95
5.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
RailsConf 2023
tenderlove
29
940
GraphQLとの向き合い方2022年版
quramy
44
13k
Adopting Sorbet at Scale
ufuk
73
9.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
Become a Pro
speakerdeck
PRO
26
5k
A designer walks into a library…
pauljervisheath
204
24k
We Have a Design System, Now What?
morganepeng
51
7.3k
Docker and Python
trallard
42
3.1k
Designing for Performance
lara
604
68k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
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