k8s-集群调度过程

142 阅读5分钟

Scheduler是k8s集群的调度器,主要任务把定义的pod分配到集群的节点上,需要解决的问题有:

  • 公平:保证每个节点都能被分配资源
  • 资源高效利用:集群所有资源最大化被利用
  • 效率:调度的性能要好,能够尽快的对大批量的pod完成调度工作
  • 灵活:允许用户根据自己的需求控制调度的逻辑

Schedule是作为单独的程序运行的,启动之后会一直监听API Server,获取PodSpec.NodeName为空的pod,对每个pod都会创建一个binding,表明该pod应该放到哪个节点上

**调度过程**

首先是过滤掉不满足条件的节点,这个过程称为predicate(预选),然后对通过的节点按照优先级排序,这个是priority(优选),最后从中选择优先级最高的节点。

predicate有一系列的算法可以使用:

  • PodFitsResources:节点上剩余的资源是否大于pod请求的资源
  • PodFitsHost:如果pod指定了NodeName,坚持节点名称是否和NodeName匹配
  • PodFitsHostPorts:节点上已经使用的port是否和pod申请的port冲突
  • PodSelectorMatches:过滤掉和pod指定的label不匹配的节点
  • NoDiskConflict:已经mount的volume和pod指定的volume不冲突,除非他们只是只读
如果在predicate过程中没有合适的节点,pod会一直在pending状态,不断重试调度,直到有节点满足条件。经过这个步骤,如果有多个节点满足条件,就继续priorities过程,按照优先级大小对节点进行排序

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

  • LeastRequestedPriority:通过计算CPU和Memory的使用率来决定权重,使用率越低权重越高,换句话说这个优先级指标倾向于资源使用比例更低的节点
  • BalancedResourceAllocation:节点上CPU和Memory使用率接近,权重越高。这个应该和上面的一起使用,不应该单独使用
  • ImageLocalityPriority:倾向于已经有要使用镜像的节点,镜像总大小值越大,权重越高
**节点亲和性**

pod.spec.nodeAffinity

  • preferredDuringSchedulingIgnoredDuringExecution:软策略
  • requiredDuringSchedulingIgnoredDuringExecution:硬策略
**requiredDuringSchedulingIgnoredDuringExecution**
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**
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
**合体**
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

键值运算关系

  • In:label 的值在某个列表中 
  • NotIn:label 的值不在某个列表中 
  • Gt:label 的值大于某个值 
  • Lt:label 的值小于某个值 
  • Exists:某个 label 存在 
  • DoesNotExist:某个 label 不存在 

Pod亲和性

pod.spec.affinity.podAffinity/podAntiAffinity

  • preferredDuringSchedulingIgnoredDuringExecution:软策略
  • 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
**亲和性/反亲和性调度策略比较如下:**
**调度策略****匹配标签****操作符****拓扑域支持****调度目标**
**nodeAffinity****主机****In, NotIn, Exists,** **DoesNotExist, Gt, Lt****否****指定主机**
**podAffinity****POD****In, NotIn, Exists,** **DoesNotExist****是****POD与指定POD同一拓扑域**
**podAnitAffinity****POD****In, NotIn, Exists,** **DoesNotExist****是****POD与指定POD不在同一拓扑域**
**Taint和Toleration**
节点亲和性,是pod的一种属性,它使pod被吸引到一类特定的节点,Taint则相反,它使节点能够排斥一类特定的pod
**污点(Taint)**
1、污点的组成
使用kubectl taint命令可以给某个node节点设置污点,Node被设置上污点之后就和pod之间存在了一种相斥的关系,可以让node拒绝pod的调度执行,甚至将node已经存在的pod驱逐出去
每个污点的组成如下:
key=value:effect
每个污点有一个key和value作为污点的标签,其中value可以为空,effect描述污点的作用。当前taint effect支持如下三个选项:
  • NoSchedule:表示k8s将不会将pod调度到具有该污点的node上
  • PreferNoSchedule:表示k8s将尽量避免将pod调度到具有该污点的node上
  • NoExcecute:表示k8s将不会将pod调度到具有该污点的node上,同时会将node上已经存在的pod驱逐出去
2、污点的设置、查看和去除
#设置污点kubectl taint nodes node1 key1=value1:NoSchedule

#节点说明中,查找Taints字段
kubectl describe pod pod-name

#去除污点
kubectl taint nodes node1 key1:NoSchedule-
**容忍(Tolerations)**
设置了污点的node将更具taint的effect:NoSchedule、PreferNoSchedule、NoExcute和pod之间产生互斥的关心,pod将在一定程度上不会调度到node上。但我们可以在pod上设置容忍,意思是设置了容忍的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,value,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
固定节点调度
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