Kubernetes RC与RS控制器 Replication Controller ReplicaSet
  AWkq7aIjuRwO 2023年11月02日 43 0

RC与RS控制器

通过资源配置清单方式直接使用pod管理器的方式来创建pod

创建创建各种各样的pod

Replication Controller (复制控制器, RC,已经废弃,被RS取代) 和 ReplicaSet (复制集, RS) 是两种简单部署 Pod 的方式。因为在生产环境中,主要使用更高级的 Deployment 等方式进行 Pod 的管理和部署,所 以只需要简单了解即可。

生产上

直接使用pod这种控制器来去创建pod,pod本身我们创建的过程中一次性只能创建一个pod,而且这种方式和docker很像,不管是使用docker run 还是kubeclt run 还是配置清单都是类似的,一次性只能 去创建一个pod出来,只是说比docker 和compose多个自动调度,能把pod根据调度规则给他调度到不同的node节点上来,这是唯一一个比容器docker多了一个功能,但是还有个问题如果 这个pod宕机了,出问题了我们就没有可用的pod了,所以在生产上我们都在pod之外还加了一层pod控制器,这个pod控制器,他的功能:

1能够根据指定的个数,相同的pod生成多个副本,

2可以根据我们管理员定制pod的时候期望的pod的状态,期望比如说的副本数,还有pod所有配置都可以用探针webhook都可以提直接使用,可以通过相同的方式把一个pod做多份,而且还可以根据每个pod,一旦在集群中运行的数量,不是我们期望值的时候,它可以自动的给我们变成我们的期望值,比如我期望运行时三个pod,但是有一个挂了,他会根据我们最初pod指定的个数在创建一个出来,他必须满足我们的期望值,这个期望值不光单单指数量,比如镜像版本,挂载的各种各样的资源,里面运行的程序,这都是我们一个期望值,这个期望值就是靠我们使用配置清单的方式,我们写什么样,他必须时什么样,除非集群出问题,资源不够了,比如我在集群运行一个pod,运行三份,结果整个集群就没有那么多资源,比如需要三个G,结果只有2个G,那没办法只能运行两个,只要集群状态时正常的,他就能够实现根据我们期望状态来去运行相应的pod,这个就是pod控制器的一个作用.

目前主流的pod控制器:

第一种RC和RS

Replication Contaroller(复制控制器简称RC已经被淘汰了) 和ReplicaSet(取代RC一种pod控制器)

第二种无状态应用管理 Deployment

第三种有状态应用管理 StartefulSet(生成使用比较少,本身编排有状态服务差点意思)

第四种守护进程集 DaemonSet

第五种Job cronjob 一次性任务 和计划任务

第六种 HPA 水平pod自动生成器 比如我们定义了3个pod,但是资源使用率达到资源控制器的要求的时候,比如检查podcpu使用率超过80了,然后他会扩容,扩容的数量需要指定,比如最多扩容100个 10个 3个

第七种 VPA 纵向扩容 比如内存目前是2G cpu是1核 纵向扩容就是把内存变大 变成6个G 8个G cpu 给他加内核 这个用的比较少

先看RC(不详细说了因为被淘汰了)

Replication Controller(了解即可,生产很少使用)

主要功能:确保副本数达到期望值,把一个pod启动几个 2个? 3个? 200个? rc可以实现,但是他有个问题,他只有一种标签选择器,这个不好,目前已经被淘汰了,不支持动态扩容,不支持滚动更新(意味着需要从新配置删除原有pod然后从新创建)

创建一个

[root@k8s-master1 ~]# kubectl explain replicationController
KIND:     ReplicationController
VERSION:  v1

DESCRIPTION:
     ReplicationController represents the configuration of a replication
     controller.

FIELDS:
   apiVersion   <string> #和pod没啥区别
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string> #和pod没啥区别
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object> #和pod没啥区别
     If the Labels of a ReplicationController are empty, they are defaulted to
     be the same as the Pod(s) that the replication controller manages. Standard
     object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object> #主要看下spec
     Spec defines the specification of the desired behavior of the replication
     controller. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Status is the most recently observed status of the replication controller.
     This data may be out of date by some window of time. Populated by the
     system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

[root@k8s-master1 ~]# kubectl explain replicationController.spec
KIND:     ReplicationController
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the specification of the desired behavior of the replication
     controller. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     ReplicationControllerSpec is the specification of a replication controller.

FIELDS:
   minReadySeconds      <integer>
   #
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   replicas     <integer>
   #副本数,指定运行的rc启动的pod数量
     Replicas is the number of desired replicas. This is a pointer to
     distinguish between explicit zero and unspecified. Defaults to 1. More
     info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#what-is-a-replicationcontroller

   selector     <map[string]string>
   #标签选择器 只有一种详细看下
     Selector is a label query over pods that should match the Replicas count.
     If Selector is empty, it is defaulted to the labels present on the Pod
     template. Label keys and values that must match in order to be controlled
     by this replication controller, if empty defaulted to labels on Pod
     template. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

   template     <Object>
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. This takes precedence over a
     TemplateRef. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template
