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
情報漏洩させないための設計
kubotak
5
1.3k
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
DMMオンラインサロンアプリのSwift化
hayatan
0
180
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
770
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
1.3k
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
return文におけるstd::moveについて
onihusube
1
1.4k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
360
Flatt Security XSS Challenge 解答・解説
flatt_security
0
730
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Designing Experiences People Love
moore
139
23k
Making Projects Easy
brettharned
116
6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
173
51k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Site-Speed That Sticks
csswizardry
2
270
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Faster Mobile Websites
deanohume
305
30k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
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