Kubernetes的Replica Set和ReplicaController有什么区别

发布于:2025-03-23 ⋅ 阅读:(42) ⋅ 点赞:(0)

ReplicaSetReplicationController 是 Kubernetes 中用于管理应用程序副本的两种资源,它们有类似的功能,但 ReplicaSetReplicationController 的增强版本。

以下是它们的主要区别:

1. 功能的演进

  • ReplicationController 是 Kubernetes 的早期版本中用来确保一定数量的 Pod 副本运行的控制器。

  • ReplicaSetReplicationController 的增强版本,引入了更多的功能和灵活性,逐步取代了 ReplicationController

2. 标签选择器

  • ReplicationController 只支持基于 等式匹配 的标签选择器。例如:

    selector:
      app: myapp
    
  • ReplicaSet 支持更复杂的 集合匹配,可以使用 innotin 等操作符。例如:

    selector:
      matchLabels:
        app: myapp
      matchExpressions:
      - key: environment
        operator: In
        values:
        - production
        - staging
    

3. 推荐使用

  • Kubernetes 更推荐使用 ReplicaSet,因为它功能更强大,尤其是支持高级标签选择器。

  • 不过,ReplicationController 仍然受支持,用于与旧版本的 Kubernetes 配置兼容。

4. 与 Deployment 的关系

  • ReplicaSet 通常与 Deployment 一起使用,Deployment 会管理 ReplicaSet,从而实现滚动更新等功能。

  • ReplicationController 已经逐渐被 DeploymentReplicaSet 取代,功能上相对较弱。

示例对比

ReplicationController 配置示例:

 

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
spec:
  replicas: 3
  selector:
    app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: nginx
ReplicaSet 配置示例:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
    matchExpressions:
    - key: environment
      operator: In
      values:
      - production
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: nginx

 

 

Scale

总结

  • 如果是新项目,直接使用 ReplicaSetDeployment,避免使用 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       6s

controlplane ~ ➜  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-pod
  Containers:
   busybox-container:
    Image:      busybox777
    Port:       <none>
    Host Port:  <none>
    Command:
      sh
      -c
      echo Hello Kubernetes! && sleep 3600
    Environment:   <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-g2tmx
  Normal  SuccessfulCreate  44s   replicaset-controller  Created pod: new-replica-set-hjk29
  Normal  SuccessfulCreate  44s   replicaset-controller  Created pod: new-replica-set-dxcsc
  Normal  SuccessfulCreate  44s   replicaset-controller  Created pod: new-replica-set-7vm2x

controlplane ~ ➜  kubectl delete pod new-replica-set-g2tmx
pod "new-replica-set-g2tmx" deleted

controlplane ~ ➜  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-pod
  Containers:
   busybox-container:
    Image:      busybox777
    Port:       <none>
    Host Port:  <none>
    Command:
      sh
      -c
      echo Hello Kubernetes! && sleep 3600
    Environment:   <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-g2tmx
  Normal  SuccessfulCreate  2m45s  replicaset-controller  Created pod: new-replica-set-hjk29
  Normal  SuccessfulCreate  2m45s  replicaset-controller  Created pod: new-replica-set-dxcsc
  Normal  SuccessfulCreate  2m45s  replicaset-controller  Created pod: new-replica-set-7vm2x
  Normal  SuccessfulCreate  12s    replicaset-controller  Created pod: new-replica-set-p5pm4

controlplane ~ ➜  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          32s

controlplane ~ ➜  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 first

controlplane ~ ✖ vim /root/replicaset-definition-1.yaml

controlplane ~ ➜  vim /root/replicaset-definition-1.yaml

controlplane ~ ➜  cat /root/replicaset-definition-1.yaml
apiVersion: v1
kind: ReplicaSet
metadata:
  name: replicaset-1
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
   containers:
    - name: nginx
      image: nginx