Kubernetes——ReplicaSet控制器

ReplicaSet 控制器

  Kubernetes 较早期的版本中仅有 ReplicationController 一种类型的 Pod 控制器,后来的版本中陆续引入了更多的控制器实现,这其中就包括用来取代 ReplicationController 的新一代实现 ResplicaSet。

  事实上,除了额外支持基于集合(set-based)的标签选择器,以及它的滚动更新(Rolling-Update)机制要基于更高级的控制 Deployment 实现之外,目前的 ReplicaSet 的其余功能基本上与 ReplicationController 相同。

  ReplicationController 废弃了,这里只介绍 ReplicaSet控制器。

一、RS概述

  ReplicaSet(简称 RS)是 Pod 控制器类型的一种实现,用于确保由其管控的 Pod 对象副本数在任一时刻都能精确满足期望的数量。

  ReplicaSet 控制器资源启动后会查找集群中匹配其标签选择器的 Pod 资源对象,当前活动对象的数量于期望的数量不吻合时,多则删除,少则通过 Pod 模版创建以补足,等 Pod 资源副本数量符合期望值后即进入下一轮和解循环。

  ReplicaSet 的副本数量、标签选择器甚至是 Pod 模板都可以随时按需进行修改,不仅仅改动期望的副本数量会对现存的 Pod 副本产生直接影响。修改标签选择器可能会使得现有的 Pod 副本的标签变得不再匹配,此时 ReplicaSet 控制器要做的不过是不再计入它们而已。

  另外,在创建完成后,ReplicaSet 也不会再关注 Pod 对象中的实际内容,因此 Pod 模板的改动也只会对后来新建的 Pod 副本产生影响。

  对比手动创建和管理 Pod 资源来说,ReplicaSet 创建和管理,优势如下:

    • 确保 Pod 资源对象的数量精确反映期望值:ReplicaSet 需要确保由其控制运行的 Pod 副本数量精确吻合配置中定义的期望值,否则就会自动补足所缺或终止所余。
    • 确保 Pod 健康运行:探测到由其管控的 Pod 对象因其所在的工作节点故障而不可用时,自动请求由调度器于其他工作节点创建缺失的 Pod 副本。
    • 弹性伸缩:业务规模因各种原因时常存在明显波动,在波峰或波谷期间,可以通过 ReplicaSet 控制器动态调整相关 Pod 资源对象的数量。此外,在必要时还可以通过 HPA(HroizontalPodAutoscaler)控制器实现 Pod 资源规模的自动伸缩。

二、创建 ReplicaSet

  类似于 Pod 资源,创建 ReplicaSet 控制器对象同样可以使用 YAML 或 JSON 格式的清单文件定义其配置,而后使用相关的创建命令来完成资源创建。它的 FIELDS 如下:

kubectl explain rs
KIND:     ReplicaSet
VERSION:  apps/v1

DESCRIPTION:
     ReplicaSet ensures that a specified number of pod replicas are running at
     any given time.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     If the Labels of a ReplicaSet are empty, they are defaulted to be the same
     as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
     info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec	<Object>
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status	<Object>
     Status is the most recently observed status of the ReplicaSet. This data
     may be out of date by some window of time. Populated by the system.
     Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

  spec 字段一般嵌套使用以下几个属性字段,如下:

kubectl explain rs.spec
KIND:     ReplicaSet
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     ReplicaSetSpec is the specification of a ReplicaSet.

FIELDS:
   minReadySeconds	<integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   replicas	<integer>
     Replicas is the number of desired replicas. This is a pointer to
     distinguish between explicit zero and unspecified. Defaults to 1. More
     info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller

   selector	<Object> -required-
     Selector is a label query over pods that should match the replica count.
     Label keys and values that must match in order to be controlled by this
     replica set. It must match the pod template's labels. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

   template	<Object>
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

三、ReplicaSet 管控下的 Pod 对象

  "kubectl describe replicasets"  命令可以打印出 ReplicaSet 控制器的详细状态。

  事实上,ReplicaSet 控制器能够对 Pod 对象数目的异常及时作出响应,是因为它向 API Server 注册监听(watch)了相关资源及其列表的变动信息,于是 API Server 会在变动发生时立即通知给相关的监听客户端。

四、更新 ReplicaSet 控制器

1、更改 Pod 模板:升级应用

  ReplicaSet 控制器的 Pod 模板可随时按需修改,但它仅影响这之后由其新建的 Pod 对象,对已有的副本不会产生作用。创建新的副本时,只影响新的副本,除非调整副本数为0,再创建新的副本数。

2、扩容和缩容

  改动 ReplicaSet 控制器对象配置中期望的 Pod 副本数量(replicas 字段)会由控制器实时做出响应,从而实现应用规模的水平伸缩。replicas 的修改及应用方式同 Pod 模板,不过,kubectl 还提供了一个专用的子命令 scale 用于实现应用规模的伸缩,它支持从资源清单文件中获取新的目标副本数量,也可以直接在命令行通过 "--replicas" 选项进行读取,命令如下:

kubectl scale replicasets pod-name --replicas=5

  另外,kubectl sacle 命令还支持在现 Pod 副本数量符合指定的值时才执行扩展操作,这仅需要为命令使用 "--current-replicas" 选项即可。例如,下面的命令表示如果 pod-name 目前的 Pod 副本数量为 2,就将其扩展至 4 个:

kubectl  scale replicasets pod-name --current-replicas=2 --replicas=4

五、删除 ReplicaSet 控制器资源

  使用 kubectl delete 命令删除 ReplicaSet 对象时默认会一并删除其管控的各 Pod 对象。

kubectl delete replicas rs-name --cascade=false

  删除操作完成后,此前由 rs-name 控制器管控的各 Pod 对象仍处于活动状态,但它们完成变成了自主式 Pod 资源,用户需要自行组织和维护好它们。

posted @ 2022-06-16 18:05  左扬  阅读(216)  评论(0编辑  收藏  举报
levels of contents