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
Djangoアプリケーションにおけるトイル撲滅戦記.pdf
Search
Yasuaki Matsuda
September 18, 2018
Programming
3
3.6k
Djangoアプリケーションにおけるトイル撲滅戦記.pdf
https://pycon.jp/2018/
Yasuaki Matsuda
September 18, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
Fibonacci Function Gallery - Part 1
philipschwarz
PRO
0
200
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
630
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Recoilを剥がしている話
kirik
5
6.6k
The rollercoaster of releasing an Android, iOS, and macOS app with Kotlin Multiplatform | droidcon Italy
prof18
0
150
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
103 Early Hints
sugi_0000
1
220
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
急成長期の品質とスピードを両立するフロントエンド技術基盤
soarteclab
0
920
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Featured
See All Featured
Fireside Chat
paigeccino
34
3.1k
Why Our Code Smells
bkeepers
PRO
335
57k
We Have a Design System, Now What?
morganepeng
51
7.3k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
How GitHub (no longer) Works
holman
311
140k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Transcript
DjangoΞϓϦέʔγϣϯʹ͓͚Δ τΠϧ໓ઓه
ࣗݾհ দా ߁ষ (Yasuaki Matsuda) @yacchin0101 גࣜձࣾΞΠϦοδ αʔόαΠυΤϯδχΞ
ࣗݾհ
ͲΜͳਓ͕ରʁ τΠϧ໓ʹڵຯ͕͋Δ ݱͰDjangoΛͬͯΔ ʑͷӡ༻ۀͰർฐ͍ͯ͠Δਓ
࣍ લ࢙ τΠϧ໓ τΠϧͱ ޮՌ ·ͱΊ
େมਃ͠༁͍͟͝·ͤΜ τΠϧΛ໓ͨ͠Θ͚Ͱͳ͍
େมਃ͠༁͍͟͝·ͤΜ SREʮτΠϧͷ໓ʯ͕ϝΠϯ
τΠϧͱ τΠϧʹ͍ͭͯͷհ photo by Yamato Fukui
SREͱ Google͕ࣾఏএɾ࣮ફ͍ͯ͠Δ γεςϜཧͱαʔϏεӡ༻ͷํ๏
SREͱ IUUQTMBOEJOHHPPHMFDPNTSFCPPLJOEFYIUNM 5ষ τΠϧͷ໓ τΧήʁ SRE αΠτϦϥΠΞϏϦςΟ ΤϯδχΞϦϯά
τΠϧͱ ຊ൪αʔϏεʹؔ͢Δ࡞ۀͰ ख࡞ۀͰ܁Γฦ͠ߦΘΕ ࣗಈԽ͢Δ͜ͱ͕ՄೳͰ͋Γ ઓज़తͰظతͳՁΛ࣋ͨͣ ࡞ۀྔ͕αʔϏεͷʹൺྫ͢Δ
৯ثͷखચ͍ ۙͳτΠϧ
ۙͳτΠϧ ख࡞ۀͰ͋Δ ܁Γฦ͞ΕΔ ࣗಈԽͰ͖Δ ઓज़తͰͳ͍ ظతͳՁΛ࣋ͨͳ͍ αʔϏεʹରͯ͠O(n)Ͱͳ͍ ৯ثͷखચ͍
ۙͳτΠϧ શࣗಈ৯ચػ
τΠϧ͕ͨΒ͢ͷ ҰॠͰՔಇΛຒΊਚ͘͞ΕΔ ϔΠτΛు͘Α͏ʹͳΔ ΩϟϦΞ͕ఀ͢Δ ͳͲ
·ͱΊ τΠϧΛ໓ͯ͠ PythonͰΠϊϕʔςΟϒͳ ࣄΛ͠Α͏ʂ
લ࢙ τΠϧ໓લͷDjangoΞϓϦέʔγϣϯʹ͍ͭͯͷհ photo by Yamato Fukui
લ࢙ Online to Offline
લ࢙ དྷళൢଅࢪࡦ Push௨ ϙΠϯτ Ϋʔϙϯ ήʔϜ
Push௨ ͓ɺങ͍ʹ ߦ͜͏͔ͳɻ ΫϦΞϥϯεηʔϧ ։࠵தʂ
ϙΠϯτ ϙΠϯτ͑Δ͠ ߦ͜͏͔ͳɻ དྷళϙΠϯτ Ωϟϯϖʔϯ࣮ࢪதʂ
Ϋʔϙϯ ͋ͷళͷΫʔϙϯ ͋Δ͡ΌΜɻ 10%OFFΫʔϙϯ தʂ
ήʔϜ ͬͨͥ ͓ΊͰͱ͏ʂ 100PTήοτʂ
લ࢙ hogeΞϓϦ Push௨ ϙΠϯτ Ϋʔϙϯ ήʔϜ ܾࡁ
Offline ͓ళͷηʔϧใΛݟΔ ϙΠϯτߴΛ֬ೝ͢Δ ήʔϜΛ࣮ࢪͯ͠ϙΠϯτ֫ಘ
Online ͓ళͰΫʔϙϯΛ͏ ΞϓϦͰܾࡁ͢Δ
લ࢙ hogeΞϓϦͱࢲͷؔ αʔόαΠυػೳ࣮ Πϯϑϥߏங ӡ༻ɾอक୲ऀ
લ࢙ CPU༻ Push௨
લ࢙ ֎࣌ ࣌ τΠϧ໓࣌ 2015 2016 2018
֎࣌ ελʔτΞοϓاۀ ։ൃऀෆ ֎
ʢ2015.x.xʣ ߏ App & Batch Server Admin Server Coupon Server
ELB ELB
֎࣌ ຯΘ͍ਂ͍ߏ DBಉډ SSLূ໌ॻஔ͖ σϓϩΠ: SCP ͳͲ
ઌํʮΞϓϦͷػೳ֦ு͍ͨ͠ʯ ֎࣌ Ӧۀʮঝ͍ͨ͠·ͨ͠ʂʯ ೖࣾͨ͠দాʮΑΖ͓͘͠Ͷ͕͍͠·͢ʂʯ PMʮࠓޙͷ֦ுΛߟ͑ͯ͢Δ͔ʯ
࣌ ࣾ։ൃ Python / Django Ͱ։ൃ GitLabͰιʔεཧ AWSͰߏங
ʢ2016.x.xʣ ߏ App Server Admin Server Coupon Server RDS ELB
ELB Batch Server
࣌ ଟগվળ͞Εͨ RDS(ϚωʔδυαʔϏε) PHP͔ΒDjangoҠߦ σϓϩΠ: Git Pull ͳͲ
ϩʔϯνޙ ઌํʮ͜͜ͷจݴमਖ਼Λ͓ئ͍Ͱ͖·͢ʁʯ PMʮঝ͠·ͨ͠ɻদా͞Μ͓ئ͍͠·͢ɻʯ দాʮ͍ɻʯ
࣌ ͋Εʁ ʮ͜ͷલґཔ͞ΕͨΑ͏ͳʯ ʮ࠷ۙɺ։ൃͰ͖͍ͯͳ͍ʯ ʮਓ͕Γͳ͍ʯ
࣌ τΠϧʹૡ͖ۤ͠Ή࣌ͷ౸དྷ
τΠϧ໓ τΠϧ໓ͷաఔʹ͍ͭͯհ photo by Yamato Fukui
ํ Twelve-Factor AppΛ Managed ServiceΛ CI / CD
ํ
Twelve-Factor App ࣌ؒɾίετͷ࠷খԽ Ҡ২ੑ্ Πϯϑϥཧ͔Βͷ։์ ܧଓతσϓϩΠ εέʔϧΞοϓ IUUQTGBDUPSOFU
Managed Service Πϯϑϥཧෆཁ ӡ༻ϦεΫݮ ܧଓతվળ
CI / CD ख࡞ۀ͔Βͷ։์ ্࣭
τΠϧ໓ Case 1
τΠϧ໓લͷঢ়گ ڥߏங࡞ۀ ৽͍͠ΤϯδχΞ͕ࢀը README௨Γʹڥߏங ಈ͔ͳ͍ ৬ਓͷग़ਞ
ػೳ։ൃ։࢝·Ͱʹ͔͔࣌ؒΔ ৬ਓͷՔಇ͕औΒΕΔ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓ઓུ Docker Compose
τΠϧ໓ઓུ ίϯςφܕԾڥ ڥࠩҟ͕খ͘͞ͳΔ Ҡ২ੑ͕ߴ͍ Docker
τΠϧ໓ઓུ Docker Compose ෳίϯςφΛఆٛ ෳίϯςφΛ࣮ߦ docker-compose up ͷΈ
͙͢։ൃͰ͖ΔΑ͏ʹͳͬͨ ৬ਓͷग़ਞ͕͑ΒΕͨ ڥࠩҟʹΑΔόά͕͑ΒΕͨ τΠϧ໓ޙ վળޮՌ
τΠϧ໓ޙ
τΠϧ໓ Case 2
खॱॻͷ࡞ 1ͣͭॖୀɾߋ৽ νΣοΧʔͱෳਓମ੍ σϓϩΠ࡞ۀ τΠϧ໓લͷঢ়گ
σϓϩΠखॱࡶʹΑΔ ώϡʔϚϯΤϥʔϦεΫ σϓϩΠʹର͢Δ͕ߴΊ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓ઓུ GitLab CI Amazon ECS
τΠϧ໓ઓུ ίϯςφΦʔέετϨʔγϣϯ αʔόཧෆཁ εέδϡʔϧ࣮ߦ Amazon ECS
τΠϧ໓ઓུ GitϦϙδτϦ CI / CDػೳ issueཧ ίʔυϨϏϡʔ GitLab
RDS ELB X-Ray ECS App Server Service X-Ray Batch Task
ELB ELB ɾɾɾ X-Ray X-Ray Coupon Server Service ɾɾɾ X-Ray X-Ray Admin Server Service ɾɾɾ X-Ray CloudWatch Logs S3 τΠϧ໓ઓུ
ιʔεमਖ਼ ϨϏϡʔ ϙνΔ τΠϧ໓ޙ σϓϩΠखॱ
ਓҝతϛε͕ൃੜ͠ͳ͘ͳͬͨ σϓϩΠʹର͢Δ͕ݮͬͨ վળޮՌ τΠϧ໓ޙ
/hoge/ͰPOST࣌IP੍ݶ͍ͨ͠ τΠϧ໓ޙ
τΠϧ໓ޙ SQLΫΤϦվળ
ECR X-Ray APP Server Service X-Ray New Task Old Task
1. Image Build & Push New Image 2. Create New Task Definition 3. Update Service 1. Push Source Code X-Ray App Task: 1 X-Ray App Task: 2 GitLab 2. Create Merge Request(MR) 3. Review & LGTM & Accept MR τΠϧ໓ޙ
τΠϧ໓ ͓·͚
τΠϧ໓લͷঢ়گ ੩తϑΝΠϧσϓϩΠ ιʔεमਖ਼ ϨϏϡʔ ϙνΔ
ɻΛ༩͍ͨ͠ τΠϧ໓ޙ
1. npm install & build 2. Put Artifacts to S3
Bucket 1. Push Source Code GitLab 2. Create Merge Request(MR) 3. Review & LGTM & Accept MR AWS CLI τΠϧ໓ޙ
τΠϧ໓ ߋʹ͓·͚
Ϣχοτςετ ίʔσΟϯάن֬ೝ ෳࡶ֬ೝ τΠϧ໓લͷঢ়گ ιʔείʔυϨϏϡ࡞ۀ
1. Unit Test 2. Flake8 3. Radon GitLab ςετ࣮ࢪʹΑΔ࣭୲อ ίʔσΟϯάنͷ֬ೝ
ίʔυͷෳࡶΛ֬ೝ 1. Push Source Code 2. Create Merge Request(MR) 3. Review & LGTM & Accept MR τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ ͓·͚ (͜ΕͰ࠷ޙ)
τΠϧ໓લͷঢ়گ όοναʔόෆཁ ECSͰఆظతʹ࣮ߦͰ͖Δ ͚ͬͨͩͷྉۚ
τΠϧ໓ޙ
τΠϧ໓ Case 3
τΠϧ໓લͷঢ়گ ֤αʔό͔ΒΞΫηεϩάऔಘ grepɾawk৬ਓ ಛఆϢʔβͷΞΫηεௐࠪ
ௐࠪʹ͕͔͔࣌ؒΔ ώϡʔϚϯΤϥʔʹΑΔϦεΫ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓ઓུ CloudWatch Logs
τΠϧ໓ઓུ ϩάͷࢹ ϩάͷอଘ ϩάͷΞΫηε CloudWatch Logs
τΠϧ໓ઓུ settings.py
τΠϧ໓ઓུ docker-compose.yml
Ͱௐࠪྃ ਓҝతϛεʹΑΔαʔϏεఀࢭ͕ͳ͍ ඇΤϯδχΞͰௐࠪՄೳ τΠϧ໓ վળޮՌ
τΠϧ໓ޙ ϩάΛҰՕॴʹू લํҰகݕࡧ ࣌ݕࡧ
τΠϧ໓ޙ
τΠϧ໓ Case 4
τΠϧ໓લͷঢ়گ RDSεέʔϧΞοϓ࡞ۀ ࣮ߦ͞ΕΔSQL͕ѱ͍ CPU༻100%ுΓ͖ αʔϏεμϯൃੜ ۚͰղܾ
αʔϏεμϯͷՄೳੑ αʔϏεҡ࣋ʹίετ͕͔͔Δ ࣮༧ఆͩͬͨͷ͕Ԇ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓ઓུ Django QuerySet
σʔλϕʔεͷ҆ఆՔಇ ༨ܭͳεέʔϧΞοϓΛ͑Δ վળޮՌ τΠϧ໓ޙ
ήʔϜ࣮ࢪՄ൱API ήʔϜ11ճ·Ͱ ΞϓϦશମͰ1ʹ10,000ճ τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
PushͰήʔϜར༻ऀ૿Ճ ϨεϙϯεྼԽ ΠϯελϯεεέʔϧΞοϓ τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ Case 5
τΠϧ໓લͷঢ়گ ཧαʔόʹϩάΠϯͰ͖ͳ͍ͷௐࠪ ϩάௐࠪ ϩάʹు͔Ε͍ͯͳ͍ ιʔεΛݟΔ
ղܾʹ͕͔͔࣌ؒΔ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓લͷঢ়گ
τΠϧ໓લͷঢ়گ Effective Python Item 14: Prefer Exceptions to returning None
νϟϦ Effective Python —
τΠϧ໓લͷঢ়گ ྑ͘ͳ͍ except۟ͰϩΪϯά͍ͯ͠ͳ͍ except۟ͰNoneΛฦ٫
τΠϧ໓ઓུ Django Logging
ϩάΛݟΔ͚ͩͰঢ়گѲ τΠϧ໓ վળޮՌ
τΠϧ໓ޙ
ྑ͘ͳͬͨ ϩάͰঢ়گΛѲͰ͖Δ ಛผͳྫ֎Λ͛Δ͜ͱͰ Γ͍ͨ͜ͱΛ໌֬ʹͨ͠ τΠϧ໓ޙ
τΠϧ໓ Case 6
τΠϧ໓લͷঢ়گ ֤APIͷϨΠςϯγௐࠪ ֤αʔό͔ΒΞΫηεϩάऔಘ grepɾawk৬ਓ
͕͔͔࣌ؒΔ ϦΞϧλΠϜʹΘ͔Βͳ͍ τΠϧ໓લͷঢ়گ ՝
τΠϧ໓ઓུ AWS X-Ray
τΠϧ໓ઓུ ϦΫΤετʹؔ͢Δσʔλऩू σʔλͷදࣔɾϑΟϧλϦϯά தؒͷαʔϏεৄࡉදࣔ AWS X-Ray
τΠϧ໓ઓུ X-Ray X-Ray Daemon
τΠϧ໓ઓུ settings.py
τΠϧ໓ઓུ Dockerfile
ຊ൪ڥͷঢ়گ͕ѲՄೳ ύϑΥʔϚϯενϡʔχϯά˕ োରԠͰʹཱͭ վળޮՌ τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
τΠϧ໓ޙ
ޮՌ τΠϧ໓ʹΑΔޮՌͷհ photo by Yamato Fukui
ޮՌ োରԠ͕ݮͬͨ ख࡞ۀݮʹ͏ώϡʔϚϯΤϥʔ͕গͳ͘ͳͬͨ͜ͱ͕େ͖͍ ΤϯδχΞϦϯάʹׂ͚Δ͕࣌ؒ૿͑ͨ DjangoΞϓϦέʔγϣϯ։ൃʹྗͰ͖ΔΑ͏ʹͳͬͨ ΩϟϦΞΞοϓͰ͖ͨ ϦʔμʹͳΓ·ͨ͠
·ͱΊ photo by Yamato Fukui
·ͱΊ ʮͦΕτΠϧ͡ΌͶʁʯͷਫ਼ਆ τΠϧΛݮΒͤΤϯδχΞϦϯάʹूதͰ͖Δ ܧଓͯ͠վળͰ͖Δঢ়ଶΛ࡞Δ τΠϧແݶʹ༙͖ଓ͚Δ Managed ServiceɾCI / CDͷ׆༻ ཧରΛݮΒ͠ɺࣗಈԽΛਪਐ͢Δ
·ͱΊ τΠϧΛ໓ͯ͠ PythonͰΠϊϕʔςΟϒͳ ࣄΛ͠Α͏ʂ
ࢀߟจݙ O'Reilly Japan - SRE αΠτϦϥΠΞϏϦςΟΤϯδχΞϦϯά O'Reilly Japan - Effective
Python