概念
Taint
和Tolerations
是节点亲和性的一种,节点亲和性,是 pod 的一种属性(偏好或硬性要求),它使 pod 被吸引到一类特定的节点。Taint 则相反,它使节点能够排斥一类特定的 pod
Taint 和 toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。如果将 toleration 应用于 pod上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上
污点组成
使用 kubectl taint 命令可以给某个 Node 节点设置污点,Node 被设置上污点之后就和 Pod 之间存在了一种相斥的关系,可以让 Node 拒绝 Pod 的调度执行,甚至将 Node 已经存在的 Pod 驱逐出去
每个污点的组成如下
key=value:effect
每个污点有一个 key 和 value 作为污点的标签,其中 value 可以为空,effffect 描述污点的作用。当前 taint effffect 支持如下三个选项:
NoSchedule :表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上
PreferNoSchedule :表示 k8s 将尽量避免将 Pod 调度到具有该污点的 Node 上
NoExecute :表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod 驱逐出去
污点容忍示例
# 只能容忍完全匹配的污点key=value:NoSchedule
tolerations:
- key: "key"
operator: "Equal" # 相等
value: "value"
effect: "NoSchedule"
# 容忍污点key=:NoSchedule,只匹配key,不匹配value
tolerations:
- key: "key"
operator: "Exists"
effect: "NoSchedule" # 不调度
# 只能容忍完全匹配的污点key=value1:NoExecute
tolerations:
-key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute" # 不运行,即直接驱逐pod
tolerationSeconds: 3600 #超时时间
特殊污点容忍:
# 容忍所有污点,即任何key和value都可以
tolerations:
- operator: "Exists"
# 容忍污点key=:
tolerations:
- key: "key"
operator: "Exists"
特殊污点示例
# 此污点一般用来给master,即尽可能的不调度
kubectl taint nodes hermes-4-1.local node-role.kubernetes.io/master=:PreferNoSchedule
练习
- 使
deploy
可以容忍污点,在yaml
中的spec.template.spec
添加如下字段
tolerations:
- effect: NoSchedule
key: node.kubernetes.io/unschedulable
operator: Exists
- effect: NoSchedule
key: video-test
operator: Exists
- 给节点打上污点
kubectl drain node hermes-4-1.local --ignore-daemonsets --delete-local-data # 驱逐该节点,会自动给节点打上node.kubernetes.io/unschedulable的污点,同时忽略daemonset,并且删除本地存储
kubectl taint node hermes-4-1 video-test=:NoSchedule # 给节点打上污点video-test
- 查看节点上的污点
使用kubectl describe node hermes-4-1.local可以看到taint字段,如下
Taint: node.kubernetes.io/unschedulable:NoSchedule
video-test:NoSchedule
- 查看节点上的pod
# 此时会发现,除了ds之外,以及能容忍污点的pod外,其他pod都已经被驱逐
kubectl describe no hermes-4-1.local
# 查看到该节点的STATUS为Ready,SchedulingDisabled,即k8s已停止调度
kubectl get no
- 去除节点上的污点
kubectl taint nodes hermes-4-1.local video-test:NoSchedule-