1. 有时我们需要将某些节点作为单独的用途,不想别的 pod 调度到这些节点上时可以对节点设置污点 taint。

  2. 而我们想要让特别的应用的 pod 调度到这些有污点的节点上时,需要在这些 pod 的调度策略中指明什么样的污点可以被容忍(tolerations),并且按亲和性(nodeAffinity)调度达到对应的节点上。

关于污点容忍(taint 和tolerations):

污点taint 有3种作用类型:

  • NoSchedule 表示不允许调度,已调度的不影响
  • PreferNoSchedule 表示尽量不调度
  • NoExecute 表示不允许调度,已调度的在tolerationSeconds(定义在Tolerations上)后删除

下面举个例子给节点打上污点和允许 pod 容忍污点,并按亲和性调度到固定的节点上:

有3个节点用作存储节点,不希望一般的应用调度到这3个节点上,并且存储相关的应用 pod 需要只跑在这3台存储节点上,3个节点为:

k8s-product01-ceph01
k8s-product01-ceph02
k8s-product01-ceph03

如果节点没有别的 pod 在跑,直接设置taint 为NoSchedule:

  • kubectl taint nodes NODE_NAME KEY=VALUE:TAINT_TYPE (VALUE 可以为空)
kubectl taint nodes k8s-product01-ceph01 storage-node/ceph=with_ssd:NoSchedule
kubectl taint nodes k8s-product01-ceph02 storage-node/ceph=with_ssd:NoSchedule
kubectl taint nodes k8s-product01-ceph03 storage-node/ceph=with_ssd:NoSchedule
*注: 如果节点上已经有别的 pod 在跑了,想要驱逐无关的 pod,可以设置NoExecute:
    kubectl taint nodes k8s-product01-ceph01 storage-node/ceph=with_ssd:NoExecute
    kubectl taint nodes k8s-product01-ceph02 storage-node/ceph=with_ssd:NoExecute
    kubectl taint nodes k8s-product01-ceph03 storage-node/ceph=with_ssd:NoExecute
    
  不能容忍污点的 pod 被驱逐后,再赋予NoSchedule,最后去掉NoExecute(顺序不能反,否则会有 pod 在配置间隙调度过来)
    kubectl taint nodes k8s-product01-ceph01 storage-node/ceph=with_ssd:NoSchedule --overwrite
    kubectl taint nodes k8s-product01-ceph02 storage-node/ceph=with_ssd:NoSchedule --overwrite
    kubectl taint nodes k8s-product01-ceph03 storage-node/ceph=with_ssd:NoSchedule --overwrite
    
    kubectl taint nodes k8s-product01-ceph02 storage-node/ceph:NoExecute-
    kubectl taint nodes k8s-product01-ceph03 storage-node/ceph:NoExecute-
    kubectl taint nodes k8s-product01-ceph01 storage-node/ceph:NoExecute-
  • 让 pod 容忍污点(添加tolerations 的位置跟containers 同级):
  1. 精确容忍-匹配key,value 和作用类型,如
tolerations:
- key: "storage-node/ceph"
  operator: "Equal"
  value: "with_ssd"
  effect: "NoSchedule"
  1. 模糊容忍-除了 key 需要显示指明,value 和 effect 可以不用指明(值为空则包含所有值)
tolerations:
- key: "storage-node/ceph"
  operator: "Equal"
  value: ""
  effect: "NoSchedule"
或
tolerations:
- key: "storage-node/ceph"
  operator: "Equal"
  value: "with_ssd"
  effect: ""
  1. 大尺度容忍,只要key存在,不管是什么污点类型都可以调度上去
tolerations:
- key: storage-node/ceph
  operator: Exists

关于亲和度nodeAffinity:

相关概念

  • 节点选择器: nodeSelector、nodeName(会被弃用)

  • Node亲和性调度:NodeAffinity

  • Pod亲和性调度:PodAffinity

  • Pod反亲和性调度:podAntiAffinity

  • requiredDuringSchedulingIgnoredDuringExecution 硬亲和性 必须满足亲和性。

  • preferredDuringSchedulingIgnoredDuringExecution 软亲和性 能满足最好,不满足也没关系。

下面以NodeAffinity以为例,其他类似

  1. 给节点打标签
#给多个节点打标签
kubectl label nodes {k8s-product01-ceph01,k8s-product01-ceph02,k8s-product01-ceph03} ceph-mon=enabled
  1. 设置 pod 亲和性,pod (affinity与containers同级)
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
        - matchExpressions:
          - key: ceph-mon
            operator: In
            values:
            - enabled

参考:

https://www.cnblogs.com/klvchen/p/10025205.html

https://www.jianshu.com/p/102c4df69af9

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