首页 > 其他分享 >控制器概念

控制器概念

时间:2022-08-16 00:15:50浏览次数:73  
标签:控制器 ReplicaSet replicasetchenxi Deployment 概念 Running deployment pod

 

1.Replicaset概念

Kubernetes中的ReplicaSet主要的作用是维持一组Pod副本的运行,它的主要作用就是保证一定数量的 Pod 能够在集群中正常运行,它会持续监听这些 Pod 的运行状态,在 Pod 发生故障时重启pod,pod数量减少时重新运行新的 Pod 副本,因此,它通常被用来保证特定数量相同的Pods的可用性。

2.replicaset的工作

ReplicaSet由字段定义,包括一个选择器,该选择器指定如何找到它所管理的Pod、维护多少个pod,以及pod的模板。ReplicaSet通过创建和删除Pod来满足期望的pod数量。当ReplicaSet需要创建新的Pod时,它将使用其Pod模板。ReplicaSet通过Pods的metadata.ownerReferences字段链接到其Pod,该字段指定当前对象所拥有的资源。由ReplicaSet获取的所有Pod在其ownerReferences字段中都有其自己的ReplicaSet的标识信息。通过此链接,ReplicaSet可以知道它正在维护的Pod的状态,并据此计划。

ReplicaSet通过使用其选择器标识要获取的新Pod。如果存在没有OwnerReference的Pod或OwnerReference不是控制器,并且它与ReplicaSet的选择器匹配,它将由所述的ReplicaSet立即获取

3.使用replicaset

replicaSet确保在任何给定时间都运行指定数量的Pod副本。但是,Deployment是一个高级概念,用于管理副本集,并提供对Pod的声明性更新以及许多其他有用的功能。因此,除非你需要自定义更新编排或根本不需要更新,否则我们使用Deployment而不是直接使用replicaset。这实际上意味着你可能永远不需要操纵ReplicaSet对象:改用Deployment,然后在spec部分中定义你的应用程序。

帮助

[root@master-1 ~]# kubectl explain replicaset.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>   准备等待启动pod 最小时间秒整数
     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>  副本个数
     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	<Object> -required-  标签选择器
     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

   template	<Object>  pod 的定义
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected. More info:
     https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

  创建名称空间

kubectl create ns replicaset

  编写replicaset yaml 并运行

apiVersion: apps/v1 #api 版本
kind: ReplicaSet   #资源类型
metadata:  #元数据
  annotations:   #注解
    dev: replicaset
  name: replicasetchenxi #控制器名字 不允许大小写同时出现
  namespace: replicaset  # 所在的名称空间
  labels:  # 标签
    dev: ReplicaSet-test
spec: #期望状态
  minReadySeconds: 8 # 新创建的 pod 等待就绪的最小秒数
  replicas: 5   # 副本数量
  selector:   # 标签选择器设置
    matchLabels:   # 使用的标签选择器,标签选择器有matchExpressions、matchLabels
      dev: chenxi   # 设置匹配的标签
  template: # 控制器控制pod 的所有属性
    metadata:  # pod 的元数据
      labels:  # 设置pod 标签
        dev: chenxi #具体值必须与控制器设置的选择的标签值一至否则控制器不停重启pod
    spec:  #pod 的期望状态
      containers:  # 容器属性
      - name: web # 容器名字
        image: nginx  # 容器镜像
        imagePullPolicy: IfNotPresent #拉取镜像的策略
        ports: # 容器端口设置
        - name: web # 端口名字
          containerPort: 80 # 容器端口
        livenessProbe:  # 存活性探测
          httpGet:  # 探针的设置
            port: web # 探测端口设置
            path: index.html #探测url

  运行查看

[root@master-1 kongzhiqi]# kubectl apply -f  replicaset.yaml 
replicaset.apps/replicasetchenxi created
[root@master-1 kongzhiqi]# kubectl get pod -n replicaset pod名字继承控制器名字
NAME                     READY   STATUS    RESTARTS   AGE
replicasetchenxi-4bs2r   1/1     Running   0          93s
replicasetchenxi-8qkfl   1/1     Running   0          93s
replicasetchenxi-dslcp   1/1     Running   0          93s
replicasetchenxi-f6dmw   1/1     Running   0          93s
replicasetchenxi-xsm2d   1/1     Running   0          93s

  查看控制器

