1. 程式人生 > >Kubernetes(k8s)中文文件 名詞解釋:ReplicaSets_Kubernetes中文社群

Kubernetes(k8s)中文文件 名詞解釋:ReplicaSets_Kubernetes中文社群

什麼是ReplicaSet?

ReplicaSet是下一代複本控制器。ReplicaSet和 Replication Controller之間的唯一區別是現在的選擇器支援。Replication Controller只支援基於等式的selector(env=dev或environment!=qa),但ReplicaSet還支援新的,基於集合的selector(version in (v1.0, v2.0)或env notin (dev, qa))。在試用時官方推薦ReplicaSet。

大多數kubectl支援Replication Controller的命令也支援ReplicaSets。

rolling-update命令有一個例外 。如果您想要滾動更新功能,請考慮使用Deployments。此外, rolling-update命令是必須的,而Deployments是宣告式的,因此我們建議通過rollout命令使用Deployments。

雖然ReplicaSets可以獨立使用,但是今天它主要被 Deployments 作為協調pod建立,刪除和更新的機制。當您使用Deployments時,您不必擔心管理他們建立的ReplicaSets。Deployments擁有並管理其ReplicaSets。

何時使用ReplicaSet?

ReplicaSet可確保指定數量的pod“replicas”在任何設定的時間執行。然而,Deployments是一個更高層次的概念,它管理ReplicaSets,並提供對pod的宣告性更新以及許多其他的功能。因此,我們建議您使用Deployments而不是直接使用ReplicaSets,除非您需要自定義更新編排或根本不需要更新。

這實際上意味著您可能永遠不需要操作ReplicaSet物件:直接使用Deployments並在規範部分定義應用程式。

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到Kubernetes叢集並將其提交給Kubernetes叢集時,應建立定義的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
K8S中文社群微信公眾號