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
4
3.7k
Djangoアプリケーションにおけるトイル撲滅戦記.pdf
https://pycon.jp/2018/
Yasuaki Matsuda
September 18, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
1.3k
functionalなアプローチで動的要素を排除する
ryopeko
1
220
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
Rubyでつくるパケットキャプチャツール
ydah
0
170
情報漏洩させないための設計
kubotak
5
1.3k
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
7
1.4k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
710
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
32
6.4k
Making Projects Easy
brettharned
116
6k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Six Lessons from altMBA
skipperchong
27
3.6k
It's Worth the Effort
3n
183
28k
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