Kubernetes 的 Taint 和 Toleration(污点和容忍)

x33g5p2x  于2021-11-29 转载在 Kubernetes  
字(1.5k)|赞(0)|评价(0)|浏览(201)

Taint 和 Toleration(污点和容忍)

k8s 集群中,节点亲和性 NodeAffinity 是一种 Pod 上定义的属性,能够让 Pod 可以按找我们的需求调度到指定节点上,而 Taints (污点) 则于NodeAffinity (节点亲和性)是相反的,它是一种 Node 上定义的属性,可以让 Pod 不被调度到带污点的节点上,甚至会对带污点节点上已有的 Pod 进行驱逐。对应的 k8s 可以给 Pod 设置 Tolerations(容忍) 让 Pod 能够对有污点的节点设置容忍,将 Pod 调度到该节点。 Taints 一般是配合 Tolerations 使用的。

为 node 设置污点和容忍

NoSchedule: 一定不能被调度
PreferNoSchedule: 尽量不要调度
NoExecute: 不仅不会调度, 还会驱逐Node上已有的Pod

为 node1 设置 taint:

kubectl taint nodes k8s-node1 key1=value1:NoSchedule
kubectl taint nodes k8s-node1 key1=value1:NoExecute
kubectl taint nodes k8s-node1 key2=value2:NoSchedule

查看 node1 上的 taint:

kubectl describe nodes k8s-node1 |grep Taint

删除上面的 taint:

kubectl taint nodes k8s-node1 key1:NoSchedule-
kubectl taint nodes k8s-node1 key1:NoExecute-
kubectl taint nodes k8s-node1 key2:NoSchedule-
kubectl taint nodes k8s-node1 key1-     # 删除指定key所有的effect

为 pod 设置 toleration

只要在 pod 的 spec 中设置 tolerations 字段即可,可以有多个 key,如下所示:

tolerations:            # containers同级
- key: "key1"           # 能容忍的污点key
  operator: "Equal"     # Equal等于表示key=value , Exists不等于,表示当值不等于下面value正常
  value: "value1"       # 值
  effect: "NoSchedule"  # effect策略,可以设置为 NoSchedule、PreferNoSchedule 或 NoExecute
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoExecute"
- key: "node.alpha.kubernetes.io/unreachable"
  operator: "Exists"
  effect: "NoExecute"
  tolerationSeconds: 4000
  • tolerationscontainers 同级。
  • key 能容忍的污点 key
  • operator``Equal 等于表示 key=valueExists 不等于,表示当值不等于下面 value 正常
  • value 可以设置为 NoSchedulePreferNoScheduleNoExecute
  • effect``effect 策略
  • tolerationSeconds 是当 pod 需要被驱逐时,可以继续在 node 上运行的时间。

具体的使用方法请参考官方文档

相关文章