[root@k8s-master1 ~]# kubectl explain replicationController.spec.selector
KIND:     ReplicationController
VERSION:  v1

FIELD:    selector <map[string]string>

DESCRIPTION:
     Selector is a label query over pods that should match the Replicas count.
     If Selector is empty, it is defaulted to the labels present on the Pod
     template. Label keys and values that must match in order to be controlled
     by this replication controller, if empty defaulted to labels on Pod
     template. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

同时我们看下deployment和replicationController的区别,多一个


[root@k8s-master1 ~]# kubectl explain deployment.spec.selector]
error: field "selector]" does not exist
[root@k8s-master1 ~]# kubectl explain deployment.spec.selector
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: selector <Object>

DESCRIPTION:
     Label selector for pods. Existing ReplicaSets whose pods are selected by
     this will be the ones affected by this deployment. It must match the pod
     template's labels.

     A label selector is a label query over a set of resources. The result of
     matchLabels and matchExpressions are ANDed. An empty label selector matches
     all objects. A null label selector matches no objects.

FIELDS:
   matchExpressions     <[]Object>
   #第一种
     matchExpressions is a list of label selector requirements. The requirements
     are ANDed.

   matchLabels  <map[string]string>
   #第二种,但是RC(replicationContrller)只有一种
     matchLabels is a map of {key,value} pairs. A single {key,value} in the
     matchLabels map is equivalent to an element of matchExpressions, whose key
     field is "key", the operator is "In", and the values array contains only
     "value". The requirements are ANDed.

除了以上这些最重要的点是: template

[root@k8s-master1 ~]# kubectl explain replicationController.spec
KIND:     ReplicationController
VERSION:  v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the specification of the desired behavior of the replication
     controller. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     ReplicationControllerSpec is the specification of a replication controller.

FIELDS:
   minReadySeconds      <integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   replicas     <integer>
   #定义pod数量
     Replicas is the number of desired replicas. This is a pointer to
     distinguish between explicit zero and unspecified. Defaults to 1. More
     info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#what-is-a-replicationcontroller

   selector     <map[string]string>
   #怎么来匹配对应的pod
     Selector is a label query over pods that should match the Replicas count.
     If Selector is empty, it is defaulted to the labels present on the Pod
     template. Label keys and values that must match in order to be controlled
     by this replication controller, if empty defaulted to labels on Pod
     template. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

   template     <Object>
   #模板,写pod的参数几乎在这里面都可以使用,创建rc控制器最主要的是启动pod
   #如何来启动pod  pod.spec里面对应的参数这里面都可以使用
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. This takes precedence over a
     TemplateRef. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

查看一下


[root@k8s-master1 ~]# kubectl explain ReplicationController.spec.template
KIND:     ReplicationController
VERSION:  v1

RESOURCE: template <Object>

DESCRIPTION:
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. This takes precedence over a
     TemplateRef. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

     PodTemplateSpec describes the data a pod should have when created from a
     template

FIELDS:
   metadata     <Object>
   #这个里面和pod.metadata里面参数一样
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
   #这个里面和pod.spec里面参数一样
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

写一个

[root@k8s-master1 rc]# vim nginx-rc.yml
apiVersion: app/v1
kind: ReplicationController
metadata:
  name: nginx-1 #这里是rc的名称.pod的名字是rc的名字加hash值
spec:
  replicas: 3 #启动pod数量
  selector: #标签选择器 可以写多个
    app: nginx
    env: dev
  template: #最主要的字段模板
    metadata:
      name: nginx
      labels: #这个标签要和上面的标签选择器对应
        app: nginx
        env: dev
    spec:
      containers:
      - name: nginx
        image: harbor.guoguo.com/apps/nginx:1.22.1
        ports:
        - containerPort: 80

[root@k8s-master1 rc]# kubectl apply -f nginx-rc.yml
replicationcontroller/nginx-1 created
[root@k8s-master1 rc]# kubectl get pods
NAME            READY   STATUS    RESTARTS      AGE
nginx-1-6hhtb   1/1     Running   0             11s
nginx-1-7ntjq   1/1     Running   0             11s
nginx-1-9vbrb   1/1     Running   0             11s

也可以通过rc来查看

[root@k8s-master1 rc]# kubectl get rc
NAME      DESIRED   CURRENT   READY   AGE
nginx-1   3         3         3       56s
#rc名称  理想状态3  目前3      准备3   启动时间  

到这里RC就介绍完了,RC不需要过多说,因为已经被淘汰了

下面是RS

RS:复制集(RplicaSet)

rs将rc做了个升级,在标签选择器位置多了一种方式

ReplicaSet 是支持基于集合的标签选择器的下一代 Replication Controller ,它主要用作Deployment 协调创建、删除和更新 Pod,和 Replication Controller 唯一的区别是,ReplicaSet 支持标签选择器比ReplicationController多了matchExpress。在实际应用中,虽然 ReplicaSet 可以单独使用,但是一般建议使用 Deployment 来 自动管理 ReplicaSet,除非自定义的 Pod 不需要更新或有其他编排等。

