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.4k
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
180
Let's implement useless Python objects
hayaosuzuki
0
1.5k
How to Write Robust Python Code
hayaosuzuki
5
3.7k
Unknown Evolution of the Built-in Function pow
hayaosuzuki
0
1.3k
Python for Everyday
hayaosuzuki
1
1.8k
How to Use In-Memory Streams
hayaosuzuki
1
4k
Do you know cmath module?
hayaosuzuki
0
3.1k
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
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
100
CSC509 Lecture 09
javiergs
PRO
0
140
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
220
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
C++でシェーダを書く
fadis
6
4.1k
初めてDefinitelyTypedにPRを出した話
syumai
0
400
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
受け取る人から提供する人になるということ
little_rubyist
0
230
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
How to Ace a Technical Interview
jacobian
276
23k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
4 Signs Your Business is Dying
shpigford
180
21k
GitHub's CSS Performance
jonrohan
1030
460k
Done Done
chrislema
181
16k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
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