Kubernetes: Resource: ReplicaSet

 8th March 2021 at 8:34pm

ReplicaSet 跟 ReplicationController 非常像,语法上差异很小。但 ReplicaSet 用来取代 ReplicationController

ReplicaSet 的优势在于:

  • 它的 selector 是 set-based,表达能力更佳;而 rc 是 equality-based。这两者的区别参考 这里
  • 它生成的 pod、或者接管的手工生成的 pod,会带 ownerReferences 属性指向自己;这使得它对 pod 的管理比 rc 更合理(rc 是完全基于 label 的)。Pod 的 yaml 会像这样子:
    apiVersion: v1
    kind: Pod
    metadata:
      creationTimestamp: "2020-02-12T07:06:16Z"
      generateName: frontend-
      labels:
        tier: frontend
      name: frontend-b2zdv
      namespace: default
      ownerReferences:
      - apiVersion: apps/v1
        blockOwnerDeletion: true
        controller: true
        kind: ReplicaSet
        name: frontend
        uid: f391f6db-bb9b-4c09-ae74-6a1f77f3d5cf
    ...

对于 unmanaged pod,如果 ReplicaSet 发现其 label 符合 rs 的 selector,会马上 将其接管,打上 ownerReferences

对于 selector 规则有重叠的情况,k8s 目前(2021 年 3 月)似乎没有处理冲突,只是在 Deployment 的 文档 中提及不要这样做:

Do not overlap labels or selectors with other controllers (including other Deployments and StatefulSets). Kubernetes doesn't stop you from overlapping, and if multiple controllers have overlapping selectors those controllers might conflict and behave unexpectedly.