污点和容忍

This commit is contained in:
huanqing.shao
2019-09-28 10:27:44 +08:00
parent 9ca8073219
commit 5cac30601d
8 changed files with 176 additions and 15 deletions

View File

@ -280,7 +280,13 @@ module.exports = {
'k8s-intermediate/config/config-map',
'k8s-intermediate/config/computing-resource',
'k8s-intermediate/config/assign-pod-node',
// 'k8s-intermediate/config/taints-and-toleration',
{
title: '污点和容忍',
collapsable: true,
children: [
'k8s-intermediate/config/taints-toleration/',
]
},
{
title: 'Secrets',
collapsable: true,
@ -341,6 +347,7 @@ module.exports = {
'k8s-practice/ocp/sequence',
'k8s-practice/ocp/eureka-server',
'k8s-practice/ocp/mysql',
'k8s-practice/ocp/redis',
]
},
]

View File

@ -69,7 +69,7 @@ Kubernetes教程的主要依据是Kubernetes 官网文档,以及使用 Kubo
* [使用 ConfigMap 配置您的应用程序](https://kuboard.cn/learning/k8s-intermediate/config/config-map.html)
* [管理容器的计算资源](https://kuboard.cn/learning/k8s-intermediate/config/computing-resource.html)
* [将容器调度到指定的节点](https://kuboard.cn/learning/k8s-intermediate/config/assign-pod-node.html)
* [污点和容忍 taints and toleration](https://kuboard.cn/learning/k8s-intermediate/config/taints-and-toleration.html)
* [污点和容忍 taints and toleration](https://kuboard.cn/learning/k8s-intermediate/config/taints-toleration/)
* [Secrets](https://kuboard.cn/learning/k8s-intermediate/config/secrets/)
## **Kubernetes高级**

View File

@ -73,10 +73,13 @@ module.exports = siteData => {
}
const siteTitle = this.$siteTitle
const selfTitle = page.frontmatter.home ? null : (
let selfTitle = page.frontmatter.home ? null : (
page.frontmatter.title // explicit title
|| page.title // inferred title
)
if (page.frontmatter.titlePrefix !== undefined) {
selfTitle = page.frontmatter.titlePrefix + '_' + selfTitle
}
if (page.path.indexOf('/learning/') === 0) {
return selfTitle + '_Kubernetes教程_学习K8S'
}

View File

@ -59,7 +59,7 @@ description: Kubernetes免费中文教程目录
* [使用 ConfigMap 配置您的应用程序](/learning/k8s-intermediate/config/config-map.html)
* [管理容器的计算资源](/learning/k8s-intermediate/config/computing-resource.html)
* [将容器调度到指定的节点](/learning/k8s-intermediate/config/assign-pod-node.html)
* [污点和容忍 taints and toleration](/learning/k8s-intermediate/config/taints-and-toleration.html) <Badge text="正在撰写" type="warn"/>
* [污点和容忍 taints and toleration](/learning/k8s-intermediate/config/taints-toleration/)
* [Secrets](/learning/k8s-intermediate/config/secrets/)
## **Kubernetes 高级**

View File

@ -1,8 +0,0 @@
---
layout: LearningLayout
description: Kubernetes教程_在Kubernetes中_配置污点和容忍taints_and_toleration
---
# 污点和容忍 taints and toleration
正在撰写...

View File

@ -0,0 +1,159 @@
---
vssueId: 90
titlePrefix: 污点和容忍
layout: LearningLayout
description: Kubernetes教程_在Kubernetes中_配置污点和容忍taints_and_toleration
---
# 概述
> 参考文档: Kubernetes 官网文档 [Taints and Tolerations](https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/)
Pod 中存在属性 [Node selector / Node affinity](/learning/k8s-intermediate/config/assign-pod-node.html),用于将 Pod 指定到合适的节点。
相对的,节点中存在属性 `污点 taints`,使得节点可以排斥某些 Pod。
污点和容忍taints and tolerations成对工作以确保 Pod 不会被调度到不合适的节点上。
* 可以为节点增加污点taints一个节点可以有 0-N 个污点)
* 可以为 Pod 增加容忍toleration一个 Pod 可以有 0-N 个容忍)
* 如果节点上存在污点则该节点不会接受任何不能容忍tolerate该污点的 Pod。
## 向节点添加污点
* 执行 `kubectl taint` 命令,可向节点添加污点,如下所示:
``` sh
kubectl taint nodes node1 key=value:NoSchedule
```
该命令为节点 `node1` 添加了一个污点。污点是一个键值对,在本例中,污点的键为 `key`,值为 `value`,污点效果为 `NoSchedule`。此污点意味着 Kubernetes 不会向该节点调度任何 Pod除非该 Pod 有一个匹配的容忍toleration
* 执行如下命令可以将本例中的污点移除:
``` sh
kubectl taint nodes node1 key:NoSchedule-
```
## 向 Pod 添加容忍
PodSpec 中有一个 `tolerations` 字段,可用于向 Pod 添加容忍。下面的两个例子中定义的容忍都可以匹配上例中的污点,包含这些容忍的 Pod 也都可以被调度到 `node1` 节点上:
* 容忍1
``` yaml
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
```
* 容忍2
``` yaml
tolerations:
- key: "key"
operator: "Exists"
effect: "NoSchedule"
```
下面这个 Pod 的例子中,使用了容忍:
``` yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
tolerations:
- key: "example-key"
operator: "Exists"
effect: "NoSchedule"
```
## 污点与容忍的匹配
当满足如下条件时Kubernetes 认为容忍和污点匹配:
* 键key相同
* 效果effect相同
* 污点的 `operator` 为:
* `Exists` (此时污点中不应该指定 `value`
* 或者 `Equal` (此时容忍的 `value` 应与污点的 `value` 相同)
如果不指定 `operator`,则其默认为 `Equal`
::: tip 特殊情况
存在如下两种特殊情况:
* 容忍中未定义 `key` 但是定义了 operator 为 `Exists`Kubernetes 认为此容忍匹配所有的污点,如下所示:
```yaml
tolerations:
- operator: "Exists"
```
* 容忍中未定义 `effect` 但是定义了 `key`Kubernetes 认为此容忍匹配所有 `effect`,如下欧式:
``` yaml
tolerations:
- key: "key"
operator: "Exists"
```
:::
支持的效果 `effect` 有:
* **`NoSchedule`**
* **`PreferNoSchedule`** 比 `NoSchedule` 更宽容一些Kubernetes 将尽量避免将没有匹配容忍的 Pod 调度到该节点上,但是并不是不可以
* **`NoExecute`** 不能在节点上运行(如果已经运行,将被驱逐)
一个节点上可以有多个污点,同时一个 Pod 上可以有多个容忍。Kubernetes 使用一种类似于过滤器的方法来处理多个节点和容忍:
* 对于节点的所有污点,检查 Pod 上是否有匹配的容忍,如果存在匹配的容忍,则忽略该污点;
* 剩下的不可忽略的污点将对该 Pod 起作用
例如:
* 如果存在至少一个不可忽略的污点带有效果 `NoSchedule`,则 Kubernetes 不会将 Pod 调度到该节点上
* 如果没有不可忽略的污点带有效果 `NoSchedule`,但是至少存在一个不可忽略的污点带有效果 `PreferNoSchedule`,则 Kubernetes 将尽量避免将该 Pod 调度到此节点
* 如果存在至少一个忽略的污点带有效果 `NoExecute`,则:
* 假设 Pod 已经在该节点上运行Kubernetes 将从该节点上驱逐evict该 Pod
* 假设 Pod 尚未在该节点上运行Kubernetes 将不会把 Pod 调度到该节点
例如,假设您给一个节点添加了三个污点:
``` sh
kubectl taint nodes node1 key1=value1:NoSchedule
kubectl taint nodes node1 key1=value1:NoExecute
kubectl taint nodes node1 key2=value2:NoSchedule
```
同时,有一个 Pod 带有两个容忍:
``` yaml
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoSchedule"
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
```
在这个案例中Pod 上有两个容忍,匹配了节点的前两个污点,只有节点的第三个污点对该 Pod 来说不可忽略,该污点的效果为 `NoSchedule`
* Kubernetes 不会将此 Pod 调度到该节点上
* 如果 Kubernetes 先将 Pod 调度到了该节点,后向该节点添加了第三个污点,则 Pod 将继续在该节点上运行而不会被驱逐(节点上带有 `NoExecute` 效果的污点已被 Pod 上的第二个容忍匹配,因此被忽略)
通常,在带有效果 `NoExecute` 的污点被添加到节点时,节点上任何不容忍该污点的 Pod 将被立刻驱逐,而容忍该污点的 Pod 则不会被驱逐。
此外,带有效果 `NoExecute` 的污点还可以指定一个可选字段 `tolerationSeconds`,该字段指定了 Pod 在多长时间后被驱逐,例如:
``` yaml
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
tolerationSeconds: 3600
```
此例子中,如果 Pod 已经运行在节点上再向节点增加此污点时Pod 将在该节点上继续运行 3600 秒,然后才被驱逐。如果污点在此之间被移除,则 Pod 将不会被驱逐。

View File

@ -22,7 +22,7 @@ Kuboard 网站将陆续 **连载** 如何使用 Kuboard 在 Kubernetes 上部署
* [部署顺序](/learning/k8s-practice/ocp/sequence.html)
* [在K8S上部署eureka-server](/learning/k8s-practice/ocp/eureka-server.html)
* [在K8S上部署mysql](/learning/k8s-practice/ocp/mysql.html)
* [在K8S上部署redis]
* [在K8S上部署redis](/learning/k8s-practice/ocp/redis.html)
* [在K8S上部署auth-server]
* [在K8S上部署user-center]
* [在K8S上部署api-gateway]

View File

@ -151,7 +151,7 @@ Kuboard 为 Kubernetes 初学者设计了如下学习路径:
* [使用 ConfigMap 配置您的应用程序](/learning/k8s-intermediate/config/config-map.html)
* [管理容器的计算资源](/learning/k8s-intermediate/config/computing-resource.html)
* [将容器调度到指定的节点](/learning/k8s-intermediate/config/assign-pod-node.html)
* [污点和容忍 taints and toleration](/learning/k8s-intermediate/config/taints-and-toleration.html) <Badge text="正在撰写" type="warn"/>
* [污点和容忍 taints and toleration](/learning/k8s-intermediate/config/taints-toleration/)
* [Secrets](/learning/k8s-intermediate/config/secrets/)
**Kubernetes 高级**
@ -176,7 +176,7 @@ Kuboard 为 Kubernetes 初学者设计了如下学习路径:
在 Kubernetes 上部署 Spring Cloud 微服务:(Open Capacity Platform)
* [OCP介绍](/leanring/k8s-practice/ocp/)
* [OCP介绍](/learning/k8s-practice/ocp/)
* [准备OCP的构建环境和部署环境](/learning/k8s-practice/ocp/prepare.html)
* [构建docker镜像并推送到仓库](/learning/k8s-practice/ocp/build.html)
* [部署顺序](/learning/k8s-practice/ocp/sequence.html)