[root@master-1 kongzhiqi]# kubectl get replicaset -n replicaset 
NAME               DESIRED   CURRENT   READY   AGE
replicasetchenxi   5         5         5       3m46s
[root@master-1 kongzhiqi]# kubectl get replicaset -n replicaset  -o wide
NAME               DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
replicasetchenxi   5         5         5       3m59s   web          nginx    dev=chenxi

  删除pod 自动恢复

[root@master-1 kongzhiqi]# kubectl get pod -n replicaset 
NAME                     READY   STATUS    RESTARTS   AGE
replicasetchenxi-69xtv   1/1     Running   0          18s
replicasetchenxi-8qkfl   1/1     Running   0          14m
replicasetchenxi-dslcp   1/1     Running   0          14m
replicasetchenxi-f6dmw   1/1     Running   0          14m
replicasetchenxi-xsm2d   1/1     Running   0          14m
[root@master-1 kongzhiqi]# kubectl delete pod -n replicaset replicasetchenxi-8qkfl   #删除指定pod
pod "replicasetchenxi-8qkfl" deleted
[root@master-1 kongzhiqi]# kubectl get pod -n replicaset 
NAME                     READY   STATUS    RESTARTS   AGE
replicasetchenxi-69xtv   1/1     Running   0          118s
replicasetchenxi-6ghvl   1/1     Running   0          6s
replicasetchenxi-dslcp   1/1     Running   0          15m
replicasetchenxi-f6dmw   1/1     Running   0          15m
replicasetchenxi-xsm2d   1/1     Running   0          15m

1.Deployment概念

Deployment控制器为 Pod 和 ReplicaSet 提供了一个声明式更新的方法,在Deployment对象中描述一个期望的状态,Deployment控制器就会按照一定的控制速率把实际状态改成期望状态,通过定义一个Deployment控制器会创建一个新的ReplicaSets控制器,通过replicaset创建pod,删除Deployment控制器,也会删除Deployment控制器下对应的ReplicaSet控制器和pod资源,Deployment可以用来管理上面说到的那种蓝绿发布情况的,建立在rs之上的,一个Deployment可以管理多个rs,有多个rs存在,但实际运行的只有一个,当你更新到一个新版本的时候,只是创建了一个新的rs,把旧的rs替换掉了

Deployment可以使用声明式定义,直接在命令行通过纯命令的方式完成对应资源版本的内容的修改,也就是通过打补丁的方式进行修改;Deployment能提供滚动式自定义自控制的更新;对Deployment来讲,我们在实现更新时还可以实现控制更新节奏和更新逻辑,比如说ReplicaSet控制5个pod副本,pod的期望值是5个,但是升级的时候需要额外多几个pod,那么我们控制器可以控制在5个pod副本之外还能再增加几个pod副本;比方说能多一个,但是不能少,那么升级的时候就是先增加一个,再删除一个,增加一个删除一个,始终保持pod副本数是5个,但是有个别交叉之间是6个;还有一种情况,最多允许多一个,最少允许少一个,也就是最多6个,最少4个,第一次加一个,删除两个,第二次加两个,删除两个,依次类推,可以自己控制更新方式,这种是滚动更新的,需要加readinessProbe和livenessProbe探测,确保pod中容器里的应用都正常启动了才删除之前的pod;启动的第一步,刚更新第一批就暂停了也可以;假如目标是5个,允许一个也不能少,允许最多可以10个,那一次加5个即可;这就是我们可以自己控制节奏来控制更新的方法

典型的使用场景:

1)创建无状态的应用

2)滚动更新和回滚

帮助

