「KUBERNETES」- Taints and Tolerations(学习笔记)

该笔记将记录:如何控制 Node 与 Pod 的调度,如何从一个节点中驱除 Pod 实例,如何将 Pod 调度到带有”污点“的节点;

kubernetes/Concepts/Taints and Tolerations

解决方案

该笔记仅作简单记录,详细细节及更多参数,参考 Taints and Tolerations 文档;

英语单词

taint,[teɪnt],腐坏、污染
tolerations,[ˌtɑːləˈreɪʃn],忍受、容忍

基础概念

Assigning Pods to Nodes 中,描述如何将 Pod 运行在特定的节点上(不管是强制还建议)。而 taint 恰巧相反,它使节点驱逐 Pod 实例;

将某个节点标记为”污染“(taint)后,任何不能够”容忍“(toleration)污染的 Pod 实例,都无法运行在该节点中。因此 taint 是作用于节点,而 toleration 则是作用于 Pod 实例;

如果在 Pod 的 spec 中定义 toleration 字段,那么 Pod 才会调度到满足该条件的”污染节点“。可以说:这个 Pod 能够容忍节点的污染;

基础操作

How can I list the taints on my nodes?

案例:防止 Pod 调度到 Node 上(Taint)

例如,某个节点不稳定,我们不希望 Pod 被调度到该节点上。可以执行如下命令:

# ----------------------------------------------------------------------------- # 添加 Taint

# node1:节点名称
# problem=unstable:自定义 KEY=VALUE 形式的标签
# NoSchedule:三种行为之一,表示不要向该节点调度 Pod 实例;
kubectl taint nodes "node1" problem=unstable:NoSchedule

# ----------------------------------------------------------------------------- # 移除 Taint

# 后缀减号(-)表示删除
kubectl taint nodes "node1" problem:NoSchedule-

# ----------------------------------------------------------------------------- # 修改 Taint

# WIP

# ----------------------------------------------------------------------------- # 查看 Taint

kubectl get nodes -o json | jq '.items[].spec' 

在添加污点后,将不会把 Pod 再调度到该 Node 上。但是,如果想驱逐正在该节点上运行的 Pod 实例,将命令中的 NoSchedule 替换为 NoExecute 即可;

场景 | 使 Pod 调度到 Node 上(Toleration)

该节点在被标记为 taint 之后,我们依旧可以将 Pod 调度到该节点上。只需要修改 Pod 定义:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  ####### 需要添加的部分 Start #######
  tolerations:
  - key: "problem"
    operator: "Exists"
    effect: "NoSchedule"
  ####### 需要添加的部分 End #######

上面的”需要添加的部分“表示:如果可以调度到”存在 key 为 problem“且”效果为 NoSchedule“的节点上;

场景 | 运行 Pod 调度打 Master 节点

Scheduler is not scheduling Pod for DaemonSet in Master node
coreos – Allow scheduling of pods on Kubernetes master? – Stack Overflow

通过删除节点 Taint 实现:

# kubectl taint node "<node name>" node-role.kubernetes.io/master:NoSchedule-

场景 | 容忍所有污点 | Toleration Everything

Taints and Tolerations | Kubernetes

问题描述:针对某些 Pod(监控收集、日志采集),需要调度到所有的节点上,所以这些 Pod 需要容忍各种污点。

解决方案:

Note:
There are two special cases:

An empty key with operator Exists matches all keys, values and effects which means this will tolerate everything.

An empty effect matches all effects with key key1.

tolerations:
- key: ""
  operator: "Exists"