- Node affinity: pod 親和節點
- Taints: 節點驅逐pod
一個node可以設置多個污點,不容忍污點的pod不能調度到這個節點,容忍度設置給pod,這樣的pod允許但不是必須調度到有這些污點的node。
概念
添加污點
kubectl taint nodes <node-name> key=value:NoSchedule
刪除污點
kubectl taint nodes <node-name> key:NoSchedule-
下面定義的pod可以調度到上面的node:
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
tolerations:
- key: "key"
operator: "Exists"
effect: "NoSchedule"
沒有value的話operator是Exists,有value就是Equal
- 沒有key的opeartor容忍一切
tolerations:
- operator: "Exists"
- 沒有effect匹配key的所有effects
tolerations:
- key: "key"
operator: "Exists"
三種 effect:
- NoSchedule:不調度
- PreferNoSchedule:儘量不調度
- NoExecute:
一個節點可以有多個污點,一個pod可以有多個容忍。k8s處理的順序像個過濾器:從全部污點開始,忽略與pod容忍匹配的污點,剩下的未忽略的污點指示影響pod的effect,通常:
- 如果有至少一個未忽略的NoSchedule,就不調度pod到該node
- 如果沒有未忽略的effect是NoSchedule的污點,但是至少有一個effect是PreferNoSchedule的污點,就儘量不調度到該node
- 如果至少有一個effect是NoExecute的未忽略污點,pod將被從節點驅逐(如果它已經在該節點運行),並且不會被調度到該node(如果尚未在該node運行)
tolerationSeconds 指示節點添加NoExecute污點以後pod還能在該node待多久,如果在這段時間去除了污點,pod就不被驅逐了。
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
tolerationSeconds: 3600
用例
- 專用節點:通過與定製的admission controller配合,可以指定某些用戶的專用節點
kubectl taint nodes nodename dedicated=groupName:NoSchedule
- 特定硬件的節點
kubectl taint nodes nodename special=true:NoSchedule
- 基於污點的驅逐
基於污點的驅逐
k8s 1.6+,node controller可以在特定條件爲真時自動添加污點:
- node.kubernetes.io/not-ready:NodeCondition的Ready爲False
- node.kubernetes.io/unreachable:NodeCondition的Ready爲Unknown
- node.kubernetes.io/out-of-disk:磁盤要用完了
- node.kubernetes.io/memory-pressure
- node.kubernetes.io/disk-pressure
- node.kubernetes.io/network-unavailable
- node.kubernetes.io/unschedulable
- node.cloudprovider.kubernetes.io/uninitialized
例如,一個pod不想因爲節點網絡問題而被立即驅逐,它希望等會網絡能恢復:
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 6000
k8s的DefaultTolerationSeconds admission controller自動爲pod添加node.kubernetes.io/not-ready:NoExecute for 300s
和node.kubernetes.io/unreachable:NoExecute for 300s
daemonset的這兩個污點沒有tolerationSeconds,使ds的pod永不被驅逐。
根據Condition爲節點打污點
節點生命週期控制器自動爲節點條件創建對應的effect爲NoSchedule的污點。scheduler不檢查幾點的condition而是檢查污點。用戶可以通過添加容忍來忽略節點的問題(通過node condition表示)
1.8+,DaemonSet控制器自動爲所有pod添加NoSchedule容忍,避免它們被驅逐
- node.kubernetes.io/memory-pressure
- node.kubernetes.io/disk-pressure
- node.kubernetes.io/out-of-disk (only for critical pods)
- node.kubernetes.io/unschedulable (1.10 or later)
- node.kubernetes.io/network-unavailable (host network only)