zoukankan      html  css  js  c++  java
  • 第十五章 Kubernetes调度器

    一、简介

    Scheduler kubernetes 的调度器,主要的任务是把定义的 pod 分配到集群的节点上。听起来非常简单,但有很多要考虑的问题:

    ① 公平:如何保证每个节点都能被分配资源

    ② 资源高效利用:集群所有资源最大化被使用

    ③ 效率:调度的性能要好,能够尽快地对大批量的 pod 完成调度工作

    ④ 灵活:允许用户根据自己的需求控制调度的逻辑

    Scheduler 是作为单独的程序运行的,启动之后会一直坚挺 API Server,获取PodSpec.NodeName为空的 pod,对每个 pod 都会创建一个 binding(必须遵守的),表明该 pod 应该放到哪个节点上

     二、调度过程

     调度分为几个部分:

     1) 首先是过滤掉不满足条件的节点,这个过程称为predicate(预选)

     2) 然后对通过的节点按照优先级排序,这个是priority(优选)

     3) 最后从中选择优先级最高的节点。

     如果中间任何一步骤有错误,就直接返回错误(先预选,后优选)

     Predicate(预选)有一系列的算法可以使用:

     ① PodFitsResources:节点上剩余的资源是否大于 pod 请求的资源

     ② PodFitsHost:如果 pod 指定了 NodeName,检查节点名称是否和 NodeName 匹配

     ③ PodFitsHostPorts:节点上已经使用的 port 是否和 pod 申请的 port 冲突

     ④ PodSelectorMatches:过滤掉和 pod 指定的 label 不匹配的节点

     ⑤ NoDiskConflict:已经 mount volume pod 指定的 volume 不冲突,除非它们都是只读

     如果在 predicate 过程中没有合适的节点,pod 会一直在pending状态pending:等待),不断重试调度,直到有节点满足条件。经过这个步骤,如果有多个节点满足条件,就继续 priorities 过程:按照优先级大小对节点排序

     优先级由一系列键值对组成,键是该优先级项的名称,值是它的权重(该项的重要性)。这些优先级选项包括:

     LeastRequestedPriority:通过计算 CPU Memory 的使用率来决定权重,使用率越低权重越高。换句话说,这个优先级指标倾向于资源使用比例更低的节点

    BalancedResourceAllocation:节点上 CPU Memory 使用率越接近,权重越高。这个应该和上面的一起使用,不应该单独使用

    ImageLocalityPriority:倾向于已经有要使用镜像的节点,镜像总大小值越大,权重越高

     通过算法对所有的优先级项目和权重进行计算,得出最终的结果

     三、自定义调度器

     除了 kubernetes 自带的调度器,你也可以编写自己的调度器。通过spec:schedulername参数指定调度器的名字,可以为 pod 选择某个调度器进行调度。比如下面的 pod 选择my-scheduler进行调度,而不是默认的default-scheduler

    apiVersion: v1

    kind: Pod

    metadata:

     name: annotation-second-scheduler

     labels:

     name: multischeduler-example

    spec:

    schedulername: my-scheduler

     containers:

     - name: pod-with-second-annotation-container

    image: gcr.io/google_containers/pause:2.0

     

    四、节点亲和性(podnode的亲和性)

    pod.spec.nodeAffinity

    preferredDuringSchedulingIgnoredDuringExecution(优先执行计划):软策略

    requiredDuringSchedulingIgnoredDuringExecution(要求执行计划):硬策略

    preferred:首选,较喜欢

    required:需要,必修

    requiredDuringSchedulingIgnoredDuringExecution

    #节点硬策略。排除node02,只能在node01上运行

    apiVersion: v1

    kind: Pod

    metadata:

    name: affinity

     labels:

     app: node-affinity-pod

    spec:

     containers:

     - name: with-node-affinity

    image: hub.atguigu.com/library/myapp:v1

     affinity:            #亲和性

     nodeAffinity:        #node亲和性

    requiredDuringSchedulingIgnoredDuringExecution:

     nodeSelectorTerms:

     - matchExpressions:

     - key: kubernetes.io/hostname

    operator: NotIn      #键值运算关系 ,NotIn:label的值不在某个列表中

    values:

     - k8s-node02

     

    Terms:条件

    matchExpressions:匹配表达式

    operator:操作人员

    preferredDuringSchedulingIgnoredDuringExecution

    #r软策略

    apiVersion: v1

    kind: Pod

    metadata:

     name: affinity

    labels:

     app: node-affinity-pod

    spec:

     containers:

     - name: with-node-affinity

     image: hub.atguigu.com/library/myapp:v1

     affinity:

     nodeAffinity:

     preferredDuringSchedulingIgnoredDuringExecution:

     - weight: 1   #权重,权重越大越亲和(多个软策略的情况)

     preference: matchExpressions:

     - key: source

    operator: In

    values:

    - qikqiak

     查看:kubectl get node --show-labels

     

    合体

    apiVersion: v1

    kind: Pod

    metadata:

     name: affinity

     labels:

     app: node-affinity-pod

    spec:

     containers:

     - name: with-node-affinity

     image: hub.atguigu.com/library/myapp:v1

     affinity:

     nodeAffinity:

    requiredDuringSchedulingIgnoredDuringExecution:

     nodeSelectorTerms:

     - matchExpressions:

     - key: kubernetes.io/hostname

     operator: NotIn

     values: - k8s-node02

     preferredDuringSchedulingIgnoredDuringExecution:

     - weight: 1

     preference:

     matchExpressions:

     - key: source

     operator: In

    values:

    - qikqiak

     

    键值运算关系

     ① Inlabel 的值在某个列表中

     ② NotInlabel 的值不在某个列表中

     ③ Gtlabel 的值大于某个值

     ④ Ltlabel 的值小于某个值

     ⑤ Exists:某个 label 存在

     ⑥ DoesNotExist:某个 label 不存在

     五、Pod 亲和性(podpod之间的亲和性)

    pod.spec.affinity.podAffinity/podAntiAffinity

    l preferredDuringSchedulingIgnoredDuringExecution:软策略

    l requiredDuringSchedulingIgnoredDuringExecution:硬策略

    apiVersion: v1

    kind: Pod

    metadata:

     name: pod-3

    labels:

     app: pod-3

    spec:

     containers:

     - name: pod-3

     image: hub.atguigu.com/library/myapp:v1

     affinity:

    podAffinity:

    requiredDuringSchedulingIgnoredDuringExecution:

    - labelSelector: matchExpressions:

    - key: app

    operator: In

    values: - pod-1

     topologyKey: kubernetes.io/hostname

     podAntiAffinity:

     preferredDuringSchedulingIgnoredDuringExecution:

     - weight: 1

     podAffinityTerm:

    labelSelector:

     matchExpressions:

    - key: app

     operator: In

     values:

    - pod-2

     topologyKey: kubernetes.io/hostname

     

    亲和性/反亲和性调度策略比较如下:

     

     六、Taint Toleration

     节点亲和性,是pod的一种属性(偏好或硬性要求),它使pod被吸引到一类特定的节点。Taint 则相反,它使节点能够排斥一类特定的 pod

     Taint toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些 taint pod,是不会被该节点接受的。如果将 toleration 应用于 pod上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上

     1、污点 ( Taint ) 的组成

     使用kubectl taint命令可以给某个 Node 节点设置污点,Node 被设置上污点之后就和 Pod 之间存在了一种相斥的关系,可以让 Node 拒绝 Pod 的调度执行,甚至将 Node 已经存在的 Pod 驱逐出去每个污点的组成如下:

    key=value:effect

    每个污点有一个 key value 作为污点的标签,其中 value 可以为空,effect 描述污点的作用。当前 taint effect 支持如下三个选项:

     NoSchedule:K8Snode添加这个effecf类型污点,新的不能容忍的pod不能再调度过来,但是老的运行在node上不受影响

    NoExecuteK8Snode添加这个effecf类型污点,新的不能容忍的pod不能调度过来,老的pod也会被驱逐

    PreferNoSchedulepod会尝试将pod分配到该节点

    2、污点的设置、查看和去除

     

    #查看节点污点
    kubectl describe node node-name
    
    # 设置污点
    kubectl taint nodes node1 key1=value1:NoSchedule
    # 节点说明中,查找 Taints 字段
    kubectl describe pod pod-name 
    # 去除污点
    kubectl taint nodes node1 key1=value1:NoSchedule-

     

    pod设置容忍一个污点

    apiVersion: apps/v1Beta1
    kind: Deployment
    metadata:
      name: nginx-deploy
    spec:
      replicas: 1
        selector:
          matchLabels:
            app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            images: nginx:laste
            ports:
            - containerPort: 80       
        tolerations:  #containers同级
        - key: "key1"          #能容忍的污点key
          operator: "Equal"    #Equal等于表示key=value , Exists不等于,表示当值不等于下面value正常
          value: "value1"      #值
          effect: "NoExecute"  #effect策略,见上面
          tolerationSeconds: 3600  #原始的pod多久驱逐,注意只有effect: "NoExecute"才能设置,不然报错

    七、容忍(Tolerations)

    设置了污点的 Node 将根据 taint effectNoSchedulePreferNoScheduleNoExecute Pod 之间产生互斥的关系,Pod 将在一定程度上不会被调度到 Node 上。但我们可以在 Pod 上设置容忍 ( Toleration ) ,意思是设置了容忍的 Pod 将可以容忍污点的存在,可以被调度到存在污点的 Node

    pod.spec.tolerations

     

    tolerations:

    - key: "key1"

     operator: "Equal"

     value: "value1"

    effect: "NoSchedule"

    tolerationSeconds: 3600

    - key: "key1"

    operator: "Equal"

     value: "value1"

    effect: "NoExecute"

    - key: "key2"

     operator: "Exists"

    effect: "NoSchedule"

     

     

    其中 key, vaule, effect 要与 Node 上设置的 taint 保持一致

    operator 的值为 Exists 将会忽略 value

    tolerationSeconds 用于描述当 Pod 需要被驱逐时可以在 Pod 上继续保留运行的时间

     

    1、当不指定 key 值时,表示容忍所有的污点 key

    tolerations:

    - operator: "Exists"

    2、当不指定 effect 值时,表示容忍所有的污点作用

    tolerations:

    - key: "key"

    operator: "Exists"

    3、有多个 Master 存在时,防止资源浪费,可以如下设置

    kubectl taint nodes Node-Name node-role.kubernetes.io/master=:PreferNoSchedule

     配置节点故障后Pod重新调度的时间

    例如下面的配置文件,对于notReadyunreachable状态的节点,其上的Pod等待300秒,如果仍未恢复,则会停止执行。

     

     八、指定调度节点

    1、Pod.spec.nodeName Pod 直接调度到指定的 Node 节点上,会跳过 Scheduler 的调度策略,该匹配规则是强制匹配

    apiVersion: extensions/v1beta1

    kind: Deployment

    metadata:

    name: myweb

    spec:

    replicas: 7

    template:

    metadata:

    labels:

    app: myweb

    spec:

     nodeName: k8s-node01

     containers:

     - name: myweb

    image: hub.atguigu.com/library/myapp:v1

     ports:

    - containerPort: 80

     

     

    2、Pod.spec.nodeSelector:通过 kubernetes label-selector 机制选择节点,由调度器调度策略匹配 label,而后调度 Pod 到目标节点,该匹配规则属于强制约束

    apiVersion: extensions/v1beta1

    kind: Deployment

    metadata:

     name: myweb

    spec:

    replicas: 2

     template:

    metadata:

    labels:

    app: myweb

    spec:

    nodeSelector:

    type: backEndNode1

    containers:

     - name: myweb

     image: harbor/tomcat:8.5-jre8

    ports:

    - containerPort: 80

     

     

    链接:

    https://www.cnblogs.com/cocowool/p/taints_and_tolerations.html

    https://www.bilibili.com/video/av66617940/?p=58

    NoExecuteK8Snode添加这个effecf类型污点,新的不能容忍的pod不能调度过来,老的pod也会被驱逐

  • 相关阅读:
    合约广告系统-在线分配问题
    合约广告系统-合约广告系统
    合约广告系统-常用广告系统开源工具
    knnsearch
    ISOMAP和MDS降维
    dijstra算法
    矩阵大于某个数置零
    mathtype 公式分节隐藏
    fifo 实现问题
    vhdl 数据类型转换 使用IEEE标准库numeric_std 需要进行两次转换 use ieee.numeric_std.all;
  • 原文地址:https://www.cnblogs.com/LiuQizhong/p/11905486.html
Copyright © 2011-2022 走看看