kubernetes中Pod调度-Taints污点和污点容忍

一、污点的概念

所谓的污点,是给k8s集群中的节点设置的,通过设置污点,来规划资源创建是所在的节点

污点的类型 解释说明
PreferNoshedule 节点设置这个污点类型后; 表示,该节点接收调度,但是会降低调度的概率
NoSheule 表示,该节点不接收新的调度,以前有的资源,也依然存在
NoExecute 表示,不接收信息的调度,驱逐以前的资源调度

根据节点设置污点

二、污点的管理

1、查看污点

查看过滤数据的前后两行

root@master deployment-demo\]#**kubectl describe nodes \| grep -i taint -A 2 -B 2** volumes.kubernetes.io/controller-managed-attach-detach: true CreationTimestamp: Fri, 29 Mar 2024 16:03:58 +0800 Taints: node-role.kubernetes.io/master:NoSchedule Unschedulable: false Lease: -- volumes.kubernetes.io/controller-managed-attach-detach: true CreationTimestamp: Fri, 29 Mar 2024 16:09:38 +0800 Taints: \ Unschedulable: false Lease: -- volumes.kubernetes.io/controller-managed-attach-detach: true CreationTimestamp: Fri, 29 Mar 2024 16:39:55 +0800 Taints: \ Unschedulable: false Lease:

2.1 创建资源

root@master deployment-demo\]# cat taint.yaml apiVersion: apps/v1 kind: Deployment metadata: name: taint-demo spec: replicas: 5 selector: matchLabels: k8s: dolphin template: metadata: name: pod01 labels: k8s: dolphin spec: containers: - name: c1 image: nginx ports: - containerPort: 80 \[root@master deployment-demo\]# kubectl apply -f taint.yaml deployment.apps/taint-demo created

2.2 创建污点

创建污点有两种方式:

  • 第一种:key=value:污点类型
  • 第二种:key:污点类型

创建污点

root@master deployment-demo\]# **kubectl taint node node1 dolphin:NoExecute**

查看状态,全都被驱离到node2节点上了

2.3 删除污点

root@master deployment-demo\]#**kubectl taint node node1 dolphin:NoExecute-** node/node1 untainted ![](https://file.jishuzhan.net/article/1783515917758500865/b9f953960f966d89a733089601600b40.webp) 我们看到删除pod,node1节点又能重新分配pod资源了

2.4 修改污点

root@master deployment-demo\]# kubectl taint node node1 dolphin:NoExecute \[root@master deployment-demo\]# **kubectl taint node node1 dolphin2=123:NoExecute --overwrite** node/node1 modified ![](https://file.jishuzhan.net/article/1783515917758500865/b19e050e8673e9b03a8ae3f8ca557cef.webp)

3、 污点容忍tolerations

3.1 污点容忍的概念

通过上文,我们知道可以通过给k8s集群节点设置不同类型的"污点",来控制资源创建的节点范围;

那么,k8s也同时提供了"污点容忍",就是即便你的节点设置了"污点",我的资源也可以创建在这个节点上的能力;

假设:一个节点上有两个污点,但是你创建pod的时候,还想在这个节点上创建,那么你就需要在资源清单中,写如"容忍这两个污点",才会创建成功

3.2 污点容忍案例

上边案例中我们已经创建了2个污点,且创建资源时,node1节点不允许分配资源

3.2.1 修改资源清单,设置污点容忍

root@master deployment-demo\]# cat taint.yaml apiVersion: apps/v1 kind: Deployment metadata: name: taint-demo spec: replicas: 5 selector: matchLabels: k8s: dolphin template: metadata: name: pod01 labels: k8s: dolphin spec: # 设置污点容忍 **tolerations: - key: node-role.kubernetes.io/master # master节点的污点 effect: NoSchedule #指定污点类型 operator: Exists #只要匹配到key就满足这个条件 - key: dolphin effect: NoExecute operator: Equal - key: dolphin2 value: "123" effect: NoExecute operator: Equal #key和value都要满足匹配条件** containers: - name: c1 image: nginx ports: - containerPort: 80 \[root@master deployment-demo\]# kubectl apply -f taint.yaml deployment.apps/taint-demo created ![](https://file.jishuzhan.net/article/1783515917758500865/cb8001ce77ada4bd404d5da1e5d626cf.webp)

通过以上污点容忍配置,我们可以看到三个节点都能分配pod资源了

3.2.2 无视所有污点配置

root@master deployment-demo\]# cat taint.yaml apiVersion: apps/v1 kind: Deployment metadata: name: taint-demo spec: replicas: 5 selector: matchLabels: k8s: dolphin template: metadata: name: pod01 labels: k8s: dolphin spec: # 设置污点容忍 **tolerations:** **# 不写污点key的相关属性表示匹配所有key - operator: Exists** containers: - name: c1 image: nginx ports: - containerPort: 80

相关推荐
阿里云云原生2 天前
阿里云获评 Agentic AI 开发平台领导者,函数计算 AgentRun 赢下关键分!
云原生
蝎子莱莱爱打怪2 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀3 天前
Docker部署Nacos
docker·容器
缓解AI焦虑3 天前
Docker + K8s 部署大模型推理服务:资源划分与多实例调度
docker·容器
阿里云云原生3 天前
MSE Nacos Prompt 管理:让 AI Agent 的核心配置真正可治理
微服务·云原生
阿里云云原生3 天前
当 AI Agent 接管手机:移动端如何进行观测
云原生·agent
阿里云云原生3 天前
AI 原生应用开源开发者沙龙·深圳站精彩回顾 & PPT下载
云原生
阿里云云原生3 天前
灵感启发:日产文章 100 篇,打造“实时热点洞察”引擎
云原生
1candobetter3 天前
Docker Compose Build 与 Up 的区别:什么时候必须重建镜像
docker·容器·eureka
~莫子3 天前
Haproxy七层负载详解+实验详细代码
云原生