ReplicaSet 是新一代 ReplicationController (将被弃用).

4.3.1 比较 ReplicaSet 和 ReplicationController

  • 行为完全相同
  • ReplicaSet 的 pod 选择器表达能力更强

4.3.2 定义 ReplicaSet

image.png

模拟:

  1. apiVersion: apps/v1
  2. kind: ReplicaSet
  3. metadata:
  4. name: hello-rs
  5. spec:
  6. replicas: 3
  7. selector:
  8. matchLabels:
  9. app: rs
  10. template:
  11. metadata:
  12. labels:
  13. app: rs
  14. spec:
  15. containers:
  16. - image: jdxj/study_kubernetes:v0.3.0
  17. name: test-study-k8s
  18. ports:
  19. - containerPort: 8080
  20. protocol: TCP

API 版本的属性:

  • API 组 (apps)
  • 实际的 API 版本 (v1beta2)

4.3.3 创建和检查 ReplicaSet

查看:

  1. $ kubectl get rs
  2. NAME DESIRED CURRENT READY AGE
  3. hello-rs 3 3 3 108s
  1. $ kubectl describe rs
  2. Name: hello-rs
  3. Namespace: default
  4. Selector: app=rs
  5. Labels: <none>
  6. Annotations: <none>
  7. Replicas: 3 current / 3 desired
  8. Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
  9. Pod Template:
  10. Labels: app=rs
  11. Containers:
  12. test-study-k8s:
  13. Image: jdxj/study_kubernetes:v0.3.0
  14. Port: 8080/TCP
  15. Host Port: 0/TCP
  16. Environment: <none>
  17. Mounts: <none>
  18. Volumes: <none>
  19. Events:
  20. Type Reason Age From Message
  21. ---- ------ ---- ---- -------
  22. Normal SuccessfulCreate 2m18s replicaset-controller Created pod: hello-rs-kzss2
  23. Normal SuccessfulCreate 2m18s replicaset-controller Created pod: hello-rs-5p6g6
  24. Normal SuccessfulCreate 2m18s replicaset-controller Created pod: hello-rs-s8tlp

4.3.4 使用 ReplicaSet 的更富表达力的标签选择器

matchExpressions 选择器:

image.png

组成:

  • key
  • operator
  • values

运算符:

image.png

多个表达式之间是 “与” 的关系.

4.3.5 ReplicaSet 小结

删除 ReplicaSet:

  • 会删除 pod
  1. $ kubectl delete rs kubia