• - -

作者:[SRE運(yùn)維博客](https://www.cnsre.cn/ )

博客地址:[https://www.cnsre.cn/](https://www.cnsre.cn/)

文章地址:??https://www.cnsre.cn/posts/211129946481/??

相關(guān)話題:??https://www.cnsre.cn/kubernetes/??

  • - -

Taint 和 Toleration(污點(diǎn)和容忍)

在 ??k8s?? 集群中,節(jié)點(diǎn)親和性 ??NodeAffinity?? 是一種 ??Pod?? 上定義的屬性,能夠讓 ??Pod?? 可以按找我們的需求調(diào)度到指定節(jié)點(diǎn)上,而 ??Taints?? (污點(diǎn)) 則于??NodeAffinity?? (節(jié)點(diǎn)親和性)是相反的,它是一種 ??Node?? 上定義的屬性,可以讓 ??Pod?? 不被調(diào)度到帶污點(diǎn)的節(jié)點(diǎn)上,甚至?xí)?duì)帶污點(diǎn)節(jié)點(diǎn)上已有的 ??Pod?? 進(jìn)行驅(qū)逐。對(duì)應(yīng)的 ??k8s?? 可以給 ??Pod?? 設(shè)置 ??Tolerations??(容忍) 讓 ??Pod?? 能夠?qū)τ形埸c(diǎn)的節(jié)點(diǎn)設(shè)置容忍,將 ??Pod?? 調(diào)度到該節(jié)點(diǎn)。 ??Taints?? 一般是配合 ??Tolerations?? 使用的。

為 node 設(shè)置污點(diǎn)和容忍

NoSchedule: 一定不能被調(diào)度

PreferNoSchedule: 盡量不要調(diào)度

NoExecute: 不僅不會(huì)調(diào)度, 還會(huì)驅(qū)逐Node上已有的Pod

為 node1 設(shè)置 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 設(shè)置 toleration

只要在 pod 的 spec 中設(shè)置 tolerations 字段即可,可以有多個(gè) ??key??,如下所示:

tolerations:            # containers同級(jí)

- key: "key1" # 能容忍的污點(diǎn)key

operator: "Equal" # Equal等于表示key=value , Exists不等于,表示當(dāng)值不等于下面value正常

value: "value1" # 值

effect: "NoSchedule" # effect策略,可以設(shè)置為 NoSchedule、PreferNoSchedule 或 NoExecute

- key: "key1"

operator: "Equal"

value: "value1"

effect: "NoExecute"

- key: "node.alpha.kubernetes.io/unreachable"

operator: "Exists"

effect: "NoExecute"

tolerationSeconds: 4000
  • ??tolerations?? 和 ??containers?? 同級(jí)。
  • ??key?? 能容忍的污點(diǎn) ??key??。
  • ??operator?? ??Equal?? 等于表示 ??key=value?? , ??Exists?? 不等于,表示當(dāng)值不等于下面 ??value?? 正常
  • ??value?? 可以設(shè)置為 ??NoSchedule??、??PreferNoSchedule?? 或 ??NoExecute??。
  • ??effect?? ??effect?? 策略
  • ??tolerationSeconds?? 是當(dāng) pod 需要被驅(qū)逐時(shí),可以繼續(xù)在 node 上運(yùn)行的時(shí)間。

具體的使用方法請(qǐng)參考??官方文檔??。

  • - -

作者:[SRE運(yùn)維博客](https://www.cnsre.cn/ )

博客地址:[https://www.cnsre.cn/](https://www.cnsre.cn/)

文章地址:??https://www.cnsre.cn/posts/211129946481/??

相關(guān)話題:??https://www.cnsre.cn/tags/kubernetes/??

  • - -