KIND:     Deployment
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the Deployment.

     DeploymentSpec is the specification of the desired behavior of the
     Deployment.

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)

   paused	<boolean> 表示部署已暂停。
     Indicates that the deployment is paused.

   progressDeadlineSeconds	<integer>  部署在完成之前取得进展的最长时间
     The maximum time in seconds for a deployment to make progress before it is
     considered to be failed. The deployment controller will continue to process
     failed deployments and a condition with a ProgressDeadlineExceeded reason
     will be surfaced in the deployment status. Note that progress will not be
     estimated during the time a deployment is paused. Defaults to 600s.

   replicas	<integer>  副本个数
     Number of desired pods. This is a pointer to distinguish between explicit
     zero and not specified. Defaults to 1.

   revisionHistoryLimit	<integer>  保留历史版本个数
     The number of old ReplicaSets to retain to allow rollback. This is a
     pointer to distinguish between explicit zero and not specified. Defaults to
     10.

   selector	<Object> -required- 标签选择器
     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.

   strategy	<Object> 更新策略
     The deployment strategy to use to replace existing pods with new ones.

   template	<Object> -required- pod 属性定义
     Template describes the pods that will be created.

  更新策略帮助

[root@master-1 ~]# kubectl explain deployment.spec.strategy.rollingUpdate
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: rollingUpdate <Object>

DESCRIPTION:
     Rolling update config params. Present only if DeploymentStrategyType =
     RollingUpdate.

     Spec to control the desired behavior of rolling update.

FIELDS:
   maxSurge	<string> 可以调度的最大 Pod 数量高于所需数量
     The maximum number of pods that can be scheduled above the desired number
     of pods. Value can be an absolute number (ex: 5) or a percentage of desired
     pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
     is calculated from percentage by rounding up. Defaults to 25%. Example:
     when this is set to 30%, the new ReplicaSet can be scaled up immediately
     when the rolling update starts, such that the total number of old and new
     pods do not exceed 130% of desired pods. Once old pods have been killed,
     new ReplicaSet can be scaled up further, ensuring that total number of pods
     running at any time during the update is at most 130% of desired pods.

   maxUnavailable	<string> 更新期间不可用的 pod 的最大数量。
     The maximum number of pods that can be unavailable during the update. Value
     can be an absolute number (ex: 5) or a percentage of desired pods (ex:
     10%). Absolute number is calculated from percentage by rounding down. This
     can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set
     to 30%, the old ReplicaSet can be scaled down to 70% of desired pods
     immediately when the rolling update starts. Once new pods are ready, old
     ReplicaSet can be scaled down further, followed by scaling up the new
     ReplicaSet, ensuring that the total number of pods available at all times
     during the update is at least 70% of desired pods.

 2.使用Deployment运行一个无状态应用

(1)创建一个nginx deployment

[root@master-1 kongzhiqi]# cat deployment.yaml
apiVersion: apps/v1  #api版本
kind: Deployment   #资源类型
metadata:   #源数据
  name: deployment   #控制器名字
  namespace: deployment  # 所在的名称空间
  labels: # 标签的设置
    dev: deployment-test
spec: # 控制器期望状态
  minReadySeconds: 4 # 等待就绪时间
  revisionHistoryLimit: 5 # 保留的历史版本
  replicas: 3 # pod 个数
  strategy: # 更新策略
    rollingUpdate: # 选择更新方式
      maxSurge: 5 # 最大的pod 数量比pod个数多
      maxUnavailable: 1 # 最大不可用的pod 数量
  selector: # 标签选择器
    matchLabels: # 标签选择器设定
      dev: deployment-test # 标签的key与值的设定
  template: # pod 属性定义
    metadata: # 元数据
      labels: # 标签设定
        dev: deployment-test # 标签的key 与值
    spec: # pod 的期望状态
      containers: # 容器的属性定义
      - name: web  # 容器的名字
        image: nginx:1.9.1 # 运行的镜像
        imagePullPolicy: IfNotPresent   # 获取镜像策略
        ports: # 端口设置
        - name: web # 端口的名字
          containerPort: 80 # 容器的端口
        livenessProbe: # 存活性探测属性设置
          httpGet:  # 探测类型
            port: web  # 探测的端口
            path: index.html  # 探测的url
          initialDelaySeconds: 5 # 容器里进程初始化时间等待探测时间
          periodSeconds: 5 # 探测周期
          timeoutSeconds: 2 #探测超时时间
          failureThreshold: 3  # 探测连续失败的最大次数
        readinessProbe: # 就绪性探测
          httpGet:  # 探测的钩子
            port: web # 探测的端 口
            path: index.html #探测的url
          initialDelaySeconds: 5 # 容器里进程初始化时间等待探测时间
          periodSeconds: 5 # 探测周期
          timeoutSeconds: 2 #探测超时时间
          failureThreshold: 3  # 探测连续失败的最大次数

  运行

