基于kubernetes 1.9版本源码
启动
入口地址在\cmd\kube-apiserver\apiserver.go\main()
函数很短,里面只有三句重要的代码
s := options.NewServerRunOptions() //新生成一个ServerRunOptions对象
s.AddFlags(pflag.CommandLine) //ServerRunOptions填值
......
if err := app.Run(s, stopCh); err != nil { //生成一个API Server实例
NewServerRunOptions()
调用了\cmd\kube-apiserver\app\options\options.go\NewServerRunOptions()
函数, 里面做的事件很简单, 就是初始化一个ServerRunOptions
结构体, 这个是API Server中的关键结构体
type ServerRunOptions struct {
GenericServerRunOptions *genericoptions.ServerRunOptions // 服务器通用的运行参数
Etcd *genericoptions.EtcdOptions
SecureServing *genericoptions.SecureServingOptions
InsecureServing *kubeoptions.InsecureServingOptions
Audit *genericoptions.AuditOptions
Features *genericoptions.FeatureOptions
Admission *genericoptions.AdmissionOptions
Authentication *kubeoptions.BuiltInAuthenticationOptions
Authorization *kubeoptions.BuiltInAuthorizationOptions
CloudProvider *kubeoptions.CloudProviderOptions
StorageSerialization *kubeoptions.StorageSerializationOptions
APIEnablement *kubeoptions.APIEnablementOptions
AllowPrivileged bool // 是否配置超级权限,即允许Pod中运行的容器拥有系统特权
EnableLogsHandler bool
EventTTL time.Duration // 事件留存事件, 默认1h
KubeletConfig kubeletclient.KubeletClientConfig // K8S kubelet配置
KubernetesServiceNodePort int
MaxConnectionBytesPerSec int64
ServiceClusterIPRange net.IPNet // TODO: make this a list
ServiceNodePortRange utilnet.PortRange
SSHKeyfile string // 指定的话,可以通过SSH指定的秘钥文件和用户名对Node进行访问
SSHUser string ProxyClientCertFile string
ProxyClientKeyFile string EnableAggregatorRouting bool MasterCount int
EndpointReconcilerType string }
上面的genericoptions
定义在\staging\src\k8s.io\apiserver\pkg\server\options
里面
初始化完成之后,最重要的任务就是启动实例了。所有的操作都是在run函数中执行,app.run()
接口实现在cmd/kube-apiserver/app/server.go
func Run(runOptions *options.ServerRunOptions, stopCh <-chan struct{}) error {
// To help debugging, immediately log version
glog.Infof("Version: %+v", version.Get())
server, err := CreateServerChain(runOptions, stopCh)
if err != nil {
return err
}
return server.PrepareRun().Run(stopCh)
}