# k8s Replica Sets

ReplicaSet(RS)是 Replication Controller(RC)的升级版本。ReplicaSet 和 Replication Controller 之间的唯一区别是对选择器的支持。ReplicaSet 支持 labels user guide 中描述的 set-based 选择器要求,而 Replication Controller 仅支持 equality-based 的选择器要求。

# 如何使用 ReplicaSet

大多数 kubectl 支持 Replication Controller 命令的也支持 ReplicaSets。rolling-update 命令除外,如果要使用 rolling-update,请使用 Deployments 来实现。

虽然 ReplicaSets 可以独立使用,但它主要被 Deployments 用作 pod 机制的创建、删除和更新。当使用 Deployment 时,你不必担心创建 pod 的 ReplicaSets,因为可以通过 Deployment 实现管理 ReplicaSets。

# 何时使用 ReplicaSet

ReplicaSet 能确保运行指定数量的 pod。然而,Deployment 是一个更高层次的概念,它能管理 ReplicaSets,并提供对 pod 的更新等功能。因此,我们建议你使用 Deployment 来管理 ReplicaSets,除非你需要自定义更新编排。

这意味着你可能永远不需要操作 ReplicaSet 对象,而是使用 Deployment 替代管理。

# 示例

frontend.yaml

apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  name: frontend
  # these labels can be applied automatically
  # from the labels in the pod template if not set
  # labels:
    # app: guestbook
    # tier: frontend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  # selector can be applied automatically
  # from the labels in the pod template if not set,
  # but we are specifying the selector here to
  # demonstrate its usage.
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80

将此配置保存到(frontend.yaml)并提交到 k8s 集群时,将创建定义的 ReplicaSet 及其管理的 pod。

$ kubectl create -f frontend.yaml
replicaset "frontend" created
$ kubectl describe rs/frontend
Name:          frontend
Namespace:     default
Image(s):      gcr.io/google_samples/gb-frontend:v3
Selector:      tier=frontend,tier in (frontend)
Labels:        app=guestbook,tier=frontend
Replicas:      3 current / 3 desired
Pods Status:   3 Running / 0 Waiting / 0 Succeeded / 0 Failed
No volumes.
Events:
  FirstSeen    LastSeen    Count    From                SubobjectPath    Type        Reason            Message
  ---------    --------    -----    ----                -------------    --------    ------            -------
  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-qhloh
  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-dnjpy
  1m           1m          1        {replicaset-controller }             Normal      SuccessfulCreate  Created pod: frontend-9si5l
$ kubectl get pods
NAME             READY     STATUS    RESTARTS   AGE
frontend-9si5l   1/1       Running   0          1m
frontend-dnjpy   1/1       Running   0          1m
frontend-qhloh   1/1       Running   0          1m

# ReplicaSet as an Horizontal Pod Autoscaler target

ReplicaSet 也可以作为 Horizontal Pod Autoscalers (HPA)的目标。也就是说,一个 ReplicaSet 可以由一个 HPA 来自动伸缩。以下是针对我们在上一个示例中创建的 ReplicaSet 的 HPA 示例。

hpa-rs.yaml
apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: frontend-scaler spec: scaleTargetRef: kind: ReplicaSet name: frontend minReplicas: 3 maxReplicas: 10 targetCPUUtilizationPercentage: 50
kubectl create -f hpa-rs.yaml
Last Updated: 6/17/2023, 6:57:19 PM