维护一组任何时候处于运行状态的 Pod 副本的稳定集合,可以保证给定数量的完全相同 Pod 的可用性。

01 ReplicaSet 与 Deployment 区别

Deployment 管理 ReplicaSet,并向 Pod 提供声明式的更新以及许多其他有用的功能。

02 模板示例

  1. apiVersion: apps/v1
  2. kind: ReplicaSet
  3. metadata:
  4. name: frontend
  5. labels:
  6. app: guestbook
  7. tier: frontend
  8. spec:
  9. replicas: 3
  10. selector:
  11. matchLabels:
  12. tier: frontend
  13. template:
  14. metadata:
  15. labels:
  16. tier: frontend
  17. spec:
  18. containers:
  19. - name: php-redis
  20. image: gcr.io/google_samples/gb-frontend:v3

03 查询

root@i-jr5jk6dq:~# kubectl get rs
NAME       DESIRED   CURRENT   READY   AGE
frontend   3         3         0       63s

属主,ReplicaSet 部署的 Pod 有自己的属性,执行:
kubectl get pods frontend-hjlmn -o yaml

ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: frontend
    uid: e08a088f-db54-43be-9773-9feb067fb95b
  resourceVersion: "893978"
  uid: 880bc815-0b87-4efd-84dc-ff09712706cd

延伸,对于使用其他工作负载资源部署的 Pod也有其属主信息:

name: hello-8hpvn
  namespace: default
  ownerReferences:
  - apiVersion: batch/v1
    blockOwnerDeletion: true
    controller: true
    kind: Job
    name: hello
    uid: 691aa645-c968-450c-8002-09257072a173
  resourceVersion: "887962"
  uid: f9b51edd-33f9-4ac7-ab71-e9ffc14915af
spec:
  containers:
  - command:
    - sh
    - -c
    - echo "hello" && sleep 3600
    image: busybox
    imagePullPolicy: Always
    name: hello

04 冲突

尽管你完全可以直接创建裸的 Pods,强烈建议你确保这些裸的 Pods 并不包含可能与你 的某个 ReplicaSet 的选择算符相匹配的标签。原因在于 ReplicaSet 并不仅限于拥有 在其模板中设置的 Pods,它还可以像前面小节中所描述的那样获得其他 Pods
参考https://kubernetes.io/zh/docs/concepts/workloads/controllers/replicaset/#%E9%9D%9E%E6%A8%A1%E6%9D%BF-pod-%E7%9A%84%E8%8E%B7%E5%BE%97

05 删除 ReplicaSet

  • 删除 ReplicaSet 与 Pod

kubectl delete rs/frongend

  • 只删除 ReplicaSet,保留 Pod

kubectl delete rs/frongend --cascade=orphan
一旦删除了原来的 ReplicaSet,就可以创建一个新的来替换它。 由于新旧 ReplicaSet 的 .spec.selector 是相同的,新的 ReplicaSet 将接管老的 Pod