Kubernetes 滚动升级(2)

这里我们可以看到,RS可以完全控制Pod.这里有两个watch,rsController和podController,他们分别负责watch ETCD中RS和Pod的变化。这里一个重要的对象不得不提,那就是syncHandler,这个是所有Controller都有的对象。每一个控制器通过Watch来监视ETCD中的变化,使用sync的方式来同步这些对象的状态,注意这个Handler只是一个委托,实际真正的Handler在创建控制器的时候指定。这种模式不仅仅适用于RS,其他控制器亦如此。
下面的逻辑更加清晰地说明了watch的逻辑。

rsc.rsStore.Store, rsc.rsController = framework.NewInformer( &cache.ListWatch{ ListFunc: func(options api.ListOptions) (runtime.Object, error) { return rsc.kubeClient.Extensions().ReplicaSets(api.NamespaceAll).List(options) }, WatchFunc: func(options api.ListOptions) (watch.Interface, error) { return rsc.kubeClient.Extensions().ReplicaSets(api.NamespaceAll).Watch(options) }, }, &extensions.ReplicaSet{}, // TODO: Can we have much longer period here? FullControllerResyncPeriod, framework.ResourceEventHandlerFuncs{ AddFunc: rsc.enqueueReplicaSet, UpdateFunc: rsc.updateRS, // This will enter the sync loop and no-op, because the replica set has been deleted from the store. // Note that deleting a replica set immediately after scaling it to 0 will not work. The recommended // way of achieving this is by performing a `stop` operation on the replica set. DeleteFunc: rsc.enqueueReplicaSet, }, )

每次Watch到ETCD中的对象的变化,采取相应的措施,具体来说就是放入队列,更新或者取出队列。对于Pod来说,也有相应的处理。

podInformer.AddEventHandler(framework.ResourceEventHandlerFuncs{ AddFunc: rsc.addPod, // This invokes the ReplicaSet for every pod change, eg: host assignment. Though this might seem like // overkill the most frequent pod update is status, and the associated ReplicaSet will only list from // local storage, so it should be ok. UpdateFunc: rsc.updatePod, DeleteFunc: rsc.deletePod, })

RS基本的内容就这些,在RS的上层是Deployment,这个对象也是一个控制器。

// DeploymentController is responsible for synchronizing Deployment objects stored // in the system with actual running replica sets and pods. type DeploymentController struct { client clientset.Interface eventRecorder record.EventRecorder // To allow injection of syncDeployment for testing. syncHandler func(dKey string) error // A store of deployments, populated by the dController dStore cache.StoreToDeploymentLister // Watches changes to all deployments dController *framework.Controller // A store of ReplicaSets, populated by the rsController rsStore cache.StoreToReplicaSetLister // Watches changes to all ReplicaSets rsController *framework.Controller // A store of pods, populated by the podController podStore cache.StoreToPodLister // Watches changes to all pods podController *framework.Controller // dStoreSynced returns true if the Deployment store has been synced at least once. // Added as a member to the struct to allow injection for testing. dStoreSynced func() bool // rsStoreSynced returns true if the ReplicaSet store has been synced at least once. // Added as a member to the struct to allow injection for testing. rsStoreSynced func() bool // 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 // Deployments that need to be synced queue workqueue.RateLimitingInterface }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8013ed37027c40fcd615545c07f74b31.html