這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
k8s
中用到一個hyperkube
模組,其功能如下:
// Package hyperkube is a framework for kubernetes server components. It
// allows us to combine all of the kubernetes server components into a single
// binary where the user selects which components to run in any individual
// process.
//
// Currently, only one server component can be run at once. As such there is
// no need to harmonize flags or identify logs across the various servers. In
// the future we will support launching and running many servers — either by
// managing processes or running in-proc.
//
// This package is inspired by https://github.com/spf13/cobra. However, as
// the eventual goal is to run multiple servers from one call, a new package
// was needed.
通俗地講,hyperkube
模組就是把各種功能整合到一個可執行檔,然後在運行時指定模組。比如km
程式就用到了hyperkube
:
$ km --helpThis is an all-in-one binary that can run any of the various Kubernetes-Mesosservers.Usage km <server> [flags]Servers apiserver The main API entrypoint and interface to the storage system. The API server is also the focal point for all authorization decisions.......
hyperkube
結構體定義:
type HyperKube struct { Name string // The executable name, used for help and soft-link invocation Long string // A long description of the binary. It will be world wrapped before output. servers []Server baseFlags *pflag.FlagSet out io.Writer helpFlagVal bool}
其中用到的Server
結構體的定義:
// Server describes a server that this binary can morph into.type Server struct { SimpleUsage string // One line description of the server. Long string // Longer free form description of the server Run serverRunFunc // Run the server. This is not expected to return. flags *pflag.FlagSet // Flags for the command (and all dependents) name string hk *HyperKube}
參看km
程式的main
函數:
func main() { hk := HyperKube{ Name: "km", Long: "This is an all-in-one binary that can run any of the various Kubernetes-Mesos servers.", } hk.AddServer(NewKubeAPIServer()) hk.AddServer(NewControllerManager()) hk.AddServer(NewScheduler()) hk.AddServer(NewKubeletExecutor()) hk.AddServer(NewKubeProxy()) hk.AddServer(NewMinion()) hk.RunToExit(os.Args)}
main
函數用到了hyperkube
的一個重要方法AddServer
:
// AddServer adds a server to the HyperKube object.func (hk *HyperKube) AddServer(s *Server) { hk.servers = append(hk.servers, *s) hk.servers[len(hk.servers)-1].hk = hk}
可以看到,在這個方法中,hk.servers[len(hk.servers)-1].hk = hk
可以讓Server
結構體的hk
欄位指向同一個binary
中的hyperkube
,這樣就把這些功能整合到一起。 接下來調用hyperkube
的RunToExit
運行相應的功能。