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 的节点上。
## Taint
默认的master的taints是不允许调度的
```
$ kubectl describe nodes k8s-11
***
Taints: node-role.kubernetes.io/master:NoSchedule
***
```
可以使用命令[kubectl taint](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#taint)给节点增加一个 taint。比如
```
kubectl taint nodes node1 key=value:NoSchedule
```
给节点`node1`增加一个 taint,它的 key 是`key`,value 是`value`,effect 是`NoSchedule`。**这表示只有拥有和这个 taint 相匹配的 toleration 的 pod 才能够被分配到`node1`这个节点**。您可以在 PodSpec 中定义 pod 的 toleration。下面两个 toleration 均与上面例子中使用`kubectl taint`命令创建的 taint 相匹配,因此如果一个 pod 拥有其中的任何一个 toleration 都能够被分配到`node1`
想删除上述命令添加的 taint ,您可以运行:
```
kubectl taint nodes node1 key:NoSchedule-
```
~~~yaml
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
~~~
~~~yaml
tolerations:
- key: "key"
operator: "Exists"
effect: "NoSchedule"
~~~
`operator`可以定义为:
* Equal 表示key是否等于value,默认
* Exists 表示key是否存在,此时无需定义value
`effect`可以定义为:
* NoSchedule 表示不允许调度,已调度的不影响
* PreferNoSchedule 表示尽量不调度
* NoExecute 表示不允许调度,已调度的在tolerationSeconds(定义在Tolerations上)后删除
一个 toleration 和一个 taint 相“匹配”是指它们有一样的 key 和 effect ,并且:
* 如果`operator`是`Exists`(此时 toleration 不能指定`value`),或者
* 如果`operator`是`Equal`,则它们的`value`应该相等
>**Note:**
>**注意:**存在两种特殊情况:
>* 如果一个 toleration 的`key`为空且 operator 为`Exists`,表示这个 toleration 与任意的 key 、 value 和 effect 都匹配,即这个 toleration 能容忍任意 taint。
>~~~yaml
> tolerations:
> - operator: "Exists"
> ~~~
>* 如果一个 toleration 的`effect`为空,则`key`值与之相同的相匹配 taint 的`effect`可以是任意值。
>~~~yaml
>tolerations:
>- key: "key"
> operator: "Exists"
>~~~
## toleration
可以让pod调度到master节点上
~~~yaml
spec:
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
containers:
- name: nginx
~~~
---
### 相关文档
[https://kubernetes.io/zh/docs/concepts/configuration/taint-and-toleration/](https://kubernetes.io/zh/docs/concepts/configuration/taint-and-toleration/)
- kubernetes基础
- 安装kubernetes
- kubeadm平滑升级群集
- Taint和Toleration
- 使用HostAliases向Pod /etc/hosts 文件添加条目
- ConfigMap
- 插件
- 支持外部dns
- 安装helm
- HPA
- 存储
- 本地存储
- 网络存储
- Secret
- ConfigMap
- QA
- k8s使用时需要注意的坑点
- 容器中的JVM资源该如何被安全的限制
- 项目实践
- eureka集群
- Traefik ingress服务发现与负载均衡
- etcd数据备份与恢复
- deployment滚动升级与回滚
- 监控
- prometheus operator初体验
- prometheus-operator监控
- metrics-server监控kubernetes资源
- weave scope可视化监控