1.前言

污点是给node节点打上污点标签,使得pod不能往该node节点上调度,污点有三种模式,分别是NoSchedule、PreferNoSchedule、NoExecute,容忍是给pod打上和node节点一样的污点标签,使pod能调度到带有该污点标签的node节点上

2.污点

NoSchedule:禁止所有pod往该node节点上调度,原本已经在节点上的pod没有影响

PreferNoSchedule:尽量不将pod调度到该节点上,但还是可以接受pod的调度,但是pod的调度一般都会优先调度到没有污点的node上

NoExecute:禁止所有pod往该node节点上调度,原本已经在节点上运行的pod也将被驱逐出去

接下来使用一下以上的三个污点模式,一个节点可以被设置多个污点

kubectl taint node 节点名称 key=value:effect

kubectl taint node k8s-node01 web=true:NoSchedule

kubectl taint node k8s-node02 app=true:PreferNoSchedule

kubectl taint node k8s-node01 web=true:NoExecute

查看节点中的污点

kubectl describe node k8s-node02

k8s 定时RemoveContainer k8s schedulingdisabled_配置项

接下来说一下去除污点

删除特定的一个污点

kubectl taint node k8s-node01 web=true:NoSchedule-

也可以不指定value去删除

kubectl taint node k8s-node02 app:PreferNoSchedule-

删除关于该key的所有污点

kubectl taint node k8s-node01 web-

 3.容忍

如果一个节点有多个污点,则pod必须包含该节点上的所有污点标签才可以被调度到该节点上,当然了PreferNoSchedule的污点标签是不影响的,接下来展示一下容忍的使用

新建一个yaml文件

vi deployment-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata: 
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 3
  progressDeadlineSeconds: 600
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec: 
      containers:
      - name: nginx
        image: nginx:1.21
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 50Mi
            cpu: 0.5
          limits:
            memory: 100Mi
            cpu: 1

这个yaml文件中没有设置容忍,接下来给node节点打上污点

kubectl taint node k8s-node01 web=true:NoSchedule

kubectl taint node k8s-node02 key1=value1:NoExecute

给两个node节点都打上了不可调度的污点,现在来执行一下yaml文件看一下效果

kubectl apply -f deployment-nginx.yaml

查看一下pod的状态

kubectl get pod -o wide -n default -l app=nginx

k8s 定时RemoveContainer k8s schedulingdisabled_配置项_02

可以看到所有三个pod都是pending状态,node列也显示是none,说明没有适合的node给pod调度

现在更改一下yaml文件,给pod加上k8s-node01的容忍策略

vi deployment-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata: 
  labels:
    app: nginx
  name: nginx
  namespace: default
spec:
  replicas: 3
  progressDeadlineSeconds: 600
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec: 
      containers:
      - name: nginx
        image: nginx:1.21
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 50Mi
            cpu: 0.5
          limits:
            memory: 100Mi
            cpu: 1
      tolerations:   #配置容忍策略
      - key: web      #k8s-node01中配置的污点key
        operator: Equal  #Equal为等于,即key=value,使用Equal为精确匹配,只有当节点上 key 的值等于 value 时,才可以容忍这个 Taint,还可以使用Exists模糊匹配,使用Exists不用配置value值,只要节点上存在对应的 key,就可以容忍这个 Taint
        value: "true"  #k8s-node01中配置的污点value
        effect: NoSchedule   #k8s-node01中配置的污点effect

重新加载一下 yaml文件配置

kubectl apply -f deployment-nginx.yaml

查看一下pod的状态

kubectl get pod -o wide -n default -l app=nginx

k8s 定时RemoveContainer k8s schedulingdisabled_nginx_03

 可以看到增加了k8s-node01污点的容忍策略后,pod都调度到了k8s-node01节点上

容忍策略以上四个常用的配置项外,还有一个配置项tolerationSeconds,此配置项在污点模式为NoExecute时使用,tolerationSeconds 的作用是为了在节点的 Taint 被移除之前,给予一定的时间让 Pod 在该节点上运行。这对于一些需要在节点上运行一段时间的任务非常有用,例如数据迁移、数据备份等