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