Node 污点与 Pod 容忍度
本章涉及的两个高级调度特性为:
Node 污点:在
nodes.spec.taints
进行相关配置;Pod 容忍度:在
pods.spec.tolerations
进行相关配置;
🦁 只有当一个 Pod 能够容忍某个 Node 中的污点,才能够被调度至该 Node 上。
Node 污点
🍓 一个污点信息的结构是 =:,其中 可以为空。
💫 每个污点都可以关联一个效果,即 ,共有下面三种效果:
NoSchedule:(调度期间生效)如果某个 Pod 无法容忍该 Node 污点,则这个 Pod 不能被调度至该 Node 上;
PreferNoSchedule:(调度期间生效)是上面的宽松版本,表示尽量阻止无法容忍该 Node 污点的 Pod 调度至该 Node 上。如果某个 Pod 无法容忍该 Node 污点,但是该 Pod 已经没有其他 Node 可以调度,那么该 Pod 依然会被调度至该 Node 上;
NoExecute:(调度及运行期间生效)若某个 Node 添加了该效果的污点,则无法容忍该污点的 Pod 将不会被调度至该 Node 上,并且该 Node 上原本已存在的 Pod 中若出现无法容忍该污点的 Pod,也会被去除;
展示案例
首先展示一个 Master 节点的 污点信息。
下面是 get nodes -o yaml
的结果:
taints:
- effect: NoSchedule
key: node-role.kubernetes.io/master
下面是 describe nodes
的结果:
Taints: node-role.kubernetes.io/master:NoSchedule
为 Node 添加污点
有两种方式可以为 Node 添加污点:
使用
kubectl taint
命令;修改 YAML 配置文件;
命令修改
为 名为 node1.k8s
的 Node 添加 node-type=production:NoSchedule
污点:
kubectl taint node node1.k8s node-type=production:NoSchedule
修改配置文件
在 nodes.spec.taints.key
中配置污点的 ;在 nodes.spec.taints.value
中配置污点的 ;在 nodes.spec.taints.effect
中配置污点的 :
spec:
taints:
# 污点 node-type=production:NoSchedule
- effect: NoSchedule # (必须)
key: node-type # (必须)
value: production
Pod 容忍度
展示案例
下面展示一个可以运行在 Master 节点的 Pod 的容忍度信息(即容忍 Node 的 node-role.kubernetes.io/master:NoSchedule
污点)。
下面是 get pods -o yaml
的结果:
tolerations:
# 容忍污点 node-role.kubernetes.io/master:NoSchedule
- effect: NoSchedule
key: node-role.kubernetes.io/master
下面是 describe pods
的结果:
Tolerations: node-role.kubernetes.io/master:NoSchedule
为 Pod 添加容忍度
可以通过 YAML 配置文件为 Pod 添加容忍度,下面展示一个例子。下面这个例子创建了一个 Deployment,其负责的 Pod 能够容忍的污点为 node-type=production:NoSchedule
、任何 key 为 CriticalAddonsOnly
的污点、任何 key 为 node.kubernetes.io/not-ready
, effect 为 NoExecute
的污点:
apiVersion: apps/v1
kind: Deployment
metadata:
name: prod
spec:
selector:
matchLabels:
app: prod
replicas: 5
template:
metadata:
labels:
app: prod
spec:
containers:
- args:
- sleep
- "99999"
image: busybox
name: main
tolerations: # 配置容忍度
# 容忍污点 node-type=production:NoSchedule
- key: node-type
operator: Equal # Equal 操作符可以理解为 "="(默认操作符)
value: production
effect: NoSchedule
# 容忍任何 key 为 CriticalAddonsOnly 的污点
- key: CriticalAddonsOnly
operator: Exists
# 容忍任何 key 为 node.kubernetes.io/not-ready, effect 为 NoExecute 的污点 300s,300s之后,若 Node 还有该污点,则该 Pod 被去除
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300 # 容忍时间,即若该 Pod 所在的 Node 被打上了该污点后,可以容忍 300s,超过这个时间,若污点还在,则 Pod 被去除。默认为 空,表示不去除;改为 0,则表示立刻被去除
Last updated
Was this helpful?