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
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1k
qmuntal/stateless のススメ
sgash708
0
120
Synchronizationを支える技術
s_shimotori
1
150
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
200
僕がつくった48個のWebサービス達
yusukebe
18
17k
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
go.mod、DockerfileやCI設定に分散しがちなGoのバージョンをまとめて管理する / Go Connect #3
arthur1
10
2.4k
Outline View in SwiftUI
1024jp
1
110
hotwire_or_react
harunatsujita
8
4k
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7k
Git: the NoSQL Database
bkeepers
PRO
425
64k
A designer walks into a library…
pauljervisheath
202
24k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
107
49k
Building an army of robots
kneath
302
42k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Typedesign – Prime Four
hannesfritz
39
2.4k
Making the Leap to Tech Lead
cromwellryan
132
8.9k
Teambox: Starting and Learning
jrom
132
8.7k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
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 ܅Λ࡞ΕΔαΠτ
͓͠·͍