5.ReplicatSet控制器

1.Replicaset控制器概念

ReplicaSet是kubernetes中的一种副本控制器,简称rs,主要作用是控制由其管理的pod,使pod副本的数量始终维持在预设的个数。它的主要作用就是保证一定数量的Pod能够在集群中正常运行,它会持续监听这些Pod的运行状态,在Pod发生故障时重启pod,pod数量减少时重新运行新的 Pod副本。官方推荐不要直接使用ReplicaSet,用Deployments取而代之,Deployments是比ReplicaSet更高级的概念,它会管理ReplicaSet并提供很多其它有用的特性,最重要的是Deployments支持声明式更新,声明式更新的好处是不会丢失历史变更。所以Deployment控制器不直接管理Pod对象,而是由 Deployment 管理ReplicaSet,再由ReplicaSet负责管理Pod对象。

2.Replicaset工作原理

Replicaset核心作用在于代用户创建指定数量的pod副本,并确保pod副本一直处于满足用户期望的数量, 起到多退少补的作用,并且还具有自动扩容缩容等机制。
Replicaset控制器主要由三个部分组成:

  • 用户期望的pod副本数:用来定义由这个控制器管控的pod副本有几个
  • 标签选择器:选定哪些pod是自己管理的,如果通过标签选择器选到的pod副本数量少于我们指定的数量,需要用到下面的组件
  • pod资源模板:如果集群中现存的pod数量不够我们定义的副本中期望的数量怎么办,需要新建pod,这就需要pod模板,新建的pod是基于模板来创建的。

3.Replicaset资源清单文件

  • 查看参数字段
[root@master1 ~]# kubectl explain rs
KIND:     ReplicaSet
VERSION:  apps/v1
DESCRIPTION:
     ReplicaSet ensures that a specified number of pod replicas are running at
     any given time.
FIELDS:
   apiVersion	<string>  #当前资源使用的api版本,跟VERSION:  apps/v1保持一致
   kind	<string>     #资源类型,跟KIND: ReplicaSet保持一致
   metadata	<Object> #元数据,定义Replicaset名字的
   spec	<Object>     ##定义副本数、定义标签选择器、定义Pod模板
   status	<Object> #状态信息,不能改

[root@master1 ~]# kubectl explain rs.spec
KIND:     ReplicaSet
VERSION:  apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
     ReplicaSetSpec is the specification of a ReplicaSet.
FIELDS:
   minReadySeconds	<integer>
   replicas	<integer>  #定义的pod副本数,根据我们指定的值创建对应数量的pod
   selector	<Object> -required-  #用于匹配pod的标签选择器
   template	<Object>      #定义Pod的模板,基于这个模板定义的所有pod是一样的

#查看replicaset的spec.template字段如何定义?
#对于template而言,其内部定义的就是pod,pod模板是一个独立的对象
[root@master1 ~]# kubectl explain rs.spec.template
KIND:     ReplicaSet
VERSION:  apps/v1
RESOURCE: template <Object>
DESCRIPTION:
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected。PodTemplateSpec describes the data a pod should have when created from a
     template
FIELDS:
   metadata	<Object>
   spec	<Object>

[root@master1 ~]# kubectl explain rs.spec.template.spec

通过上面可以看到,ReplicaSet资源中有两个spec字段。第一个spec声明的是ReplicaSet定义多少个Pod副本(默认将仅部署1个Pod)、匹配Pod标签的选择器、创建pod的模板。第二个spec是spec.template.spec:主要用于Pod里的容器属性等配置。
.spec.template里的内容是声明Pod对象时要定义的各种属性,所以这部分也叫做PodTemplate(Pod模板)。还有一个值得注意的地方是:在.spec.selector中定义的标签选择器必须能够匹配到spec.template.metadata.labels里定义的Pod标签,否则Kubernetes将不允许创建ReplicaSet。

4.Replicaset使用案例:部署Guestbook留言板

apiVersion: apps/v1  #ReplicaSet 这个控制器属于的核心群组
kind: ReplicaSet  #创建的资源类型
metadata:
  name: frontend  #控制器的名字
  labels:
    app: guestbook
    tier: frontend
spec:
  replicas: 3   #管理的pod副本数量
  selector:
    matchLabels:
      tier: frontend  #管理带有tier=frontend标签的pod
  template:  #定义pod的模板
    metadata:
      labels:
        tier: frontend
#pod标签,一定要有,这样上面控制器就能找到它要管理的pod是哪些了
    spec:
      containers: #定义pod里运行的容器
      - name: php-redis #定义容器的名字
        image: yecc/gcr.io-google_samples-gb-frontend:v3
          ports:    #定义端口
          - name: http  #定义容器的名字
            containerPort:  80 #定义容器暴露的端口

5.扩容缩容和镜像更新

kubectl edit rs frontend

修改这项是扩容和缩容replicas: 3
修改这项是更新镜像image: yecc/gcr.io-google_samples-gb-frontend:v3

posted @ 2022-02-27 21:37  ForLivetoLearn  阅读(75)  评论(0编辑  收藏  举报