当前位置:   article > 正文

Kubernetes-Taint (污点)和 Toleration(容忍)

Kubernetes-Taint (污点)和 Toleration(容忍)

目录

一、Taint(污点)

1.污点的组成

2.污点的设置、查看和去除

3.污点实验:

二、Toleration(容忍)

1.容忍设置的方案

2.容忍实验:


       Taint 和 toleration 相互配合,可以用来避免 pod 被分配到不合适的节点上。每个节点上都可以应用一个或多个 taint ,这表示对于那些不能容忍这些 taint 的 pod,是不会被该节点接受的。如果将 toleration 应用于 pod 上,则表示这些 pod 可以(但不要求)被调度到具有匹配 taint 的节点上。

一、Taint(污点)

       使用 `kubectl taint` 命令可以给某个 Node 节点设置污点,Node 被设置上污点之后就和 Pod 之间存在了一种相斥的关系,可以让 Node 拒绝 Pod 的调度执行,甚至将 Node 已经存在的 Pod 驱逐出去

1.污点的组成

         key=value:effect

         每个污点有一个 key 和 value 作为污点的标签,其中 value 可以为空,effect 描述污点的作用。当前 taint effect 支持如下三个选项:

  1. NoSchedule:表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上
  2. PreferNoSchedule:表示 k8s 将尽量避免将 Pod 调度到具有该污点的 Node 上
  3. NoExecute:表示 k8s 将不会将 Pod 调度到具有该污点的 Node 上,同时会将 Node 上已经存在的 Pod 驱逐出去。

2.污点的设置、查看和去除

  1. kubectl taint nodes node1 key1=value1:NoSchedule
  2. # 设置污点
  3. kubectl describe node node-name
  4. # 节点说明中,查找 Taints 字段
  5. kubectl taint nodes node1 key1:NoSchedule-
  6. # 去除污点

3.污点实验:

  1. kubectl describe node k8s-master01
  2. #主节点自带污点,所以master节点不能被调度

  1. kubectl taint node k8s-master01 node-role.kubernetes.io/master=:NoSchedule-
  2. #取消master节点的污点
  3. kubectl describe node k8s-master01
  4. #查看污点已被删除
  5. kubectl create deployment taint_deploy --image=nginx:latest
  6. #创建名为taint_deploy的deployment,使用nginx:latest作为pod内容器创建的镜像。
  7. kubectl scale deployment taint_deploy --replicas=20
  8. #设置刚刚创建的deployment的pod副本数为20
  9. kubectl get pod -o wide
  10. #观察刚刚创建的pod,因为取消了master节点的污点,所以pod可以被调度到master节点运行了
  11. #master节点没有污点的情况下,运行daemonset,master节点也会运行一个daemonset。

二、Toleration(容忍)

       设置了污点的 Node 将根据 taint 的 effect:NoSchedule、PreferNoSchedule、NoExecute 和 Pod 之间产生互斥的关系,Pod 将在一定程度上不会被调度到 Node 上。 但我们可以在 Pod 上设置容忍 ( Toleration ) ,意思是设置了容忍的 Pod 将可以容忍污点的存在,可以被调度到存在污点的 Node 上。

pod.spec.tolerations

1.容忍设置的方案

  1. tolerations: #容忍
  2. 第一种方案:
  3. - key: "key1" #指定污点的key1
  4. operator: "Equal" #运算符等于
  5. value: "value1" #value是污点value1
  6. effect: "NoSchedule" #策略是NoSchedule
  7. #这种写法的含义是key,value,effect必须全部匹配,有一个不一样都匹配不到。
  8. 第二种方案:
  9. - key: "key1"
  10. operator: "Equal"
  11. value: "value1"
  12. effect: "NoExecute"
  13. tolerationSeconds: 3600 #容忍3600秒
  14. 第三种方案:
  15. - key: "key2" #只要匹配到key和dffect,value是什么无所谓。
  16. operator: "Exists"
  17. effect: "NoSchedule"

例:

  1. Ⅰ、当不指定 key 值时,表示容忍所有的污点 key:**
  2. tolerations:
  3. - operator: "Exists" #只写一个存在,只要有污点,就能容忍
  4. Ⅱ、当不指定 effect 值时,表示容忍所有的污点作用**
  5. tolerations:
  6. - key: "key"
  7. operator: "Exists" #只要key匹配到了,其他的无所谓
  8. Ⅲ、有多个 Master 存在时,防止资源浪费,可以如下设置
  9. kubectl taint nodes Node-Name node-role.kubernetes.io/master=:PreferNoSchedule

2.容忍实验:

  1. kubectl taint node k8s-master01 node-role.kibernetes.io/master=:NoSchedule
  2. #将主节点的污点恢复回来
  3. kubectl get daemonset -n kube-system
  4. #查看有哪些daemonset资源
  5. kubectl get daemonset -n kube-system calico-node -o yaml
  6. #查看calico的容忍
  7. #策略NoSchedule,运算符Exists(存在),key和value无所谓
  8. #value和策略无所谓
  9. #还能容忍NoExecute策略的,value和key无所谓

  1. kubectl describe node k8s-master01
  2. #查询master节点的key
  3. vim tolerations_daemonset.yaml
  4. apiVersion: apps/v1
  5. kind: DaemonSet
  6. metadata:
  7. name: tolerations-daemonset
  8. labels:
  9. app: daemonset
  10. spec:
  11. selector:
  12. matchLabels:
  13. name: tolerations-daemonset-pod
  14. template:
  15. metadata:
  16. labels:
  17. name: tolerations-daemonset-pod
  18. spec:
  19. tolerations:
  20. - key: "node-role.kubernetes.io/master" #master节点的污点的key
  21. operator: "Exists" #匹配规则为exists,存在key就容忍。
  22. containers:
  23. - name: tolerations-daemonset-pod
  24. image: nginx:latest
  25. imagePullPolicy: IfNotPresent
  26. kubectl apply -f tolerations_daemonset.yaml
  27. kubectl get daemonset
  28. #查看创建的daemonset
  29. kubectl get pod -o wide
  30. #查看daemonset的pod是否调度在master上。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42408
推荐阅读
相关标签
  

闽ICP备14008679号