Basic principles of kubernetes replicaset

1 Overview

The replicaset object describes a [application] of a [specific version], and controls the attributes (images, container environment variables, etc.) and number of pods.
Note:
1) The replicaset object does not control the version of the pod object, but is for the same mirrored pod.
2) The deployment object controls the replicaset object, which can describe an application, because applications are divided into versions.

2) General principles

The controller of the replicaset will monitor the pod object and replicaset object of kuberntes, and the changes of these monitored objects will trigger the syncLoop loop (called the syncReplicaSet() method) to let the kubernetes cluster evolve toward the state described by the replicaset object.

3) Implementation details

Each time the controller executes the callback method syncReplicaSet(), the following operations are performed:

1) Get the rs object according to the
input parameter string key: namespace, name, err := cache.SplitMetaNamespaceKey(key)
rs, err := rsc.rsLister.ReplicaSets(namespace).Get(name)

2) Get a boolean value: check whether the operation of the number of synchronized pods needs to be really performed
rsNeedsSync := rsc.expectations.SatisfiedExpectations(key)

3) Get the pods under the rs object. This pod list is called filteredPods:
allPods, err := rsc.podLister.Pods(rs.Namespace).List(labels.Everything())
filteredPods := controller.FilterActivePods(allPods)
filteredPods , err = rsc.claimPods(rs, selector, filteredPods)

4) According to the value of rsNeedsSync, decide whether to synchronize the [number] of pods: actually create or delete pods: create or delete pods in
batches rsc.manageReplicas(filteredPods, rs)

5) Update the status field of the rs object

4) Simple flow chart of source code


								SatisfiedExpectations(此机制决定是否真的执行manageReplicas()方法)
									|
									|
									∨									
rs对象的变更或pod对象的变更 --> syncReplicaSet方法 --> manageReplicas(保持数量一致性:为rs创建pod或删除pod)  -->  1.判断究竟是新建pod还是删除pod  
																									     2. 创建为rs对象创建一个expectations对象,并设置add值/del值 
																									     3.调用slowStartBatch()方法批量创建pod 或 删除经过筛选出的多余的pod  
																								  	     4.更新rs对象关联的expectations对象
										|
										|
									    ∨
								updateReplicaSetStatus(更新rs对象的status字段)	

Guess you like

Origin blog.csdn.net/nangonghen/article/details/106153278