Kubernetes のソースコードとの付き合い方 #gounco / Kubernetes source code reading

Kubernetes のソースコードとの付き合い方 #gounco / Kubernetes source code reading

Go(Un)Conference(Goあんこ)LT大会 6kg で発表した資料です
https://gounconference.connpass.com/event/129090/

Cad656ed619672b702191833dc819943?s=128

d-kuro

May 27, 2019
Tweet

Transcript

  1. Kubernetes ͷ ιʔείʔυͱͷ ෇͖߹͍ํ 19/05/27 Go(Un)ConferenceʢGo͋Μ͜ʣLTେձ 6kg
 @ponde_m

  2. None
  3. ࠓ೔࿩͢͜ͱ • Kubernetes ͷΞʔΩςΫνϟʹ͍ͭͯ • Kubernetes ͷιʔείʔυΛಡΉࡍʹ
 ໾ʹཱͭ࿩ Go ͷษڧձ͚ͩͲ

    Kubernetes ͱ͸ͳΜͧ΍
 Έ͍ͨͳ࿩͕݁ߏ͋Γ·͢ (Gopher ͘Μ͸͍ͬͺ͍ग़·͢)
  4. Kubernetes ͱ͸

  5. Kubernetes ͱ͸ • ίϯςφΛಈ͔ͨ͢ΊͷϓϥοτϑΥʔϜ • ίϯςφΛ͍͍ײ͡ʹӡ༻͢ΔͨΊͷػೳΛ ͍ͬͺ͍͍࣋ͬͯΔ • σϓϩΠ, εέʔϦϯά,

    ࣗݾम෮ etc… k8s ͬͯॻ͔ΕͨΓ͢Δͷ͸
 Kubernetes ͕ (k + 8 จࣈ + s) ͔ͩΒ
  6. Kubernetes ͷ
 ΞʔΩςΫνϟ

  7. • ΞʔΩςΫνϟʹ͍ͭͯཧղ͢Δͱ,
 ࣗ෼ͷಡΈ͍ͨ෦෼ͷίʔυ͕
 Ͳͷίϯϙʔωϯτʹ͋Δͷ͔Θ͔Δ • ͦΕͧΕͷίϯϙʔωϯτͱ
 ͦͷ੹຿ʹ͍ͭͯཧղ͠Α͏ Kubernetes ͷΞʔΩςΫνϟ ࣍ͷϖʔδ͔Βͬ͘͟Γͱ


    ਤΛ༻͍ͯઆ໌͠·͢
  8. Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API

  9. Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API

  10. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd

  11. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd API Server

    Kubernetes ͷ ϦιʔεΛ؅ཧ͢Δ API Server શͯͷϦιʔεૢ࡞͸ API Server Λܦ༝ͯ͠ߦ͏
  12. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd Contoroller
 Manager

    API Server Ͱ͸Ϧιʔε৘ใͷ CRUD ͙Β͍͔͠ߦͳͬͯ ͓ΒͣɺϦιʔεʹର͢Δ࣮ࡍͷॲཧͷத਎͸ Controller ͱݺ͹ΕΔίϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ Controller (goroutine) Controller (goroutine) Controller (goroutine) ىಈ
  13. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd Scheduler Pod

    (ίϯςφ) Λϊʔυ΁ׂΓ౰ͯΔ
  14. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd ϦιʔεͷӬଓԽʹ࢖ΘΕΔ෼ࢄ KVS

    etcd
  15. Kubernetes ͷΞʔΩςΫνϟ Master Node Node API API

  16. Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet kubelet

  17. Kubernetes ͷΞʔΩςΫνϟ API Server kubelet kube-proxy Pod Pod Pod Internet

    kubelet ֤ϊʔυͰಈ࡞͢Δ Pod Λىಈ, ؅ཧ͢ΔΤʔδΣϯτ kubelet
  18. Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet kube-proxy

    kubelet ֤ϊʔυͰಈ࡞͢Δ Service ͷఆٛΛݩʹϧʔςΟϯά ͯ͘͠ΕΔωοτϫʔΫϓϩΩγ kube-proxy
  19. Kubernetes ͷΞʔΩςΫνϟ API Server kube-proxy Pod Pod Pod Internet Pod

    Pod Pod kubelet Container Runtime Docker Ҏ֎΋ಈ͘ Pod Pod Pod
  20. Kubernetes ͷ ιʔείʔυʹ͍ͭͯ

  21. Kubernetes ͷιʔείʔυʹ͍ͭͯ • github.com/kubernetes/kubernetes • Go Modules ରԠͯ͠ΔͷͰ go mod

    ίϚϯ υΛ࣮ߦ͢ΔͱϏϧυͰ͖ΔΑ͏ʹͳΔ • ಡΉ࣌͸ IDE ͰίʔυδϟϯϓΛ࢖͓͏ 4݄͙Β͍ʹ Go Modules ରԠͨ͠
 https://github.com/kubernetes/kubernetes/pull/74877
  22. Kubernetes ͷιʔείʔυʹ͍ͭͯ • ϒϥ΢β͔Β௥͏࣌͸ SourceGraph Λ
 ೖΕΔͱศར • ϒϥ΢β͔Β Code

    Jump Ͱ͖Δ • https://about.sourcegraph.com/
  23. Kubernetes ͷιʔείʔυʹ͍ͭͯ • Kubernetes ͷίϯϙʔωϯτͷ࣮૷Ͱ͸
 github.com/spf13/cobra ͕࢖ΘΕ͍ͯΔ • Server ͱͯ͠ৼΔ෣͏΋ͷ΋

    cobra Ͱ࣮૷͞Ε͍ͯΔ • ࣮ߦ࣌ͷ Option ͰύϥϝʔλΛ౉ͨ͠ΓͰ͖Δ
  24. main package ͸ Ͳ͜ʹ͋Δͷ͔

  25. main package ͸Ͳ͜ʹ͋Δͷ͔ • cmd package ͷதΛݟΔͱ֤ίϯϙʔωϯτ͝ ͱʹ͞Βʹ package ͕෼͔Ε͍ͯΔ

  26. github.com/kubernetes/ kubernetes/cmd/ ͖ͬ͞ݟͨΑ͏ͳ໊લ͕ͪΒ΄Β

  27. • main package ͸ͦͷ഑Լʹ͋Δ main package ͸Ͳ͜ʹ͋Δͷ͔

  28. None
  29. Controller ʹ͍ͭͯ

  30. Kubernetes ͷΞʔΩςΫνϟ API Server Contoroller
 Manager Scheduler etcd Contoroller
 Manager

    API Server Ͱ͸Ϧιʔε৘ใͷ CRUD ͙Β͍͔͠ߦͳͬͯ ͓ΒͣɺϦιʔεʹର͢Δ࣮ࡍͷॲཧͷத਎͸ Controller ͱݺ͹ΕΔίϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ Controller (goroutine) Controller (goroutine) Controller (goroutine) ىಈ ͓͞Β͍
  31. • Ϧιʔεʹର͢Δ࣮ࡍͷॲཧͷத਎͸ Controller ͱݺ͹ΕΔί ϯϙʔωϯτ͕ඇಉظͰߦͳ͍ͬͯΔ • ྫ͑͹ Deployment ͷಈ͖ͷ࣮૷͕ݟ͍ͨʂ
 ͬͯͳͬͨ࣌ʹ

    API Server ͷίʔυΛಡΈʹߦͬͯ΋
 Կ΋ॻ͍ͯͳ͍ • Controller Λಡ΋͏ Controller ʹ͍ͭͯ ͜ͷ͜ͱΛ஌Βͣʹ࠷ॳ API Server ͷίʔυಡΜͰ·ͨ͠
  32. Controller ʹ͍ͭͯ Contoroller
 Manager Controller (goroutine) Controller (goroutine) Controller (goroutine)

    ىಈ • ࣮ࡍʹͲΜͳײ͡ͳͷ͔ίʔυͰݟͯΈΔ
  33. ͜Μͳײ͡ͷ map Ͱఆٛ͞Ε͍ͯΔ https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/ controllermanager.go#L341-L384

  34. Controller ͷ໊લͱ Controller Λ࣮ߦ͢ΔͨΊͷؔ਺

  35. func StartControllers() Ͱ Controller ΛൃՐ (ΊͬͪΌίʔυ࡟ͬͯΔ) https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/ controllermanager.go#L461-L501

  36. ͖ͬ͞ͷ map Λ for Ͱճͯ͠ ͜͜Ͱ Controller ͷ࣮ߦؔ਺ΛݺΜͰΔ

  37. ྫͱͯ͠ Deployment ͷ Controller ͷ࣮ߦؔ਺ https://github.com/kubernetes/kubernetes/blob/v1.14.2/ cmd/kube-controller-manager/app/apps.go#L82-L97

  38. ίϯετϥΫλͰ Controller Λ࡞ͬͯ goroutine Ͱ Controller Λ࣮ߦ

  39. ·ͱΊ • Kubernetes ͷΞʔΩςΫνϟ͸
 ͓֮͑ͯ͘ͱ໾ʹཱͭ • Kubernetes ͷιʔείʔυ͸ҙ֎ʹಡΊΔ • Go

    ͸ಡΈ΍͍͢ ΈΜͳ΋ಡ΋͏
  40. ͓·͚

  41. ໾ʹཱͭϦϯΫͱ͔ • github.com/jamiehannaford/what-happens-when-k8s • Kubernetes ͕ىಈ͔ͯ͠ΒͲΜͳ͜ͱΛ͍ͯ͠Δ͔
 ιʔείʔυϨϕϧͰղઆͯ͘͠ΕΔ • ͳΜͱ೔ຊޠ ver

    ΋͋Δ • ιʔείʔυͷϦϯΫ͕ݹ͔ͬͨΓ, ϦϯΫ੾Εͩͬͨ Γ͢ΔͷͰ PR Λग़ͦ͏! (ग़ͯ͠Δ)
  42. ໾ʹཱͭϦϯΫͱ͔ • https://gopherize.me/ • Զ͚ͩͷ࠷ߴʹ͔Θ͍͍ Gopher ܅Λ࡞ΕΔαΠτ

  43. ͓͠·͍