Kubernetes之Taints污点与Tolerations容忍

本文已参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

1 概述

节点和Pod亲和力,是将Pod吸引到一组节点(作为优选或硬性要求)。

污点(Taints)则相反,它们允许一个节点排斥一组Pod。

容忍(Tolerations)应用于Pod,允许(但不强制要求)Pod调度到具有匹配污点的节点上。

污点(Taints)和容忍(Tolerations)共同作用,确保Pod不会被调度到不适当的节点。一个或多个污点应用于节点;这标志着该节点不应该接受任何不容忍污点的Pod。

说明:我们在平常使用中发现Pod不会调度到k8s的master节点,就是因为master节点存在污点。

2 Taints污点

2.1 Taints污点的组成

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

每个污点的组成如下:

key=value:effect
复制代码

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

  • NoSchedule:表示K8S将不会把Pod调度到具有该污点的Node节点上
  • PreferNoSchedule:表示K8S将尽量避免把Pod调度到具有该污点的Node节点上
  • NoExecute:表示K8S将不会把Pod调度到具有该污点的Node节点上,同时会将Node上已经存在的Pod驱逐出去

2.2 关于NoExecute需要注意

taint 的 effect 值 NoExecute,它会影响已经在节点上运行的 Pod:

  • 如果 Pod 不能容忍 effect 值为 NoExecute 的 taint,那么 Pod 将马上被驱逐
  • 如果 Pod 能够容忍 effect 值为 NoExecute 的 taint,且在 toleration 定义中没有指定 tolerationSeconds,则 Pod 会一直在这个节点上运行。
  • 如果 Pod 能够容忍 effect 值为 NoExecute 的 taint,但是在 toleration 定义中指定了 tolerationSeconds,则表示 Pod 还能在这个节点上继续运行的时间长度。

2.3 Taints污点设置

污点(Taints)查看

k8s master节点查看

kubectl describe node k8s-master
复制代码

污点(Taints)添加

[root@centos ~]$ kubectl taint node 10.1.2.8 disk=ssd:NoSchedule
node/10.1.2.8 tainted
复制代码

10.1.2.8节点添加了一个污点(taint),污点的key为disk,value为ssd,污点effect为NoSchedule。这意味着没有Pod可以调度到10.1.2.8节点,除非具有相匹配的容忍。

污点(Taints)删除

[root@centos ~]$ kubectl taint nodes 10.1.2.8 disk=ssd:NoSchedule-
node/10.1.2.8 untainted
[root@centos ~]$ kubectl describe node 10.1.2.8
Name:               10.1.2.8
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/instance-type=S2.LARGE8
                    beta.kubernetes.io/os=linux
                    cloud.tencent.com/node-instance-id=ins-e9d2sh1i
                    failure-domain.beta.kubernetes.io/region=gz
                    failure-domain.beta.kubernetes.io/zone=100002
                    kubernetes.io/arch=amd64
                    kubernetes.io/hostname=10.1.2.8
                    kubernetes.io/os=linux
                    node.kubernetes.io/instance-type=S2.LARGE8
                    topology.kubernetes.io/region=gz
                    topology.kubernetes.io/zone=100002
Annotations:        csi.volume.kubernetes.io/nodeid: {"com.tencent.cloud.csi.cfs":"10.1.2.8"}
                    node.alpha.kubernetes.io/ttl: 0
                    volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Sun, 03 Oct 2021 08:36:20 +0800
Taints:             <none> ## 污点已删除
复制代码

3 Tolerations容忍

设置了污点的Node将根据taint的effect:NoSchedule、PreferNoSchedule、NoExecute和Pod之间产生互斥的关系,Pod将在一定程度上不会被调度到Node上。

但我们可以在Pod上设置容忍(Tolerations),意思是设置了容忍的Pod将可以容忍污点的存在,可以被调度到存在污点的Node上。

pod.spec.tolerations示例

tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"
---
tolerations:
- key: "key"
  operator: "Exists"
  effect: "NoSchedule"
---
tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoExecute"
  tolerationSeconds: 3600
复制代码

重要说明:

  • 其中key、value、effect要与Node上设置的taint保持一致
  • operator的值为Exists时,将会忽略value;只要有key和effect就行
  • tolerationSeconds:表示pod 能够容忍 effect 值为 NoExecute 的 taint;当指定了 tolerationSeconds【容忍时间】,则表示 pod 还能在这个节点上继续运行的时间长度。

3.1 当不指定key值时

当不指定key值和effect值时,且operator为Exists,表示容忍所有的污点【能匹配污点所有的keys,values和effects】

tolerations:
- operator: "Exists"
复制代码

3.2 当不指定effect值时

当不指定effect值时,则能匹配污点key对应的所有effects情况

tolerations:
- key: "key"
  operator: "Exists"
复制代码

3.3 当有多个Master存在时

当有多个Master存在时,为了防止资源浪费,可以进行如下设置:

kubectl taint nodes Node-name node-role.kubernetes.io/master=:PreferNoSchedule
复制代码

4 多个Taints污点和多个Tolerations容忍怎么判断

可以在同一个node节点上设置多个污点(Taints),在同一个pod上设置多个容忍(Tolerations)。Kubernetes处理多个污点和容忍的方式就像一个过滤器:从节点的所有污点开始,然后忽略可以被Pod容忍匹配的污点;保留其余不可忽略的污点,污点的effect对Pod具有显示效果:特别是:

  • 如果有至少一个不可忽略污点,effect为NoSchedule,那么Kubernetes将不调度Pod到该节点
  • 如果没有effect为NoSchedule的不可忽视污点,但有至少一个不可忽视污点,effect为PreferNoSchedule,那么Kubernetes将尽量不调度Pod到该节点
  • 如果有至少一个不可忽视污点,effect为NoExecute,那么Pod将被从该节点驱逐(如果Pod已经在该节点运行),并且不会被调度到该节点(如果Pod还未在该节点运行)

\

猜你喜欢

转载自juejin.im/post/7017637252989878279