K8S中Pod调度之污点和容忍

news/2025/1/19 15:29:15/

污点和容忍

  • 在 Kubernetes 中,污点(Taints)和容忍(Tolerations)是调度系统中的两个重要概念,它们允许管理员对节点(Node)进行标记,以此来影响 Pod 的调度行为。

  • 前面的调度方式都是站在Pod的角度上,通过在Pod上添加属性,来确定Pod是否要调度到指定的Node上,其实我们也可以站在Node的角度上,通过在Node上添加污点属性,来决定是否允许Pod调度过来。

  • Node被设置上污点之后就和Pod之间存在了一种相斥的关系,进而拒绝Pod调度进来,甚至可以将已经存在的Pod驱逐出去。

 污点(Taints)

污点是添加到节点上的标记,用来指示某种限制条件,它们可以阻止不符合特定条件的 Pod 被调度到这些节点上。污点的一般格式为:key=value:effect

  • key:污点的标识符。

  • value:污点的值,可以为空,用于进一步指定污点的条件。

  • effect:污点的效果,可以是以下几种之一:

    • NoSchedule:这是默认的效果,如果未指定。带有此效果的污点会阻止新的 Pod 被调度到该节点上,但不影响已经在上面运行的 Pod。

    • PreferNoSchedule:带有此效果的污点会推荐不要把 Pod 调度到该节点上,但不是强制的。如果实在没有其他节点可用,Pod 仍然可以被调度到这个节点。

    • NoExecute:带有此效果的污点不仅阻止新的 Pod 被调度到该节点,还会驱逐(驱逐是指 Pod 被删除,其资源被释放)所有不容忍该污点的现有 Pod。

基本命令:

  • 设置污点

$ kubectl taint nodes NODE_NAME key=value:effect- NODE_NAME 要添加污点的节点名称。
- key 是污点的标识符。
- value 是与污点相关联的值(如果需要)。
- effect 是污点的效果,可以是 NoSchedule, PreferNoSchedule 或 NoExecute。
  •  去除污点
$ kubectl taint nodes NODE_NAME key=value:effect-- 将 effect 替换为 -(连字符),这告诉 Kubernetes 移除该特定 key 和 value 的污点。
  •  去除所有污点
$ kubectl taint nodes NODE_NAME key:effect-- 由于没有指定 key 的值,这将移除所有匹配该 key 的污点。
  • 设置污点为PreferNoSchedule

 将node2节点机器关机,只留master和node1节点

在node1节点上设置PreferNoSchedule污点