查看一下比RC标签选择器多的方式


[root@k8s-master1 ~]# kubectl explain ReplicaSet.spec.selector
KIND:     ReplicaSet
VERSION:  apps/v1

RESOURCE: selector <Object>

DESCRIPTION:
     Selector is a label query over pods that should match the replica count.
     Label keys and values that must match in order to be controlled by this
     replica set. It must match the pod template's labels. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

     A label selector is a label query over a set of resources. The result of
     matchLabels and matchExpressions are ANDed. An empty label selector matches
     all objects. A null label selector matches no objects.

FIELDS:
   matchExpressions     <[]Object>
   #可以支持通配符和正则,可以写包含 不包含 唯一的区别就在这里
   #定义标签选择器的方式多了一种,之前写的标签选择器, rc少这个字段,rs就分开了,这个和rc写法一样
   #直接在selector下面写两个标签
     matchExpressions is a list of label selector requirements. The requirements
     are ANDed.

   matchLabels  <map[string]string>
   #完全匹配
     matchLabels is a map of {key,value} pairs. A single {key,value} in the
     matchLabels map is equivalent to an element of matchExpressions, whose key
     field is "key", the operator is "In", and the values array contains only

详细看下matchExpressions

[root@k8s-master1 ~]# kubectl explain ReplicaSet.spec.selector.matchExpressions
KIND:     ReplicaSet
VERSION:  apps/v1

RESOURCE: matchExpressions <[]Object>

DESCRIPTION:
     matchExpressions is a list of label selector requirements. The requirements
     are ANDed.

     A label selector requirement is a selector that contains values, a key, and
     an operator that relates the key and values.

FIELDS:
   key  <string> -required-
   #必选字段
     key is the label key that the selector applies to.

   operator     <string> -required-
   #必选 详细看下 
     operator represents a key's relationship to a set of values. Valid
     operators are In, NotIn, Exists and DoesNotExist.

   values       <[]string>
     values is an array of string values. If the operator is In or NotIn, the
     values array must be non-empty. If the operator is Exists or DoesNotExist,
     the values array must be empty. This array is replaced during a strategic
     merge patch.

详细看下operator

[root@k8s-master1 ~]# kubectl explain ReplicaSet.spec.selector.matchExpressions.operator
KIND:     ReplicaSet
VERSION:  apps/v1

FIELD:    operator <string>

DESCRIPTION:
     operator represents a key's relationship to a set of values. Valid
     operators are In, NotIn, Exists and DoesNotExist.
#opertor就是选项,in notin 包含 不包含  存在  不存在  判断的意思

写一个

生产上直接使用rs的也少都是上层使用deployment,下层使用rs

[root@k8s-master1 rc]# vim nginx-rs.yml
apiVersion: apps/v1 #注意这里版本 可以通过kubectl explain ReplicaSet.apiVersion 看到版本
kind: ReplicaSet
metadata:
  name: nginx-rs-1
  labels: #这里本身也可以写标签
    app: nginx
    env: test
spec:
  replicas: 5
  selector: #和rc不同的是 这里多了一个层级
    matchLabels:
      env: test
    matchExpressions:  #这是另一种标签选择器 它支持正则
      - {key: app, operator: In, values: [nginx,tomcat]} 
      #列表名为app的 in是包含的意思  列表为app的里面可以包含的值为nginx tomcat
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
        env: test
    spec:
      containers:
      - name: nginx
        image: harbor.guoguo.com/apps/ubuntu-nginx:1.22.1
        ports:
        - containerPort: 80
#详细解释下这块
spec:
  replicas: 5
  selector:  #下面种标签选择器都用到了,其实没必要,只是为了做演示
    matchLabels: #如果只用这种就把标签全写上
      env: test
    matchExpressions: #如果只用这种 就可以写某一个标签 匹配到就可以
      - {key: app, operator: In, values: [nginx,tomcat]}

创建

[root@k8s-master1 rc]# kubectl apply -f nginx-rs.yml
replicaset.apps/nginx-rs-1 created
[root@k8s-master1 rc]# kubectl get rs
NAME         DESIRED   CURRENT   READY   AGE
nginx-rs-1   5         5         5       9s
[root@k8s-master1 rc]# kubectl get pods
NAME               READY   STATUS    RESTARTS   AGE
nginx-rs-1-7lbkm   1/1     Running   0          32s
nginx-rs-1-8m4kp   1/1     Running   0          32s
nginx-rs-1-crzr7   1/1     Running   0          32s
nginx-rs-1-n56sk   1/1     Running   0          32s
nginx-rs-1-sjjhf   1/1     Running   0          32s

以上就是RS(ReplicaSet)

RS和RC的区别就是标签选择器,RS的标签选择器更强大一点支持通配符,正则,匹配不匹配 存在不存在

以上两个生产上用的比较少

无状态的服务直接用Deployment

下节介绍Deployment

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: mac docker怎么设置中文 下一篇: 初识container
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论