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
Kubernetes のソースコードとの付き合い方 #gounco / Kubernetes ...
Search
d-kuro
May 27, 2019
Programming
24
4.9k
Kubernetes のソースコードとの付き合い方 #gounco / Kubernetes source code reading
Go(Un)Conference(Goあんこ)LT大会 6kg で発表した資料です
https://gounconference.connpass.com/event/129090/
d-kuro
May 27, 2019
Tweet
Share
More Decks by d-kuro
See All by d-kuro
kube-state-metrics Sharding
daikurosawa
0
290
Helm Chart を Unit Test する / Unit Testing Helm Chart
daikurosawa
0
1.9k
Understanding CPU throttling in Kubernetes to improve application performance #k8sjp
daikurosawa
12
14k
Leader Election in Kubernetes #k8sjp
daikurosawa
5
9.9k
Argo CD 実践ガイド #k8sjp / Argo CD Practice Guide
daikurosawa
13
8.2k
図で理解する Descheduler #k8sjp #ymju / Introduction to Descheduler
daikurosawa
12
23k
GolangCI を使ってコードの品質を保ちながら快適な Golang 生活を送る話 #gounco / GolangCI
daikurosawa
4
5.8k
Introduction gRPC
daikurosawa
1
240
Go Cloud を触ってみる / gopher-dojo-lt
daikurosawa
2
2.1k
Other Decks in Programming
See All in Programming
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
Realtime API 入門
riofujimon
0
150
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Arm移行タイムアタック
qnighy
0
330
Quine, Polyglot, 良いコード
qnighy
4
650
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Remix on Hono on Cloudflare Workers
yusukebe
1
300
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
960
受け取る人から提供する人になるということ
little_rubyist
0
250
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
It's Worth the Effort
3n
183
27k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Code Reviewing Like a Champion
maltzj
520
39k
Writing Fast Ruby
sferik
627
61k
[RailsConf 2023] Rails as a piece of cake
palkan
52
4.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
655
59k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Transcript
Kubernetes ͷ ιʔείʔυͱͷ ͖߹͍ํ 19/05/27 Go(Un)ConferenceʢGo͋Μ͜ʣLTେձ 6kg @ponde_m
None
ࠓ͢͜ͱ • Kubernetes ͷΞʔΩςΫνϟʹ͍ͭͯ • Kubernetes ͷιʔείʔυΛಡΉࡍʹ ʹཱͭ Go ͷษڧձ͚ͩͲ
Kubernetes ͱͳΜͧ Έ͍ͨͳ͕݁ߏ͋Γ·͢ (Gopher ͘Μ͍ͬͺ͍ग़·͢)
Kubernetes ͱ
Kubernetes ͱ • ίϯςφΛಈ͔ͨ͢ΊͷϓϥοτϑΥʔϜ • ίϯςφΛ͍͍ײ͡ʹӡ༻͢ΔͨΊͷػೳΛ ͍ͬͺ͍͍࣋ͬͯΔ • σϓϩΠ, εέʔϦϯά,
ࣗݾम෮ etc… k8s ͬͯॻ͔ΕͨΓ͢Δͷ Kubernetes ͕ (k + 8 จࣈ + s) ͔ͩΒ
Kubernetes ͷ ΞʔΩςΫνϟ
• ΞʔΩςΫνϟʹ͍ͭͯཧղ͢Δͱ, ࣗͷಡΈ͍ͨ෦ͷίʔυ͕ Ͳͷίϯϙʔωϯτʹ͋Δͷ͔Θ͔Δ • ͦΕͧΕͷίϯϙʔωϯτͱ ͦͷʹ͍ͭͯཧղ͠Α͏ Kubernetes ͷΞʔΩςΫνϟ ࣍ͷϖʔδ͔Βͬ͘͟Γͱ
ਤΛ༻͍ͯઆ໌͠·͢
Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API
Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd API Server
Kubernetes ͷ ϦιʔεΛཧ͢Δ API Server શͯͷϦιʔεૢ࡞ API Server Λܦ༝ͯ͠ߦ͏
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd Contoroller Manager
API Server ͰϦιʔεใͷ CRUD ͙Β͍͔͠ߦͳͬͯ ͓ΒͣɺϦιʔεʹର͢Δ࣮ࡍͷॲཧͷத Controller ͱݺΕΔίϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ Controller (goroutine) Controller (goroutine) Controller (goroutine) ىಈ
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd Scheduler Pod
(ίϯςφ) ΛϊʔυׂΓͯΔ
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd ϦιʔεͷӬଓԽʹΘΕΔࢄ KVS
etcd
Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API
Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet kubelet
Kubernetes ͷΞʔΩςΫνϟ API Server kubelet kube-proxy Pod Pod Pod Internet
kubelet ֤ϊʔυͰಈ࡞͢Δ Pod Λىಈ, ཧ͢ΔΤʔδΣϯτ kubelet
Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet kube-proxy
kubelet ֤ϊʔυͰಈ࡞͢Δ Service ͷఆٛΛݩʹϧʔςΟϯά ͯ͘͠ΕΔωοτϫʔΫϓϩΩγ kube-proxy
Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet Pod
Pod Pod kubelet Container Runtime Docker Ҏ֎ಈ͘ Pod Pod Pod
Kubernetes ͷ ιʔείʔυʹ͍ͭͯ
Kubernetes ͷιʔείʔυʹ͍ͭͯ • github.com/kubernetes/kubernetes • Go Modules ରԠͯ͠ΔͷͰ go mod
ίϚϯ υΛ࣮ߦ͢ΔͱϏϧυͰ͖ΔΑ͏ʹͳΔ • ಡΉ࣌ IDE ͰίʔυδϟϯϓΛ͓͏ 4݄͙Β͍ʹ Go Modules ରԠͨ͠ https://github.com/kubernetes/kubernetes/pull/74877
Kubernetes ͷιʔείʔυʹ͍ͭͯ • ϒϥβ͔Β͏࣌ SourceGraph Λ ೖΕΔͱศར • ϒϥβ͔Β Code
Jump Ͱ͖Δ • https://about.sourcegraph.com/
Kubernetes ͷιʔείʔυʹ͍ͭͯ • Kubernetes ͷίϯϙʔωϯτͷ࣮Ͱ github.com/spf13/cobra ͕ΘΕ͍ͯΔ • Server ͱͯ͠ৼΔ͏ͷ
cobra Ͱ࣮͞Ε͍ͯΔ • ࣮ߦ࣌ͷ Option ͰύϥϝʔλΛͨ͠ΓͰ͖Δ
main package Ͳ͜ʹ͋Δͷ͔
main package Ͳ͜ʹ͋Δͷ͔ • cmd package ͷதΛݟΔͱ֤ίϯϙʔωϯτ͝ ͱʹ͞Βʹ package ͕͔Ε͍ͯΔ
github.com/kubernetes/ kubernetes/cmd/ ͖ͬ͞ݟͨΑ͏ͳ໊લ͕ͪΒ΄Β
• main package ͦͷԼʹ͋Δ main package Ͳ͜ʹ͋Δͷ͔
None
Controller ʹ͍ͭͯ
Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller Manager Scheduler etcd Contoroller Manager
API Server ͰϦιʔεใͷ CRUD ͙Β͍͔͠ߦͳͬͯ ͓ΒͣɺϦιʔεʹର͢Δ࣮ࡍͷॲཧͷத Controller ͱݺΕΔίϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ Controller (goroutine) Controller (goroutine) Controller (goroutine) ىಈ ͓͞Β͍
• Ϧιʔεʹର͢Δ࣮ࡍͷॲཧͷத Controller ͱݺΕΔί ϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ • ྫ͑ Deployment ͷಈ͖ͷ࣮͕ݟ͍ͨʂ ͬͯͳͬͨ࣌ʹ
API Server ͷίʔυΛಡΈʹߦͬͯ Կॻ͍ͯͳ͍ • Controller Λಡ͏ Controller ʹ͍ͭͯ ͜ͷ͜ͱΛΒͣʹ࠷ॳ API Server ͷίʔυಡΜͰ·ͨ͠
Controller ʹ͍ͭͯ Contoroller Manager Controller (goroutine) Controller (goroutine) Controller (goroutine)
ىಈ • ࣮ࡍʹͲΜͳײ͡ͳͷ͔ίʔυͰݟͯΈΔ
͜Μͳײ͡ͷ map Ͱఆٛ͞Ε͍ͯΔ https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/ controllermanager.go#L341-L384
Controller ͷ໊લͱ Controller Λ࣮ߦ͢ΔͨΊͷؔ
func StartControllers() Ͱ Controller ΛൃՐ (ΊͬͪΌίʔυͬͯΔ) https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/ controllermanager.go#L461-L501
͖ͬ͞ͷ map Λ for Ͱճͯ͠ ͜͜Ͱ Controller ͷ࣮ߦؔΛݺΜͰΔ
ྫͱͯ͠ Deployment ͷ Controller ͷ࣮ߦؔ https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/apps.go#L82-L97
ίϯετϥΫλͰ Controller Λ࡞ͬͯ goroutine Ͱ Controller Λ࣮ߦ
·ͱΊ • Kubernetes ͷΞʔΩςΫνϟ ͓֮͑ͯ͘ͱʹཱͭ • Kubernetes ͷιʔείʔυҙ֎ʹಡΊΔ • Go
ಡΈ͍͢ ΈΜͳಡ͏
͓·͚
ʹཱͭϦϯΫͱ͔ • github.com/jamiehannaford/what-happens-when-k8s • Kubernetes ͕ىಈ͔ͯ͠ΒͲΜͳ͜ͱΛ͍ͯ͠Δ͔ ιʔείʔυϨϕϧͰղઆͯ͘͠ΕΔ • ͳΜͱຊޠ ver
͋Δ • ιʔείʔυͷϦϯΫ͕ݹ͔ͬͨΓ, ϦϯΫΕͩͬͨ Γ͢ΔͷͰ PR Λग़ͦ͏! (ग़ͯ͠Δ)
ʹཱͭϦϯΫͱ͔ • https://gopherize.me/ • Զ͚ͩͷ࠷ߴʹ͔Θ͍͍ Gopher ܅Λ࡞ΕΔαΠτ
͓͠·͍