概念

污点(Taint)使节点能够排斥/驱逐一类特定的 Pod,通过给 Node 打一些污点,来限制 Pod 调度到某些 Node 上。
容忍度(Toleration) 是应用于 Pod等资源上的,容忍度允许调度器调度带有对应容忍度的 Pod到带有污点的Node上。

taints 内容包括 key、value、effect:

  • key 就是配置的键值
  • value 就是内容
  • effect 是标记了这个 taints 行为是什么

目前 Kubernetes 里面有三个 taints 行为:

  • NoSchedule: 禁止新的 Pod 调度上来;
  • PreferNoSchedul: 尽量不调度到这个节点上;
  • NoExecute 会驱逐没有对应toleration的Pods,并且也不会调度新的上来。这个策略是非常严格的,大家在使用的时候要小心一点。

模版

污点使用kubectl命令即可,容忍写到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"
    value: "value1"
    effect: "NoSchedule"
    tolerationSeconds: 60

operator 的默认值是 Equal。
一个容忍度和一个污点相“匹配”是指它们有一样的键名和效果,并且:

  • 如果 operator 是 Exists (此时容忍度不能指定 value)
  • 如果 operator 是 Equal ,则它们的 value 应该相等

toletationSeconds是容忍的时间,默认是永久,就是不驱逐。可以写上,一般和NoExecute搭配,可以在有污点的node上存在一会儿再被驱逐,单位是秒

实战

添加污点

给某节点xxx上添加污点 ,key为master,value为system,effect是NoSchedule

命令

kubectl taint node xxx master=system:NoSchedule

在这里插入图片描述
验证

在xxx节点上创建pod
busybox-tainttest.yaml

apiVersion: v1
kind: Pod
metadata:
  name: busybox-tainttest
spec:
  nodeSelector:
    kubernetes.io/hostname: xxx
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ['sh','-c','sleep 3600']
    ports:
    - containerPort: 80

命令

kubectl create -f busybox-tainttest.yaml
kubectl describe po busybox-tainttest

结果
在这里插入图片描述
会看到该Pod处于Pending状态,describe时显示该pod不能容忍污点master:system,部分显示结果如上图,

添加容忍

apiVersion: v1
kind: Pod
metadata:
  name: busybox-tainttest
spec:
  tolerations:
    - key: master
      value: system
      effect: NoSchedule
  nodeSelector:
    kubernetes.io/hostname: xxx
  containers:
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ['sh','-c','sleep 3600']
    ports:
    - containerPort: 80

命令

kubectl apply -f  busybox-tainttest.yaml

结果
在这里插入图片描述
可以看到,已经可以调度到这个节点了

移除污点

命令

kubectl taint node xxx master=NoSchedule-

结果
在这里插入图片描述
可以看到该节点没有污点了

参考

k8s-污点和容忍
云原生技术公开课 - k8s调度
k8s学习-kubectl命令常用选项详解与实战

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