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
tolerations和containers同级。key能容忍的污点key。operatorEqual等于表示key=value,Exists不等于,表示当值不等于下面value正常value可以设置为NoSchedule、PreferNoSchedule或NoExecute。effecteffect策略tolerationSeconds是当 pod 需要被驱逐时,可以继续在 node 上运行的时间。
具体的使用方法请参考官方文档。
随时随地看视频