ReplicaSet
和 ReplicationController
是 Kubernetes 中用于管理应用程序副本的两种资源,它们有类似的功能,但 ReplicaSet
是 ReplicationController
的增强版本。
以下是它们的主要区别:
1. 功能的演进
-
ReplicationController 是 Kubernetes 的早期版本中用来确保一定数量的 Pod 副本运行的控制器。
-
ReplicaSet 是
ReplicationController
的增强版本,引入了更多的功能和灵活性,逐步取代了ReplicationController
。
2. 标签选择器
-
ReplicationController 只支持基于 等式匹配 的标签选择器。例如:
selector:app: myapp
-
ReplicaSet 支持更复杂的 集合匹配,可以使用
in
、notin
等操作符。例如:selector:matchLabels:app: myappmatchExpressions:- key: environmentoperator: Invalues:- production- staging
3. 推荐使用
-
Kubernetes 更推荐使用 ReplicaSet,因为它功能更强大,尤其是支持高级标签选择器。
-
不过,
ReplicationController
仍然受支持,用于与旧版本的 Kubernetes 配置兼容。
4. 与 Deployment 的关系
-
ReplicaSet 通常与
Deployment
一起使用,Deployment
会管理ReplicaSet
,从而实现滚动更新等功能。 -
ReplicationController 已经逐渐被
Deployment
和ReplicaSet
取代,功能上相对较弱。
示例对比
ReplicationController 配置示例:
apiVersion: v1
kind: ReplicationController
metadata:name: myapp-rc
spec:replicas: 3selector:app: myapptemplate:metadata:labels:app: myappspec:containers:- name: myapp-containerimage: nginx
ReplicaSet 配置示例:
apiVersion: apps/v1
kind: ReplicaSet
metadata:name: myapp-rs
spec:replicas: 3selector:matchLabels:app: myappmatchExpressions:- key: environmentoperator: Invalues:- productiontemplate:metadata:labels:app: myappspec:containers:- name: myapp-containerimage: nginx
Scale
总结
-
如果是新项目,直接使用
ReplicaSet
或Deployment
,避免使用ReplicationController
。 -
如果你的需求更复杂(比如基于集合匹配的调度策略),
ReplicaSet
是更好的选择。
练习
controlplane ~ ➜ kubectl get pods
No resources found in default namespace.controlplane ~ ➜ kubectl get replicaSet
No resources found in default namespace.controlplane ~ ➜ kubectl get replicaSet
NAME DESIRED CURRENT READY AGE
new-replica-set 4 4 0 6scontrolplane ~ ➜ kubectl describe replicaSet new-replica-set
Name: new-replica-set
Namespace: default
Selector: name=busybox-pod
Labels: <none>
Annotations: <none>
Replicas: 4 current / 4 desired
Pods Status: 0 Running / 4 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels: name=busybox-podContainers:busybox-container:Image: busybox777Port: <none>Host Port: <none>Command:sh-cecho Hello Kubernetes! && sleep 3600Environment: <none>Mounts: <none>Volumes: <none>Node-Selectors: <none>Tolerations: <none>
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulCreate 44s replicaset-controller Created pod: new-replica-set-g2tmxNormal SuccessfulCreate 44s replicaset-controller Created pod: new-replica-set-hjk29Normal SuccessfulCreate 44s replicaset-controller Created pod: new-replica-set-dxcscNormal SuccessfulCreate 44s replicaset-controller Created pod: new-replica-set-7vm2xcontrolplane ~ ➜ kubectl delete pod new-replica-set-g2tmx
pod "new-replica-set-g2tmx" deletedcontrolplane ~ ➜ kubectl describe replicaSet new-replica-set
Name: new-replica-set
Namespace: default
Selector: name=busybox-pod
Labels: <none>
Annotations: <none>
Replicas: 4 current / 4 desired
Pods Status: 0 Running / 4 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels: name=busybox-podContainers:busybox-container:Image: busybox777Port: <none>Host Port: <none>Command:sh-cecho Hello Kubernetes! && sleep 3600Environment: <none>Mounts: <none>Volumes: <none>Node-Selectors: <none>Tolerations: <none>
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal SuccessfulCreate 2m45s replicaset-controller Created pod: new-replica-set-g2tmxNormal SuccessfulCreate 2m45s replicaset-controller Created pod: new-replica-set-hjk29Normal SuccessfulCreate 2m45s replicaset-controller Created pod: new-replica-set-dxcscNormal SuccessfulCreate 2m45s replicaset-controller Created pod: new-replica-set-7vm2xNormal SuccessfulCreate 12s replicaset-controller Created pod: new-replica-set-p5pm4controlplane ~ ➜ kubectl get pod
NAME READY STATUS RESTARTS AGE
new-replica-set-7vm2x 0/1 ErrImagePull 0 3m5s
new-replica-set-dxcsc 0/1 ImagePullBackOff 0 3m5s
new-replica-set-hjk29 0/1 ErrImagePull 0 3m5s
new-replica-set-p5pm4 0/1 ErrImagePull 0 32scontrolplane ~ ➜ kubectl create -f /root/replicaset-definition-1.yaml
error: resource mapping not found for name: "replicaset-1" namespace: "" from "/root/replicaset-definition-1.yaml": no matches for kind "ReplicaSet" in version "v1"
ensure CRDs are installed firstcontrolplane ~ ✖ vim /root/replicaset-definition-1.yamlcontrolplane ~ ➜ vim /root/replicaset-definition-1.yamlcontrolplane ~ ➜ cat /root/replicaset-definition-1.yaml
apiVersion: v1
kind: ReplicaSet
metadata:name: replicaset-1
spec:replicas: 2selector:matchLabels:tier: frontendtemplate:metadata:labels:tier: frontendcontainers:- name: nginximage: nginx