当前位置: 首页 > news >正文

Kubernetes的Replica Set和ReplicaController有什么区别

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

相关文章:

  • 计划管理工具应该具备的能(甘特图)
  • Qt高分屏自适应
  • 框架的CVE漏洞利用 php类 java类 手工操作和自动化操作蓝队分析漏洞利用的流量特征
  • Python网络编程入门
  • 【测试篇】关于自动化测试前言,自动化测试的基本概念
  • 每日学习总结
  • 前端常见面试题(不断更新版)
  • Linux部署若依前后端分离版
  • 【yolo】yolo推理报错,以及解决方案
  • 数字化转型驱动卫生用品安全革新
  • 从零开始实现 C++ TinyWebServer 数据库连接池 SqlConnectPool详解
  • 第5节:AWK环境准备
  • 新增菜品-03.代码开发2
  • 在Windows和Linux系统上的Docker环境中使用的镜像是否相同
  • C++函数与STL
  • 区块链交易
  • [AI速读]用脚本加速高速链路验证:一个高效覆盖率收敛方案
  • 【Tips】Vim文档的使用
  • mysql 对json的处理?
  • AI安全学习(刚开始,未完版)
  • 临港新片区:发布再保险、国际航运、生物医药3个领域数据出境操作指引
  • 美英达成贸易协议,美股集体收涨
  • 罗氏制药全新生物制药生产基地投资项目在沪启动:预计投资20.4亿元,2031年投产
  • 探索人类的心灵这件事,永远也不会过时
  • 以色列计划“占领加沙”,特朗普下周中东行结束之际将是“机会窗口”
  • 创历史同期新高!“五一”假期全国快递揽投超48亿件