Taint-Toleration

48 阅读2分钟

Taint/Toleration

kubeadm默认情况下Master节点是不允许运行用户Pod的,依靠的是Kubernetes的Taint/Toleration机制。

一旦某个节点被加上了一个Taint,即被“打上了污点”,那么所有Pod就都不能在这个节点上运行,甚至将Node已经存在的Pod驱逐出去。

除非有个别的Pod声明自己能“容忍”这个“污点”,即声明了Toleration,它才可以在这个节点上运行。

Taint

每个污点的组成如下

key=value:effect

每个污点有一个 key 和 value 作为污点的标签,其中 value 可以为空,effect 描述污点的作用

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

打污点示例

为节点k8s-master打上Taint的命令是

kubectl taint nodes k8s-master foo=bar:NoSchedule

为节点k8s-master删除Taint的命令是

kubectl taint nodes k8s-master foo=bar:NoSchedule-

查看节点k8s-master的污点信息

kubectl describe node k8s-master | grep Taints

Toleration

声明toleration,在yaml文件中加入tolerations字段

如果toleration容忍标签上与污点匹配,那么就代表容忍此污点,可以调度到此节点上

apiVersion: v1
kind: Pod
...
spec:
tolerations:
  - key: "foo"
    operator: "Equal"
    value: "bar"
    effect: "NoSchedule"

tolerations示例

查看master节点的taint,键为node-role.kubernetes.io/control-plane,没有值

# kubectl describe node master
Taints:             node-role.kubernetes.io/control-plane:NoSchedule

如果想让pod在master节点上运行,需要声明如下,该Pod能够容忍所有以node-role.kubernetes.io/control-plane为键的Taint

apiVersion: v1
kind: Pod
...
spec:
  tolerations:
  - key: "node-role.kubernetes.io/control-plane"
    operator: "Exists"      # Exists表示满足一项即可
    effect: "NoSchedule"    # d排斥等级

tolerations配置说明

toleration通常有三个元素,key,operattor,value,effect

  • key为空,并且operator为Exists

表示匹配所有的key,values,effect,即容忍所有的taints

  • effect为空

表示匹配所有的effect

  • TolerationSeconds

此参数的值与effect:"NoExecute"配套使用,用来指定添加taint之后,不能容忍此污点的pod会在多少秒后被驱逐