0. 概述

在通常情况下,我们都会让 Kubernetes 帮助我们做决策,将 Pod 调度到对应的 Node 中去运行。但是,有的时候并不能这么随意,我们有着一些要求,于是乎你就需要做一些设定,本文将介绍在 Kubernetes 中,原生支持的所有可能影响调度的字段和设置。

1. 默认调度

节点选择器

通过节点选择器,可以指定 Pod 调度到符合过滤条件的 Node 中,示例:

[root@liqiang.io]# cat node-selector.yaml
... ...
spec:
  nodeSelector:
    kubernetes.io/hostname: 192.168.29.48
  containers:
... ...

2. 高级调度

污点和容忍度

  • 污点:节点有污点
    • 一个 Pod 如果不能容忍这个污点,将无法调度到对应的节点
  • 污点格式:key=value:operation
    • key:node-role.kubernetes.io/master
    • value:可以为空
    • 效果
      • NoSchedule:如果 pod 没有容忍这些污点,pod 则不能被调度到包含这些污点的节点上。
      • PreferNoSchedule:NoSchedule 的一个宽松的版本,表示尽量阻止 pod 被调度到这个节点上,但是如果没有其他节点可以调度,pod 依然会被调度到这个节点上。
      • NoExecute:不同于 NoSchedule 以及 PreferNoSchedule,后两者只在调度期间起作用, 而 NoExecute 也会影响正在节点上运行着的 pod 。 如果在一个节点上添加了 NoExecute 污点,那些在该节点上运行着的 pod,如果没有容忍这个 NoExecute 污点,将会从这个节点去除。
  • 尽管在 pod 的污点容忍度中显示了等号,但是在节点的污点信息中却没有。当污点或者污点容忍度中的 value 为 null 时,kubectl 故意将污点和污点容忍度进行不同形式的显示。

操作

  • 添加污点:
    • k taint node 192.168.28.217 node-type=production:NoSchedule
  • 查看污点:
    • k describe nodes 192.168.28.217 | grep -A 5 Tain
  • 删除污点:
    • k taint node 192.168.28.217 node-type=production:NoSchedule-
  • 添加污点容忍
        tolerations:
          - key: node-type
            operator: Equal
            value: production
            effect: NoSchedule
    
    • 重新调度时间
        - effect: NoExecute
          key: node.alpha.kubernetes.io/unreachable
          operator: Exists
          tolerationSeconds: 300 # 该pod允许所在节点处于 notReady 状态为300秒,之后pod将被重新调度
      

节点亲缘性

  • 亲缘性规则只会影响正在被调度的 Pod,并且不会导致己经在运行的 Pod 被剔除
  • 可以指定优先级

操作

      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: gpu
                    operator: In
                    values:
                      - "true"

Pod 亲缘性

  • 可以通过 Pod 亲缘性将 Pod 部署在同一个节点,或者通过 Pod 非亲缘性将 Pod 不部署在同一个节点
  • 如果擅长了不包含亲缘性设置的 Pod,重新添加回来时也会考虑其他 Pod 的亲缘性
  • Topology Key:pod亲缘性的topologyKey表示了被调度的pod和另 一 个 pod的距离(在同一个节点、 同一个机柜、同一个可用性局域或者可用性地域),(一个用于识别节点的标签)

操作

 podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: security
            operator: In
            values:
            - S1
        topologyKey: failure-domain.beta.kubernetes.io/zone

3. 自定义调度器

You can run multiple, custom schedulers alongside the default scheduler and configure which scheduler to use for each pods.

To schedule a given pod using a specific scheduler, specify the name of the scheduler in that pod specification.

... ...
kind: Pod
... ...
spec:
  schedulerName: custom-scheduler 
  containers:
... ...