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:NoSchedule
kubectl taint nodes node1 key1=value1:NoExecute
kubectl 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-
为 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
apiVersion: v1
kind: Pod
metadata:
name: tf1
spec:
nodeSelector:
kubernetes.io/hostname: 10.10.10.11 # 指定调度到 10.10.10.11
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoSchedule"
containers:
- name: demo
image: horovod:0.18.2-tf2.0.0-torch1.3.0-mxnet1.5.0-py3.6-gpu
command: ["bash", "-c"]
args: ["/usr/sbin/sshd -p 12345; sleep infinity"]
imagePullPolicy: Always
resources:
limits:
nvidia.com/gpu: 1
restartPolicy: Always
imagePullPolicy: IfNotPresent