[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   Ready      control-plane,master   22d   v1.21.10
k8s-node1    Ready      <none>                 22d   v1.21.10
k8s-node2    NotReady   <none>                 22d   v1.21.10[root@k8s-master ~]# kubectl taint nodes k8s-node1 tag=wyx:PreferNoSchedule
node/k8s-node1 tainted[root@k8s-master ~]# kubectl describe nodes k8s-node1
Name:               k8s-node1
Roles:              <none>
Labels:             beta.kubernetes.io/arch=amd64beta.kubernetes.io/os=linuxkubernetes.io/arch=amd64kubernetes.io/hostname=k8s-node1kubernetes.io/os=linux
Annotations:        kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.socknode.alpha.kubernetes.io/ttl: 0projectcalico.org/IPv4Address: 192.168.58.232/24projectcalico.org/IPv4IPIPTunnelAddr: 10.244.36.64volumes.kubernetes.io/controller-managed-attach-detach: true
CreationTimestamp:  Wed, 25 Dec 2024 07:56:26 -0500
Taints:             tag=wyx:PreferNoSchedule
Unschedulable:      false
Lease:HolderIdentity:  k8s-node1AcquireTime:     <unset>RenewTime:       Fri, 17 Jan 2025 03:27:42 -0500
Conditions:Type                 Status  LastHeartbeatTime                 LastTransitionTime                Reason                       Message----                 ------  -----------------                 ------------------                ------                       -------NetworkUnavailable   False   Fri, 17 Jan 2025 03:21:38 -0500   Fri, 17 Jan 2025 03:21:38 -0500   CalicoIsUp                   Calico is running on this nodeMemoryPressure       False   Fri, 17 Jan 2025 03:23:05 -0500   Wed, 25 Dec 2024 07:56:26 -0500   KubeletHasSufficientMemory   kubelet has sufficient memory availableDiskPressure         False   Fri, 17 Jan 2025 03:23:05 -0500   Wed, 25 Dec 2024 07:56:26 -0500   KubeletHasNoDiskPressure     kubelet has no disk pressurePIDPressure          False   Fri, 17 Jan 2025 03:23:05 -0500   Wed, 25 Dec 2024 07:56:26 -0500   KubeletHasSufficientPID      kubelet has sufficient PID availableReady                True    Fri, 17 Jan 2025 03:23:05 -0500   Wed, 25 Dec 2024 08:25:28 -0500   KubeletReady                 kubelet is posting ready status
Addresses:InternalIP:  192.168.58.232Hostname:    k8s-node1
Capacity:cpu:                2ephemeral-storage:  17394Mihugepages-1Gi:      0hugepages-2Mi:      0memory:             3861288Kipods:               110
Allocatable:cpu:                2ephemeral-storage:  16415037823hugepages-1Gi:      0hugepages-2Mi:      0memory:             3758888Kipods:               110
System Info:Machine ID:                 a34fc0322dfe4557acf75b76f37487fbSystem UUID:                CD4D4D56-2260-3BF8-7A19-6F45865B4C71Boot ID:                    b6caabb4-bcfd-4fef-b50a-8c9b0ff56f59Kernel Version:             3.10.0-1160.el7.x86_64OS Image:                   CentOS Linux 7 (Core)Operating System:           linuxArchitecture:               amd64Container Runtime Version:  docker://20.10.8Kubelet Version:            v1.21.10Kube-Proxy Version:         v1.21.10
PodCIDR:                      10.244.1.0/24
PodCIDRs:                     10.244.1.0/24
Non-terminated Pods:          (5 in total)Namespace                   Name                                        CPU Requests  CPU Limits  Memory Requests  Memory Limits  Age---------                   ----                                        ------------  ----------  ---------------  -------------  ---kube-system                 calico-kube-controllers-697d846cf4-79hpj    0 (0%)        0 (0%)      0 (0%)           0 (0%)         22dkube-system                 calico-node-gc547                           250m (12%)    0 (0%)      0 (0%)           0 (0%)         22dkube-system                 coredns-6f6b8cc4f6-5nbb6                    100m (5%)     0 (0%)      70Mi (1%)        170Mi (4%)     22dkube-system                 coredns-6f6b8cc4f6-q9rhc                    100m (5%)     0 (0%)      70Mi (1%)        170Mi (4%)     22dkube-system                 kube-proxy-7hp6l                            0 (0%)        0 (0%)      0 (0%)           0 (0%)         22d
Allocated resources:(Total limits may be over 100 percent, i.e., overcommitted.)Resource           Requests    Limits--------           --------    ------cpu                450m (22%)  0 (0%)memory             140Mi (3%)  340Mi (9%)ephemeral-storage  0 (0%)      0 (0%)hugepages-1Gi      0 (0%)      0 (0%)hugepages-2Mi      0 (0%)      0 (0%)
Events:Type    Reason     Age    From        Message----    ------     ----   ----        -------Normal  NodeReady  22d    kubelet     Node k8s-node1 status is now: NodeReadyNormal  Starting   6m39s  kube-proxy  Starting kube-proxy.
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   Ready      control-plane,master   22d   v1.21.10
k8s-node1    Ready      <none>                 22d   v1.21.10
k8s-node2    NotReady   <none>                 22d   v1.21.10
[root@k8s-master ~]# kubectl create ns dev
namespace/dev created
[root@k8s-master ~]#  kubectl run taint1 --image=nginx:1.17.1 -n dev
pod/taint1 created
[root@k8s-master ~]# kubectl get pod taint1 -n dev -o wide
NAME     READY   STATUS              RESTARTS   AGE   IP       NODE        NOMINATED NODE   READINESS GATES
taint1   0/1     ContainerCreating   0          14s   <none>   k8s-node1   <none>           <none>
[root@k8s-master ~]# kubectl get pod taint1 -n dev -o wide -w
NAME     READY   STATUS              RESTARTS   AGE   IP       NODE        NOMINATED NODE   READINESS GATES
taint1   0/1     ContainerCreating   0          18s   <none>   k8s-node1   <none>           <none>
taint1   1/1     Running             0          24s   10.244.36.71   k8s-node1   <none>           <none>
^C[root@k8s-master ~]# kubectl describe pods taint1 -n dev
Name:         taint1
Namespace:    dev
Priority:     0
Node:         k8s-node1/192.168.58.232
Start Time:   Fri, 17 Jan 2025 03:29:06 -0500
Labels:       run=taint1
Annotations:  cni.projectcalico.org/containerID: a5db60ee3198eafa2d7e89b6a8f57030d33bb5bc6468d2b75431108f00da36d5cni.projectcalico.org/podIP: 10.244.36.71/32cni.projectcalico.org/podIPs: 10.244.36.71/32
Status:       Running
IP:           10.244.36.71
IPs:IP:  10.244.36.71
Containers:taint1:Container ID:   docker://b43c30799394daaf62e7b9712da5a3c6c9a8ffd7dd71d07a21f26004e9ae0a92Image:          nginx:1.17.1Image ID:       docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbbPort:           <none>Host Port:      <none>State:          RunningStarted:      Fri, 17 Jan 2025 03:29:30 -0500Ready:          TrueRestart Count:  0Environment:    <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-m47x8 (ro)
Conditions:Type              StatusInitialized       True Ready             True ContainersReady   True PodScheduled      True 
Volumes:kube-api-access-m47x8:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type    Reason     Age   From               Message----    ------     ----  ----               -------Normal  Scheduled  56s   default-scheduler  Successfully assigned dev/taint1 to k8s-node1Normal  Pulling    54s   kubelet            Pulling image "nginx:1.17.1"Normal  Pulled     33s   kubelet            Successfully pulled image "nginx:1.17.1" in 20.507450779sNormal  Created    32s   kubelet            Created container taint1Normal  Started    32s   kubelet            Started container taint1

 

  • 设置污点为NoSchedule

[root@k8s-master ~]# kubectl taint nodes k8s-node1 tag=wyx:PreferNoSchedule-
node/k8s-node1 untainted
[root@k8s-master ~]# kubectl taint nodes k8s-node1 tag=wyx:NoSchedule
node/k8s-node1 tainted
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS     ROLES                  AGE   VERSION
k8s-master   Ready      control-plane,master   22d   v1.21.10
k8s-node1    Ready      <none>                 22d   v1.21.10
k8s-node2    NotReady   <none>                 22d   v1.21.10
[root@k8s-master ~]# kubectl get pod taint1 -n dev -o wide 
NAME     READY   STATUS    RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
taint1   1/1     Running   0          2m58s   10.244.36.71   k8s-node1   <none>           <none>
[root@k8s-master ~]# kubectl run taint2 --image=nginx:1.17.1 -n dev
pod/taint2 created
[root@k8s-master ~]# kubectl get pod taint2 -n dev -o wide -w
NAME     READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
taint2   0/1     Pending   0          10s   <none>   <none>   <none>           <none>[root@k8s-master ~]# kubectl  describe pods taint2 -n dev
Name:         taint2
Namespace:    dev
Priority:     0
Node:         <none>
Labels:       run=taint2
Annotations:  <none>
Status:       Pending
IP:           
IPs:          <none>
Containers:taint2:Image:        nginx:1.17.1Port:         <none>Host Port:    <none>Environment:  <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-cb226 (ro)
Conditions:Type           StatusPodScheduled   False 
Volumes:kube-api-access-cb226:Type:                    Projected (a volume that contains injected data from multiple sources)TokenExpirationSeconds:  3607ConfigMapName:           kube-root-ca.crtConfigMapOptional:       <nil>DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:Type     Reason            Age   From               Message----     ------            ----  ----               -------Warning  FailedScheduling  49s   default-scheduler  0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 1 node(s) had taint {node.kubernetes.io/unreachable: }, that the pod didn't tolerate, 1 node(s) had taint {tag: wyx}, that the pod didn't tolerate.Warning  FailedScheduling  48s   default-scheduler  0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 1 node(s) had taint {node.kubernetes.io/unreachable: }, that the pod didn't tolerate, 1 node(s) had taint {tag: wyx}, that the pod didn't tolerate.
  •  设置污点为NoExecute

可见之前存在的pod全部被清除,而且新pod也不会被创建 

[root@k8s-master ~]# kubectl taint nodes k8s-node1 tag=wyx:NoSchedule-
node/k8s-node1 untainted
[root@k8s-master ~]#  kubectl taint nodes k8s-node1 tag=wyx:NoExecute
node/k8s-node1 tainted
[root@k8s-master ~]# kubectl get pod -n dev -o wide | grep k8s-node1
No resources found in dev namespace.
[root@k8s-master ~]#  kubectl get pod -n dev -o wide
No resources found in dev namespace.
[root@k8s-master ~]# kubectl run taint3 --image=nginx:1.17.1 -n dev
pod/taint3 created
[root@k8s-master ~]#  kubectl get pod -n dev -o wide -w
NAME     READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
taint3   0/1     Pending   0          5s    <none>   <none>   <none>           <none>

 问题:为什么创建pod时,pod不会被调度到master节点?

因为集群在创建时,master节点就默认加上了污点

 

容忍(Toleration)
  • 在Kubernetes中,污点(Taint)是一种标记在节点上的特殊标记,用来指示该节点不希望某些Pods调度上去。而容忍(Toleration)是Pods的一个属性,它允许Pods忽视节点上的某些污点,从而允许Pods调度到这些节点上。

image-20240511101536095

污点就是拒绝,容忍就是忽略,Node通过污点拒绝pod调度上去,Pod通过容忍忽略拒绝

[root@k8s-master ~]# kubectl describe node k8s-node1 | grep Taints
Taints:             tag=wyx:NoExecute#node1的污点为NoExecute,Pod是无法调度到node1的[root@k8s-master ~]# vim pod-toleration.yaml
[root@k8s-master ~]# cat pod-toleration.yaml 
---
apiVersion: v1
kind: Pod
metadata:name: pod-tolerationnamespace: dev
spec:containers:- name: nginximage: nginx:1.17.1tolerations:- key: "tag"operator: "Equal"value: "wyx"effect: "NoExecute"- key: "tag",这是节点上污点的键。
- operator: "Equal",这表明容忍将匹配具有相同键和值的污点。
- value: "wyx",这是节点上污点的值。
- effect: "NoExecute",这是污点的效果,表示如果Pod没有相应的容忍,则不会被调度到该节点上,如果已经存在,则会被驱逐。[root@k8s-master ~]#  kubectl apply -f pod-toleration.yaml 
pod/pod-toleration created
[root@k8s-master ~]# kubectl get pod pod-toleration -n dev -o wide
NAME             READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
pod-toleration   1/1     Running   0          9s    10.244.36.73   k8s-node1   <none>           <none>

 


http://www.ppmy.cn/news/1564444.html

相关文章

leetcode刷题记录(四十八)——128. 最长连续序列

&#xff08;一&#xff09;问题描述 128. 最长连续序列 - 力扣&#xff08;LeetCode&#xff09;128. 最长连续序列 - 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff09;的长度。请你设计并实现时间复…

WEB攻防-通用漏洞_XSS跨站_绕过修复_http_only_CSP_标签符号

目录 1、关卡361 - 反射型xss 2、关卡317 - 过滤标签 3、关卡318 319 - 过滤标签 4、关卡320--326 - 过滤空格和尖括号 5、关卡327 - 存储型跨站 6、关卡328 7、关卡329 - 失效凭据需1步完成所需操作 8、关卡330 - 存储型-借助修改密码URL重置管理员密码&#xff08;GE…

探索 Transformer²:大语言模型自适应的新突破

目录 一、来源&#xff1a; 论文链接&#xff1a;https://arxiv.org/pdf/2501.06252 代码链接&#xff1a;SakanaAI/self-adaptive-llms 论文发布时间&#xff1a;2025年1月14日 二、论文概述&#xff1a; 图1 Transformer 概述 图2 训练及推理方法概述 图3 基于提示的…

详解深度学习中的Dropout

Dropout是一种在神经网络训练中常用的正则化技术&#xff0c;其操作是在每次训练迭代中随机“丢弃”一部分神经元&#xff08;即将其输出置为零&#xff09;。以下是对这一操作的详细解释&#xff1a; 一、基本思想 Dropout的基本思想是减少神经元之间的复杂共适应关系&#…

Web安全|渗透测试|网络安全

基础入门(P1-P5) p1概念名词 1.1域名 什么是域名&#xff1f; 域名&#xff1a;是由一串用点分隔的名字组成的Internet上某一台计算机或计算机组的名称&#xff0c;用于在数据传输时对计算机的定位标识&#xff08;有时也指地理位置&#xff09;。 什么是二级域名多级域名…

用ChatGPT进行酒店评论情感分析

现在,许多开发人员已经使用并测试过这款聊天机器人来尝试开发他们的代码和AI想法。当然,这款聊天机器人的使用严格取决于你的背景。例如,如果你是一名Web开发人员,你会要求ChatGPT使用HTML构建一个网站。如果您是一名测试人员,您可以请求ChatGPT帮助您查找特定系统中的错误…

【AIGC-ChatGPT进阶提示词指令】心灵修复师:一个基于情感共鸣的智慧对话系统设计

引言 在当今快节奏的生活中&#xff0c;心理健康问题日益凸显。如何借助人工智能技术&#xff0c;构建一个既富有温度又专业可靠的心理支持系统&#xff0c;成为了一个值得深入探讨的课题。本文将详细介绍一个名为"心灵修复师"的对话系统设计&#xff0c;这个系统通…

深入探索 Vue.js 组件开发中的最新技术:Teleport 和 Suspense 的使用

Vue.js 是一款广泛使用的前端框架&#xff0c;凭借其简洁的设计和强大的功能&#xff0c;已经成为了许多开发者首选的框架。随着 Vue 3 的发布&#xff0c;新的特性和改进为开发者提供了更多的选择和灵活性。其中&#xff0c;Teleport 和 Suspense 是 Vue 3 引入的两项非常有趣…