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

  1. apiVersion: extensions/v1beta1
  2. kind: ReplicaSet
  3. metadata:
  4. name: frontend
  5. # these labels can be applied automatically
  6. # from the labels in the pod template if not set
  7. # labels:
  8. # app: guestbook
  9. # tier: frontend
  10. spec:
  11. # this replicas value is default
  12. # modify it according to your case
  13. replicas: 3
  14. # selector can be applied automatically
  15. # from the labels in the pod template if not set,
  16. # but we are specifying the selector here to
  17. # demonstrate its usage.
  18. selector:
  19. matchLabels:
  20. tier: frontend
  21. matchExpressions:
  22. - {key: tier, operator: In, values: [frontend]}
  23. template:
  24. metadata:
  25. labels:
  26. app: guestbook
  27. tier: frontend
  28. spec:
  29. containers:
  30. - name: php-redis
  31. image: gcr.io/google_samples/gb-frontend:v3
  32. resources:
  33. requests:
  34. cpu: 100m
  35. memory: 100Mi
  36. env:
  37. - name: GET_HOSTS_FROM
  38. value: dns
  39. # If your cluster config does not include a dns service, then to
  40. # instead access environment variables to find service host
  41. # info, comment out the 'value: dns' line above, and uncomment the
  42. # line below.
  43. # value: env
  44. ports:
  45. - containerPort: 80

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

  1. $ kubectl create -f frontend.yaml
  2. replicaset "frontend" created
  3. $ kubectl describe rs/frontend
  4. Name: frontend
  5. Namespace: default
  6. Image(s): gcr.io/google_samples/gb-frontend:v3
  7. Selector: tier=frontend,tier in (frontend)
  8. Labels: app=guestbook,tier=frontend
  9. Replicas: 3 current / 3 desired
  10. Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
  11. No volumes.
  12. Events:
  13. FirstSeen LastSeen Count From SubobjectPath Type Reason Message
  14. --------- -------- ----- ---- ------------- -------- ------ -------
  15. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-qhloh
  16. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-dnjpy
  17. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-9si5l
  18. $ kubectl get pods
  19. NAME READY STATUS RESTARTS AGE
  20. frontend-9si5l 1/1 Running 0 1m
  21. frontend-dnjpy 1/1 Running 0 1m
  22. 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 k8s Replica Sets - 图1
  1. apiVersion: autoscaling/v1
  2. kind: HorizontalPodAutoscaler
  3. metadata:
  4. name: frontend-scaler
  5. spec:
  6. scaleTargetRef:
  7. kind: ReplicaSet
  8. name: frontend
  9. minReplicas: 3
  10. maxReplicas: 10
  11. targetCPUUtilizationPercentage: 50
  1. kubectl create -f hpa-rs.yaml

K8S中文社区微信公众号

原文: http://docs.kubernetes.org.cn/314.html