千家信息网

Kubernetes中的Taint和Toleration怎么用

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,这期内容当中小编将会给大家带来有关Kubernetes中的Taint和Toleration怎么用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。Taint(污点)和
千家信息网最后更新 2025年02月05日Kubernetes中的Taint和Toleration怎么用

这期内容当中小编将会给大家带来有关Kubernetes中的Taint和Toleration怎么用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Taint(污点)和 Toleration(容忍)可以作用于 node 和 pod 上,其目的是优化 pod 在集群间的调度,这跟节点亲和性类似,只不过它们作用的方式相反,具有 taint 的 node 和 pod 是互斥关系,而具有节点亲和性关系的 node 和 pod 是相吸的。另外还有可以给 node 节点设置 label,通过给 pod 设置 nodeSelector 将 pod 调度到具有匹配标签的节点上。

Taint 和 toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个 taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。如果将 toleration 应用于 pod 上,则表示这些 pod 可以(但不要求)被调度到具有相应 taint 的节点上。

示例

以下分别以为 node 设置 taint 和为 pod 设置 toleration 为例。

为 node 设置 taint

为 node1 设置 taint:

kubectl taint nodes node1 key1=value1:NoSchedulekubectl taint nodes node1 key1=value1:NoExecutekubectl taint nodes node1 key2=value2:NoSchedule

删除上面的 taint:

kubectl taint nodes node1 key1:NoSchedule-kubectl taint nodes node1 key1:NoExecute-kubectl taint nodes node1 key2:NoSchedule-

查看 node1 上的 taint:

kubectl describe nodes node1

为 pod 设置 toleration

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

tolerations:- key: "key1"  operator: "Equal"  value: "value1"  effect: "NoSchedule"- key: "key1"  operator: "Equal"  value: "value1"  effect: "NoExecute"- key: "node.alpha.kubernetes.io/unreachable"  operator: "Exists"  effect: "NoExecute"  tolerationSeconds: 6000
  • value 的值可以为 NoSchedulePreferNoScheduleNoExecute

  • tolerationSeconds 是当 pod 需要被驱逐时,可以继续在 node 上运行的时间。

详细使用方法请参考官方文档。

kubectl taint node [node] key=value[effect]        其中[effect] 可取值: [ NoSchedule | PreferNoSchedule | NoExecute ]      NoSchedule: 一定不能被调度      PreferNoSchedule: 尽量不要调度      NoExecute: 不仅不会调度, 还会驱逐Node上已有的Pod

上述就是小编为大家分享的Kubernetes中的Taint和Toleration怎么用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

0