[root@master-1 kongzhiqi]# kubectl apply -f deployment.yaml 
deployment.apps/deployment created
[root@master-1 kongzhiqi]# kubectl get pod -n deployment
NAME                          READY   STATUS    RESTARTS   AGE
deployment-68c84ccc46-27mhj   0/1     Running   0          43s
deployment-68c84ccc46-67fgn   1/1     Running   0          43s
deployment-68c84ccc46-g5wtv   1/1     Running   0          43s
[root@master-1 kongzhiqi]# kubectl get pod -n deployment
NAME                          READY   STATUS    RESTARTS   AGE
deployment-68c84ccc46-27mhj   0/1     Running   0          43s
deployment-68c84ccc46-67fgn   1/1     Running   0          43s
deployment-68c84ccc46-g5wtv   1/1     Running   0          43s
[root@master-1 kongzhiqi]# kubectl get pod -n deployment
NAME                          READY   STATUS    RESTARTS   AGE
deployment-68c84ccc46-27mhj   0/1     Running   0          44s
deployment-68c84ccc46-67fgn   1/1     Running   0          44s
deployment-68c84ccc46-g5wtv   1/1     Running   0          44s
[root@master-1 kongzhiqi]# kubectl get pod -n deployment
NAME                          READY   STATUS    RESTARTS   AGE
deployment-68c84ccc46-27mhj   1/1     Running   0          44s
deployment-68c84ccc46-67fgn   1/1     Running   0          44s
deployment-68c84ccc46-g5wtv   1/1     Running   0          44s

  

(2)使用kubectl列举关于deployment信息

 

(3)更新deployment

 

标签:控制器,ReplicaSet,replicasetchenxi,Deployment,概念,Running,deployment,pod
From: https://www.cnblogs.com/rdchenxi/p/16590158.html

相关文章

  • 35.概念/意义一致原则
    主语和谓语动词的一致关系并非只取决于语法上的单、复数形式,还取决于主语的单、复数意义。有时主语在语法形式上并非复数,但含有复数意义,其后的谓语动词应用复数形式;反之,主......
  • JWT 基础概念详解
    什么是JWTJWT(JSONWebToken)是目前最流行的跨域认证解决方案,是一种基于Token的认证授权机制。从JWT的全称可以看出,JWT本身也是Token,一种规范化之后的JSON结构......
  • jsp_概念和原理
    jsp_概念概念:javaserverPages:java服务器端页面可以理解为:一个特殊的页面其中既可以指定定义html标签又可以定义java代码用于简化书写jsp和HTMl的区别HTML......
  • http和https的概念 和 区别
    1、http和https的基本概念http http是客户端和服务端传输请求应答的标准,用与从www服务器传输超文本到本地浏览器的超文本传输协议。httpshttps是以安全为目的的ht......
  • 使用"新概念英语"提升英语水平?
    最近想了下学生时代,英语是一直都没有解决的问题,同时在找工作的时候,也没法看外企的机会.所以想学习一下外语.最先想到的是"新概念"英语,这套丛书4册,特别经典.我就找了......
  • JSP概念和JSP原理
    JSP入门学习概念:JavaServerPages:Java服务器端页面可以理解为:一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码用于简化书写<html><head>......
  • Statefulset+Deamonset控制器实战(简单版本)
    Statefulset说明:  为了解决有状态服务的部署,例如mysql的主从等  pod拥有唯一且固定的名称  按照顺序对pod进行启停、伸缩和回收  HeadlessServices(......
  • k8s控制器探针
    HealthCheck:由发起者对容器进行周期性检测 dockershealthcheck实现方式:#第一种方式:docker-composecat<<EOF>docker-compose.yamlversion:'3.6'service: ......
  • git学习(01) git的概念及相关介绍
    git的定义Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。Git是LinusTorvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本......
  • k8s--deployment 控制器、扩缩容、升级策略
    介绍Deployment表示用户对K8S集群的一次更新操作。Deployment是一个比RS(ReplicaSet,RS)应用模型更广的API对象,可以是创建一个新的服务,更新一个新的服务,也可以......