In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to understand the Main function of Kubernetes". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Main function of Kubernetes.
Main function:
After reading the source code of Kubernetes, you will find that Kubernetes uses the corba framework, so the Main functions of each component are more or less the same, as follows: kube-controller-manager 's main function
K8s.io/kubernetes/cmd/kube-controller-manager/controller-manager.gofunc main () {rand.Seed (time.Now (). UTC (). UnixNano ()) command: = app.NewControllerManagerCommand () / / generate Command in corba format, add flag and initialize the functions of Command / / TODO: once we switch everything over to Cobra commands, we can go back to calling / / utilflag.InitFlags () (by removing its pflag.Parse () call). For now, we have to set the / / normalize func and add the go flag set by hand. Pflag.CommandLine.SetNormalizeFunc (utilflag.WordSepNormalizeFunc) pflag.CommandLine.AddGoFlagSet (goflag.CommandLine) / / utilflag.InitFlags () logs.InitLogs () defer logs.FlushLogs () if err: = command.Execute (); err! = nil {/ / execute Command fmt.Fprintf (os.Stderr, "% v\ n", err) os.Exit (1)} NewControllerManagerCommand function:
First, call the NewKubeControllerManagerOptions function of the options package to generate KubeControllerManagerOptions, which assigns default values to some parameters. The default values of the parameters can be found in the code package: k8s.io/kubernetes/pkg/controller/apis/config/v1alpha1/defaults.go and the last 10 lines of the NewKubeControllerManagerOptions function.
Generate the Command of Kube-controller-manager
Generate the parameters of each controller of controller-manager and add them to the Flag of kube-controller-manager 's Command. The parameters of controller can view the AddFlags functions in each controller package in the k8s.io/kubernetes/cmd/kube-controller-manager/app/options library and the k8s.io/kubernetes/cmd/controller-manager/app/options library.
Set the usage and help functions of Kube-controller-manager
K8s.io/kubernetes/cmd/kube-controller-manager/app/controllermanager.go:77func NewControllerManagerCommand () * cobra.Command {s, err: = options.NewKubeControllerManagerOptions () / / generate KubeControllerManagerOptions structure if err! = nil {glog.Fatalf ("unable to initialize command options:% v", err)} cmd: = & cobra.Command {/ / generate Command Use of kube-controller-manager: "kube-controller-manager" Long: `The Kubernetes controller manager is a daemon that embedsthe core control loops shipped with Kubernetes. In applications of robotics andautomation, a control loop is a non-terminating loop that regulates the state ofthe system. In Kubernetes, a controller is a control loop that watches the sharedstate of the cluster through the apiserver and makes changes attempting to move thecurrent state towards the desired state. Examples of controllers that ship withKubernetes today are the replication controller, endpoints controller, namespacecontroller, and serviceaccounts controller.`, Run: func (cmd * cobra.Command, args [] string) {verflag.PrintAndExitIfRequested () utilflag.PrintFlags (cmd.Flags ()) c, err: = s.Config (KnownControllers (), ControllersDisabledByDefault.List ()) if err! = nil {fmt.Fprintf (os.Stderr, "% v\ n" Err) os.Exit (1)} if err: = Run (c.Complete (), wait.NeverStop) Err! = nil {fmt.Fprintf (os.Stderr, "% v\ n", err) os.Exit (1)},} fs: = cmd.Flags () namedFlagSets: = s.Flags (KnownControllers (), ControllersDisabledByDefault.List ()) / / generate the parameter for _ for each controller of controller-manager F: = range namedFlagSets.FlagSets {/ / add the parameters of each controller to the flag of Command fs.AddFlagSet (f)} usageFmt: = "Usage:\ n% s\ n" cols, _, _: = apiserverflag.TerminalSize (cmd.OutOrStdout ()) cmd.SetUsageFunc (func (cmd * cobra.Command) error {/ / set Usage function fmt.Fprintf (cmd.OutOrStderr (), usageFmt) of Kube-controller-manager Cmd.UseLine () apiserverflag.PrintSections (cmd.OutOrStderr (), namedFlagSets, cols) return nil}) cmd.SetHelpFunc (func (cmd * cobra.Command, args [] string) {/ / set Kube-controller-manager 's help function fmt.Fprintf (cmd.OutOrStdout (), "% s\ n\ n" + usageFmt, cmd.Long, cmd.UseLine () apiserverflag.PrintSections (cmd.OutOrStdout (), namedFlagSets, cols)}) return cmd} command.Execute Namely command.Run function
After the command is generated, the command.Execute function is executed, that is, the Run function defined for command in the previous step NewKubeControllerManagerOptions function is executed. Note: corba's command.Execute method has a lot of checks, there will be parent command and command itself will have a lot of functions (i.e PreRun,PostRun), interested can read the github.com/spf13/cobra project, because kube-controller-manager only defines the Run function, so here directly look at the Run function.
Check if you have the-- version parameter, and if so, print the version of Kubernetes and exit the kube-controller-manager program
Print the value of all parameters and parameters to the log
The Config method of option validation all controller and generate controller manager config objective according to the Master,Kubeconfig,ContentType,Qps,Burst parameter of options
Execute the Run function in this package
K8s.io/kubernetes/cmd/kube-controller-manager/app/controllermanager.go:93Run: func (cmd * cobra.Command, args [] string) {verflag.PrintAndExitIfRequested () / / check whether there is a-- version parameter, if so, print the version of Kubernetes and exit the program utilflag.PrintFlags (cmd.Flags ()) / / print all parameters and their value to the log / / Config method to validation all controller, and do validation according to options's Master,Kubeconfig,ContentType,Qps The Burst parameter generates controller manager config objective c, err: = s.Config (KnownControllers (), ControllersDisabledByDefault.List ()) if err! = nil {fmt.Fprintf (os.Stderr, "% v\ n", err) os.Exit (1)} if err: = Run (c.Complete (), wait.NeverStop) Err! = nil {/ / execute the Run function in this package fmt.Fprintf (os.Stderr, "% v\ n", err) os.Exit (1)}} Run function of the controllermanager.go package
Start the http service, listen to the health interface, and provide the metrics interface to prometheus
Define the run function, mainly to define client, and the startup functions that start all controller,controller are in k8s.io/kubernetes/cmd/kube-controller-manager/app/core.go
If leader-elect is not enabled, execute the above run function directly
If leader-elect is enabled, execute the run function seen above after the election
K8s.io/kubernetes/cmd/kube-controller-manager/app/controllermanager.go:141func Run (c * config.CompletedConfig, stopCh)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.