Try   HackMD

創建一台專屬於 某個 pod 的 專屬機器 kubernetes Taint & Toleration

前言

taint 跟 node affinity 雖然都是屬於 scheduling 的一部份,但要達成的目的其實完全相反:

  • node affinity:設計如何讓 pod 被分派到某個 worker node
  • taint:設計讓 pod 如何不要被分派到某個 worker node

設定 Taint & Toleration

運作規則要分為以下幾個部份說明,分別是:

  • 如何為 node 設定 taint,避免 pod 被分派到上面
  • 如何為 node 移除 taint
  • 如何在 pod spec 中設定 toleration,讓 pod 可以分派到有 taint 的 node 上

如何設定 node taint

每個 taint 都有以下 3 個屬性:

共有三種,分別是 NoSchedule, PreferNoSchedule & NoExecute

  • Key
  • Value
  • Effect

而設定 node taint 很簡單,透過 kubectl 執行以下指令將 3 個屬性給入即可即可:

kubectl taint nodes node1 key=value:NoSchedule

以下是一個實際操作範例:

$ kubectl get node NAME STATUS ROLES AGE VERSION ... (略) leon-k8s-node03 Ready node 15d v1.12.1 leon-k8s-node04 Ready node 15d v1.12.1 leon-k8s-node05 Ready node 15d v1.12.1 # 檢視 node 狀態細節,查看 taint 設定狀態 # 可以看出目前並沒有任何的 taint 設定 $ kubectl describe node/leon-k8s-node03 Name: leon-k8s-node03 Roles: node Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux kubernetes.io/hostname=leon-k8s-node03 node-role.kubernetes.io/node=true ... (略) Taints: <none> Unschedulable: false ... (略) # 為 node03 加上 taint $ kubectl taint nodes leon-k8s-node03 key=value:NoSchedule node/leon-k8s-node03 tainted # 重新檢視 node 狀態細節,察看 taint 設定狀態 # 目前已經多了一個 taint 的設定 $ kubectl describe node/leon-k8s-node03 Name: leon-k8s-node03 Roles: node Labels: beta.kubernetes.io/arch=amd64 beta.kubernetes.io/os=linux kubernetes.io/hostname=leon-k8s-node03 node-role.kubernetes.io/node=true ... (略) Taints: key=value:NoSchedule Unschedulable: false ... (略)

當以上步驟完成後,後續新增進來的 pod 就不會被分派到這個 node 上。

如何移除 taint

移除的語法很簡單,只要在 taint 的 Key:Effect 後面加上 - 即可:

kubectl taint nodes node1 key:NoSchedule-

設定 pod toleration

pod toleration 是設定在 pod spec 中,我們可以設定以下的 pod toleration 讓 pod 可以接受經過上面指令所產生的 node taint:

# 表示可以接受"帶有 key=value & effect=NoSchedule" 的 taint tolerations: - key: "key" operator: "Equal" value: "value" effect: "NoSchedule"

也可以是下面這樣設定:

# 表示可以接受"存在 key(不論 value 為何) & effect=NoSchedule" 的 taint tolerations: - key: "key" operator: "Exists" effect: "NoSchedule"
... spec: containers: - image: xxx/xxx imagePullPolicy: Always name: application-job ports: ... dnsPolicy: ClusterFirst nodeSelector: alpha.eksctl.io/nodegroup-name: Worker-Nodes-PRODUCTION-MW-SP tolerations: - effect: NoSchedule key: dedicated operator: Equal value: application-job

使用 label taint node groups

$ kubectl taint node -l alpha.eksctl.io/nodegroup-name=redis-nodegroup redis=true:NoSchedule ... node/ip-172-20-101-31.ap-northeast-1.compute.internal tainted node/ip-172-20-129-158.ap-northeast-1.compute.internal tainted