kube-apiserver启动流程解析
kube-apiserver
Kubernetes API server 为 api 对象验证并配置数据,包括 pods、 services、 replicationcontrollers 和其它 api 对象。API Server 提供 REST 操作和到集群共享状态的前端,所有其他组件通过它进行交互
启动流程
kube-apiserver的启动入口路径为/cmd/kube-apiserver/apiserver.go:main(),主要作以下三件事:
生成APIServer启动命令
初始化log
运行APIServer命令
func main() { rand.Seed(time.Now().UnixNano()) command := app.NewAPIServerCommand() // 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. // utilflag.InitFlags() logs.InitLogs() defer logs.FlushLogs() if err := command.Execute(); err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(1) } }
API Server Command配置
app.NewAPIServerCommand()方法定义在/cmd/kube-apiserver/app/server.go文件中,目的是创建一个cobra 命名对象。
// NewAPIServerCommand creates a *cobra.Command object with default parameters func NewAPIServerCommand() *cobra.Command { // 创建一个新的NewServerRunOptions对象,并配置默认参数 s := options.NewServerRunOptions() cmd := &cobra.Command{ Use: "kube-apiserver", Long: `The Kubernetes API server validates and configures data for the api objects which include pods, services, replicationcontrollers, and others. The API Server services REST operations and provides the frontend to the cluster's shared state through which all other components interact.`, // 定义Run方法 RunE: func(cmd *cobra.Command, args []string) error { verflag.PrintAndExitIfRequested() utilflag.PrintFlags(cmd.Flags()) // set default options completedOptions, err := Complete(s) if err != nil { return err } // validate options if errs := completedOptions.Validate(); len(errs) != 0 { return utilerrors.NewAggregate(errs) } return Run(completedOptions, genericapiserver.SetupSignalHandler()) }, } fs := cmd.Flags() namedFlagSets := s.Flags() verflag.AddFlags(namedFlagSets.FlagSet("global")) globalflag.AddGlobalFlags(namedFlagSets.FlagSet("global"), cmd.Name()) options.AddCustomGlobalFlags(namedFlagSets.FlagSet("generic")) for _, f := range namedFlagSets.FlagSets { fs.AddFlagSet(f) } usageFmt := "Usage:\n %s\n" cols, _, _ := term.TerminalSize(cmd.OutOrStdout()) cmd.SetUsageFunc(func(cmd *cobra.Command) error { fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine()) cliflag.PrintSections(cmd.OutOrStderr(), namedFlagSets, cols) return nil }) cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine()) cliflag.PrintSections(cmd.OutOrStdout(), namedFlagSets, cols) }) return cmd }
ServerRunOptions配置
这个结构主要是关于一些命令行参数的解析配置,例如etcd与kubelet client端的参数配置等,该结构定义在/cmd/kube-apiserver/app/options/options.go文件中:
// NewServerRunOptions creates a new ServerRunOptions object with default parameters func NewServerRunOptions() *ServerRunOptions { ... } // ServerRunOptions runs a kubernetes api server. type ServerRunOptions struct { GenericServerRunOptions *genericoptions.ServerRunOptions Etcd *genericoptions.EtcdOptions SecureServing *genericoptions.SecureServingOptionsWithLoopback InsecureServing *genericoptions.DeprecatedInsecureServingOptionsWithLoopback Audit *genericoptions.AuditOptions Features *genericoptions.FeatureOptions Admission *kubeoptions.AdmissionOptions Authentication *kubeoptions.BuiltInAuthenticationOptions Authorization *kubeoptions.BuiltInAuthorizationOptions CloudProvider *kubeoptions.CloudProviderOptions APIEnablement *genericoptions.APIEnablementOptions AllowPrivileged bool EnableLogsHandler bool EventTTL time.Duration KubeletConfig kubeletclient.KubeletClientConfig KubernetesServiceNodePort int MaxConnectionBytesPerSec int64 ServiceClusterIPRange net.IPNet // TODO: make this a list ServiceNodePortRange utilnet.PortRange SSHKeyfile string SSHUser string ProxyClientCertFile string ProxyClientKeyFile string EnableAggregatorRouting bool MasterCount int EndpointReconcilerType string ServiceAccountSigningKeyFile string ServiceAccountIssuer serviceaccount.TokenGenerator ServiceAccountTokenMaxExpiration time.Duration }
Run函数定义
主要完成以下操作:
设置默认options - Complelte(s)
验证options - completedOptions.Validate()
运行真正的Run方法 - Run(completedOptions, genericapiserver.SetupSignalHandler())
运行APIServer命令
执行上述定义的Run方法(路径为/cmd/kube-apiserver/app/server.go):
// Run runs the specified APIServer. This should never exit. func Run(completeOptions completedServerRunOptions, stopCh <-chan struct{}) error { // To help debugging, immediately log version klog.Infof("Version: %+v", version.Get()) // 创建apiserver chain server, err := CreateServerChain(completeOptions, stopCh) if err != nil { return err } return server.PrepareRun().Run(stopCh) }
创建apiserver chain
路径为/cmd/kube-apiserver/app/server.go,主要完成以下操作:
创建NodeDialer - CreateNodeDialer(completedOptions)
创建kube-apiserver配置资源 - CreateKubeAPIServerConfig(completedOptions, nodeTunneler, proxyTransport)
创建扩展API配置 - CreateKubeAPIServerConfig(completedOptions, nodeTunneler, proxyTransport)和createAPIExtensionsServer(apiExtensionsConfig, genericapiserver.NewEmptyDelegate())
创建kube-apiserver实例 - CreateKubeAPIServer(kubeAPIServerConfig, apiExtensionsServer.GenericAPIServer, admissionPostStartHook)
运行准备阶段 - kubeAPIServer.GenericAPIServer.PrepareRun()和apiExtensionsServer.GenericAPIServer.PrepareRun()
创建Aggregator配置 - createAggregatorConfig()和createAggregatorServer()
运行kube-apiserver
当创建好kube-apiserver所需资源配置时,运行kube-apiserver实例:
server.PrepareRun().Run(stopCh)
流程概述
转自:https://xigang.github.io/2019/11/23/kube-apisever/
Kubernetes
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。