Kubernetes Apiserver介绍一核心服务通用配置genericapiserver.Config

332 阅读2分钟

本节重点总结 :

  • API核心服务需要的通用配置工作中的准备工作
  • 创建和节点通信的结构体proxyTransport ,使用缓存长连接提高效率
  • 创建 clientset
  • 初始化etcd存储

CreateKubeAPIServerConfig创建所需配置解析

  • D:\go_path\src\github.com\kubernetes\kubernetes\cmd\kube-apiserver\app\server.go

创建和节点通信的结构体proxyTransport ,使用缓存长连接提高效率

// 底层使用了http.Transport
proxyTransport := CreateProxyTransport()
  • http.transport功能简介
    • transport的主要功能其实就是缓存了长连接
    • 用于大量http请求场景下的连接复用
    • 减少发送请求时TCP(TLS)连接建立的时间损耗

创建通用配置genericConfig

	genericConfig, versionedInformers, serviceResolver, pluginInitializers, admissionPostStartHook, storageFactory, err := buildGenericConfig(s.ServerRunOptions, proxyTransport)
	
  • 下面是众多的ApplyTo分析

众多ApplyTo分析,并且有对应的AddFlags标记命令行参数

  • 先创建genericConfig
genericConfig = genericapiserver.NewConfig(legacyscheme.Codecs)
  • 以检查https配置的ApplyTo分析
	if lastErr = s.SecureServing.ApplyTo(&genericConfig.SecureServing, &genericConfig.LoopbackClientConfig); lastErr != nil {
		return
	}
  • 底层调用 SecureServingOptions的ApplyTo,有对应的AddFlags方法标记命令行参数,位置D:\go_path\src\github.com\kubernetes\kubernetes\staging\src\k8s.io\apiserver\pkg\server\options\serving.go
func (s *SecureServingOptions) AddFlags(fs *pflag.FlagSet) {
	if s == nil {
		return
	}

	fs.IPVar(&s.BindAddress, "bind-address", s.BindAddress, ""+
		"The IP address on which to listen for the --secure-port port. The "+
		"associated interface(s) must be reachable by the rest of the cluster, and by CLI/web "+
		"clients. If blank or an unspecified address (0.0.0.0 or ::), all interfaces will be used.")

	desc := "The port on which to serve HTTPS with authentication and authorization."
	if s.Required {
		desc += " It cannot be switched off with 0."
	} else {
		desc += " If 0, don't serve HTTPS at all."
	}
	fs.IntVar(&s.BindPort, "secure-port", s.BindPort, desc)

初始化etcd存储

  • 创建存储工厂配置
	storageFactoryConfig := kubeapiserver.NewStorageFactoryConfig()
	storageFactoryConfig.APIResourceConfig = genericConfig.MergedResourceConfig
	completedStorageFactoryConfig, err := storageFactoryConfig.Complete(s.Etcd)
	if err != nil {
		lastErr = err
		return
	}
  • 初始化存储工厂
	storageFactory, lastErr = completedStorageFactoryConfig.New()
	if lastErr != nil {
		return
	}
  • 将存储工厂应用到服务端运行对象中,后期可以通过RESTOptionsGetter获取操作 Etcd 的句柄
	if lastErr = s.Etcd.ApplyWithStorageFactoryTo(storageFactory, genericConfig); lastErr != nil {
		return
	}
func (s *EtcdOptions) ApplyWithStorageFactoryTo(factory serverstorage.StorageFactory, c *server.Config) error {
	if err := s.addEtcdHealthEndpoint(c); err != nil {
		return err
	}

	// use the StorageObjectCountTracker interface instance from server.Config
	s.StorageConfig.StorageObjectCountTracker = c.StorageObjectCountTracker

	c.RESTOptionsGetter = &StorageFactoryRestOptionsFactory{Options: *s, StorageFactory: factory}
	return nil
}

addEtcdHealthEndpoint 创建etcd的健康检测

func (s *EtcdOptions) addEtcdHealthEndpoint(c *server.Config) error {
	healthCheck, err := storagefactory.CreateHealthCheck(s.StorageConfig)
	if err != nil {
		return err
	}
	c.AddHealthChecks(healthz.NamedCheck("etcd", func(r *http.Request) error {
		return healthCheck()
	}))

	if s.EncryptionProviderConfigFilepath != "" {
		kmsPluginHealthzChecks, err := encryptionconfig.GetKMSPluginHealthzCheckers(s.EncryptionProviderConfigFilepath)
		if err != nil {
			return err
		}
		c.AddHealthChecks(kmsPluginHealthzChecks...)
	}

	return nil
}
  • 从CreateHealthCheck得知 ,只支持etcdV3的接口
// CreateHealthCheck creates a healthcheck function based on given config.
func CreateHealthCheck(c storagebackend.Config) (func() error, error) {
	switch c.Type {
	case storagebackend.StorageTypeETCD2:
		return nil, fmt.Errorf("%s is no longer a supported storage backend", c.Type)
	case storagebackend.StorageTypeUnset, storagebackend.StorageTypeETCD3:
		return newETCD3HealthCheck(c)
	default:
		return nil, fmt.Errorf("unknown storage type: %s", c.Type)
	}
}

设置使用 protobufs 用来内部交互,并且禁用压缩功能

  • 因为内部网络速度快,没必要为了节省带宽而将cpu浪费在压缩和解压上
	genericConfig.LoopbackClientConfig.ContentConfig.ContentType = "application/vnd.kubernetes.protobuf"
	// Disable compression for self-communication, since we are going to be
	// on a fast local network
	genericConfig.LoopbackClientConfig.DisableCompression = true

创建 clientset

	kubeClientConfig := genericConfig.LoopbackClientConfig
	clientgoExternalClient, err := clientgoclientset.NewForConfig(kubeClientConfig)
	if err != nil {
		lastErr = fmt.Errorf("failed to create real external clientset: %v", err)
		return
	}
	versionedInformers = clientgoinformers.NewSharedInformerFactory(clientgoExternalClient, 10*time.Minute)
  • versionedInformers 代表k8s-client的informer对象,用于listAndWatch k8s对象的

本节重点总结 :

  • API核心服务需要的通用配置工作中的准备工作
  • 创建和节点通信的结构体proxyTransport ,使用缓存长连接提高效率
  • 创建 clientset
  • 初始化etcd存储