kube-apiserver啟動流程解析
kube-apiserver

Kubernetes API server 為 api 對象驗證并配置數(shù)據(jù),包括 pods、 services、 replicationcontrollers 和其它 api 對象。API Server 提供 REST 操作和到集群共享狀態(tài)的前端,所有其他組件通過它進行交互
啟動流程
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文件中,目的是創(chuàng)建一個cobra 命名對象。
//?NewAPIServerCommand?creates?a?*cobra.Command?object?with?default?parameters func?NewAPIServerCommand()?*cobra.Command?{ ????//?創(chuàng)建一個新的NewServerRunOptions對象,并配置默認參數(shù) 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配置
這個結(jié)構(gòu)主要是關于一些命令行參數(shù)的解析配置,例如etcd與kubelet client端的參數(shù)配置等,該結(jié)構(gòu)定義在/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函數(shù)定義
主要完成以下操作:
設置默認options - Complelte(s)
驗證options - completedOptions.Validate()
運行真正的Run方法 - Run(completedOptions, genericapiserver.SetupSignalHandler())
運行APIServer命令
執(zhí)行上述定義的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()) ????//?創(chuàng)建apiserver?chain server,?err?:=?CreateServerChain(completeOptions,?stopCh) if?err?!=?nil?{ return?err } return?server.PrepareRun().Run(stopCh) }
創(chuàng)建apiserver chain
路徑為/cmd/kube-apiserver/app/server.go,主要完成以下操作:
創(chuàng)建NodeDialer - CreateNodeDialer(completedOptions)
創(chuàng)建kube-apiserver配置資源 - CreateKubeAPIServerConfig(completedOptions, nodeTunneler, proxyTransport)
創(chuàng)建擴展API配置 - CreateKubeAPIServerConfig(completedOptions, nodeTunneler, proxyTransport)和createAPIExtensionsServer(apiExtensionsConfig, genericapiserver.NewEmptyDelegate())
創(chuàng)建kube-apiserver實例 - CreateKubeAPIServer(kubeAPIServerConfig, apiExtensionsServer.GenericAPIServer, admissionPostStartHook)
運行準備階段 - kubeAPIServer.GenericAPIServer.PrepareRun()和apiExtensionsServer.GenericAPIServer.PrepareRun()
創(chuàng)建Aggregator配置 - createAggregatorConfig()和createAggregatorServer()
運行kube-apiserver
當創(chuàng)建好kube-apiserver所需資源配置時,運行kube-apiserver實例:
server.PrepareRun().Run(stopCh)
流程概述
轉(zhuǎn)自:https://xigang.github.io/2019/11/23/kube-apisever/
Kubernetes
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。