共计 5318 个字符,预计需要花费 14 分钟才能阅读完成。
本篇内容主要讲解“Kubernetes Replication Controller 的结构定义是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“Kubernetes Replication Controller 的结构定义是什么”吧!
ReplicationManager
ReplicationManager 就是 ReplicationController 控制器对象,方便在代码中和 ReplicationController Resource API Object 进行区分。下面代码是 ReplicationManager 的结构定义。
pkg/controller/replication/replication_controller.go:75
// ReplicationManager is responsible for synchronizing ReplicationController objects stored in the system with actual running pods.
type ReplicationManager struct {
kubeClient clientset.Interface
podControl controller.PodControlInterface
// internalPodInformer is used to hold a personal informer. If we re using
// a normal shared informer, then the informer will be started for us. If
// we have a personal informer, we must start it ourselves. If you start
// the controller using NewReplicationManager(passing SharedInformer), this
// will be null
internalPodInformer cache.SharedIndexInformer
// An rc is temporarily suspended after creating/deleting these many replicas.
// It resumes normal action after observing the watch events for them.
burstReplicas int
// To allow injection of syncReplicationController for testing.
syncHandler func(rcKey string) error
// A TTLCache of pod creates/deletes each rc expects to see.
expectations *controller.UIDTrackingControllerExpectations
// A store of replication controllers, populated by the rcController
rcStore cache.StoreToReplicationControllerLister
// Watches changes to all replication controllers
rcController *cache.Controller
// A store of pods, populated by the podController
podStore cache.StoreToPodLister
// Watches changes to all pods
podController cache.ControllerInterface
// podStoreSynced returns true if the pod store has been synced at least once.
// Added as a member to the struct to allow injection for testing.
podStoreSynced func() bool
lookupCache *controller.MatchingCache
// Controllers that need to be synced
queue workqueue.RateLimitingInterface
// garbageCollectorEnabled denotes if the garbage collector is enabled. RC
// manager behaves differently if GC is enabled.
garbageCollectorEnabled bool
}
重点对下面个几个对象介绍说明:
podControl: 提供 Create/Delete Pod 的操作接口。
burstReplicas: 每次批量 Create/Delete Pods 时允许并发的最大数量。
syncHandler: 真正执行 Replica Sync 的函数。
expectation: 维护的期望状态下的 Pod 的 Uid Cache,并且提供了修正该 Cache 的接口。
rcStore: ReplicationController Resource 对象的 Indexer, 数据由 rcController 提供和维护。
rcController: 用来 watch 所有 ReplicationController Resource,watch 到的 change 更新到 rcStore 中。
podStore: Pod 的 Indexer,数据由 podController 提供和维护。
podController: 用来 watch 所有 Pod Resource,watch 到的 change 更新到 podStore 中。
queue: 用来存放待 sync 的 RC,是一个 RateLimit 类型的 queue。
lookupCache: 提供 Pod 和 RC 匹配信息的 cache,以提高查询效率。
ReplicationController 在何处启动的
看过我我的博文: Kubernetes ResourceQuota Controller 内部实现原理及源码分析的可能有印象,里面也提到了 controller manager 是如何启动 ResourceQuotaController 的,ReplicationController 也是一样的。在 kube-controller-manager 调用 newControllerInitializers 进行控制器初始化的时候,将 startReplicationController 注册进去了,用来启动 ReplicationController 控制器。
cmd/kube-controller-manager/app/controllermanager.go:224
func newControllerInitializers() map[string]InitFunc {controllers := map[string]InitFunc{}
controllers[endpoint] = startEndpointController
controllers[replicationcontroller] = startReplicationController
controllers[podgc] = startPodGCController
controllers[resourcequota] = startResourceQuotaController
controllers[namespace] = startNamespaceController
controllers[serviceaccount] = startServiceAccountController
controllers[garbagecollector] = startGarbageCollectorController
controllers[daemonset] = startDaemonSetController
controllers[job] = startJobController
controllers[deployment] = startDeploymentController
controllers[replicaset] = startReplicaSetController
controllers[horizontalpodautoscaling] = startHPAController
controllers[disruption] = startDisruptionController
controllers[statefuleset] = startStatefulSetController
controllers[cronjob] = startCronJobController
controllers[certificatesigningrequests] = startCSRController
return controllers
}
代码继续跟到 startReplicationController,很简单,启动一个 goroutine,调用 replicationcontroller.NewReplicationManager 创建一个 ReplicationManager 并执行其中 Run 方法开始工作。
cmd/kube-controller-manager/app/core.go:55
func startReplicationController(ctx ControllerContext) (bool, error) {
go replicationcontroller.NewReplicationManager(ctx.InformerFactory.Pods().Informer(),
ctx.ClientBuilder.ClientOrDie(replication-controller),
ResyncPeriod(ctx.Options),
replicationcontroller.BurstReplicas,
int(ctx.Options.LookupCacheSizeForRC),
ctx.Options.EnableGarbageCollector,
).Run(int(ctx.Options.ConcurrentRCSyncs), ctx.Stop)
return true, nil
}
创建 ReplicationManager
上面分析到,controller-manager 通过 NewReplicationManager 创建一个 ReplicationManager 对象,其实就是 ReplicationController 控制器。
pkg/controller/replication/replication_controller.go:122
// NewReplicationManager creates a replication manager
func NewReplicationManager(podInformer cache.SharedIndexInformer, kubeClient clientset.Interface, resyncPeriod controller.ResyncPeriodFunc, burstReplicas int, lookupCacheSize int, garbageCollectorEnabled bool) *ReplicationManager {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(v1core.EventSinkImpl{Interface: kubeClient.Core().Events()})
return newReplicationManager(
eventBroadcaster.NewRecorder(v1.EventSource{Component: replication-controller}),
podInformer, kubeClient, resyncPeriod, burstReplicas, lookupCacheSize, garbageCollectorEnabled)