1,RS介绍

ReplicaSet是下一代复本控制器。RS和RC之间的唯一区别是现在的选择器支持。RC只支持基于等式的selector(env=dev或environment!=qa),但RS还支持新的,基于集合的selector(version in (v1.0, v2.0)或env notin (dev, qa))。
在新版本的 Kubernetes 中建议使用RS来取代RC,虽然也RS可以独立使用,但建议使用 Deployment 来自动管理 RS,这样就无需担心跟其他机制的不兼容问题(比如 RS 不支持 rolling-update 但 Deployment 支持),并且Deployment还支持版本记录、回滚、暂停升级等高级特性

2,示例

  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
  1. # 通过create创建好后可以查看pod信息
  2. $ kubectl describe rs/frontend
  3. Name: frontend
  4. Namespace: default
  5. Image(s): gcr.io/google_samples/gb-frontend:v3
  6. Selector: tier=frontend,tier in (frontend)
  7. Labels: app=guestbook,tier=frontend
  8. Replicas: 3 current / 3 desired
  9. Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
  10. No volumes.
  11. Events:
  12. FirstSeen LastSeen Count From SubobjectPath Type Reason Message
  13. --------- -------- ----- ---- ------------- -------- ------ -------
  14. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-qhloh
  15. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-dnjpy
  16. 1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-9si5l
  17. $ kubectl get pods
  18. NAME READY STATUS RESTARTS AGE
  19. frontend-9si5l 1/1 Running 0 1m
  20. frontend-dnjpy 1/1 Running 0 1m
  21. frontend-qhloh 1/1 Running 0 1m