RedCloud Help

6. 控制器Replicaset

6.1 Replicaset 控制器:概念、原理解读

6.1.1 Replicaset 概述

ReplicaSet 是kubernetes 中的一种副本控制器,简称rs,主要作用是控制由其管理的pod,使pod 副本的数量始终维持在预设的个数。它的主要作用就是保证一定数量的Pod 能够在集群中正常运行,它 会持续监听这些Pod 的运行状态,在Pod 发生故障时重启pod,pod 数量减少时重新运行新的 Pod 副本。 官方推荐不要直接使用ReplicaSet,用Deployments 取而代之,Deployments 是比ReplicaSet 更高级的 概念,它会管理ReplicaSet 并提供很多其它有用的特性,最重要的是Deployments 支持声明式更新,声 明式更新的好处是不会丢失历史变更。所以Deployment 控制器不直接管理Pod 对象,而是由 Deployment 管理ReplicaSet,再由ReplicaSet 负责管理Pod 对象。

6.1.2 Replicaset 工作原理:如何管理Pod?

Replicaset 核心作用在于代用户创建指定数量的 pod 副本,并确保 pod 副本一直处于满足用户期望的数量, 起到多退少补的作用,并且还具有自动扩容缩容等机制。

Replicaset 控制器主要由三个部分组成:

  • 用户期望的 pod 副本数:用来定义由这个控制器管控的 pod 副本有几个

  • 标签选择器:选定哪些 pod 是自己管理的,如果通过标签选择器选到的 pod 副本数量少于我们指定的数量,需要用到下面的组件

  • pod 资源模板:如果集群中现存的 pod 数量不够我们定义的副本中期望的数量怎么办,需要新 建 pod,这就需要 pod 模板,新建的 pod 是基于模板来创建的。

6.2 Replicaset 资源清单文件编写技巧

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> # 当前资源使用的api版本,跟version: app/v1保持一直 kind <string> 资源类型,跟KIND:replicaSet保持一致 metadata <Object> 元数据定义Replicaset名字的 spec <Object> 定义副本数,定义标签选择器,定义pod模板 status <Object> 状态信息,不能改
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> replicas <integer> 定义的pod副本数,根据我们指定的值创建对应的数量的pod selector <Object> 用于匹配pod的标签选择器 template <Object> 定义pod的模板,基于这个模板定义的所有pod是一样的。
kubectl explain rs.spec.template KIND: ReplicaSet VERSION: apps/v1 RESOURCE: template <Object> DESCRIPTION: 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 PodTemplateSpec describes the data a pod should have when created from a template FIELDS: metadata <Object> Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata spec <Object> Specification of the desired behavior of the pod. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
kubectl explain rs.spec.template.spec

通过上面可以看到,ReplicaSet 资源中有两个spec 字段。第一个spec 声明的是ReplicaSet 定义多 少个Pod 副本(默认将仅部署1 个Pod) 、匹配Pod 标签的选择器、创建pod 的模板。第二个spec 是 spec.template.spec:主要用于Pod 里的容器属性等配置。

.spec.template 里的内容是声明Pod 对象时要定义的各种属性,所以这部分也叫做PodTemplate (Pod 模板)。还有一个值得注意的地方是: 在.spec.selector 中定义的标签选择器必须能够匹配到 spec.template.metadata.labels 里定义的Pod 标签,否则Kubernetes 将不允许创建ReplicaSet。

6.2.1 Replicaset使用案例

# 编写一个车ReplicaSet资源清单 cat replicaset.yaml
apiVersion: apps/v1 kind: ReplicaSet metadata: name: frontend #控制器的名称 namespace: default labels: app: guestbook tier: frontend spec: replicas: 3 #管理的pod副本数量 selector: matchLabels: tier1: frontend1 #管理带有tier=frontend标签的pod template: #定义pod的模板 metadata: labels: tier1: frontend1 #pod标签,一定要有,这样上面控制器就能找到它要管理的pod是那些了 spec: containers: # 定义容器的名称 - name: php-redis #定义容器的名称 image: docker.io/yecc/gcr.io-google_samples-gb-frontend:v3 imagePullPolicy: IfNotPresent ports: # 定义端口 - containerPort: 80 startupProbe: periodSeconds: 5 initialDelaySeconds: 20 timeoutSeconds: 10 httpGet: scheme: HTTP port: 80 path: / livenessProbe: periodSeconds: 5 initialDelaySeconds: 20 timeoutSeconds: 10 httpGet: scheme: HTTP port: 80 path: / readinessProbe: periodSeconds: 5 initialDelaySeconds: 20 timeoutSeconds: 10 httpGet: scheme: HTTP port: 80 path: /
kubectl apply -f replicaset.yaml kubectl get rs kubectl get pods

6.2.2 Replicaset管理pod:扩容/缩容/更新

ReplicaSet 最核心的功能是可以动态扩容和回缩,如果我们觉得两个副本太少了,想要增加,只需 要修改配置文件replicaset.yaml 里的replicas 的值即可,原来replicas: 3,现在变成replicaset: 4,修改之后,执行如下命令更新:

kubectl apply -f replicaset.yaml kubectl get rs kubectl get pods

生产环境如果升级,可以删除一个pod,观察一段时间之后没问题再删除另一个pod,但是这样需要 人工干预多次;实际生产环境一般采用蓝绿发布,原来有一个rs1,再创建一个rs2(控制器),通过修 改service 标签,修改service 可以匹配到rs2 的控制器,这样才是蓝绿发布,这个也需要我们精心的 部署规划,我们有一个控制器就是建立在rs 之上完成的,叫做Deployment

13 February 2026