Kubernetes 污点和容忍


一、前言

1. 污点(Taints)和容忍(Tolerations)的概述

节点的亲和力和pod的亲和力主要通过标签和拓扑域将pod调度到集群节点上,而污点(taints)却恰恰相反,集群中存在污点的节点将会排斥pod,pod将不会调度到具有污点的节点上。
容忍(tolerations)应用于pod当中,允许(但不强制要求)pod调度到具有污点的节点上。
在k8s默认集群中,pod是不可以调度到master节点上的,因为master节点上存在污点

2. 污点(Taints)的组成

节点添加污点的格式

key=value:effect
effect包含了三个值:
1.NoSchedule:不会将pod调度到具有该污点的节点上
2.PreferNoSchedule:尽量避免把pod调度到具有该污点的节点上
3.NoExecute:不会将pod调度到具有该污点的节点上,同时也会将正在运行的进行pod驱逐

二、NoExecute的应用

NoSchedule、PreferNoSchedule的应用差不多,这里只演示NoExecute
NoExecute针对已经运行在节点上的pod

  1. 如果pod不能够容忍effect的值为NoExecute,那么pod将马上驱逐
  2. 如果pod能够容忍effect的值为NoExecute,且没有定义tolerationSeconds,那么pod将会一直在该节点上运行
  3. 如果pod能够容忍effect的值为NoExecute ,但是在toleration定义中指定了tolerationSeconds,那么pod还能够在节点上的时间就是该选项设置的值
为node节点添加污点
[root@master ~]# kubectl taint node node1 key1=value:NoSchedule
[root@master ~]# kubectl taint node node1 key2=value:NoExecute

污点查看
[root@master ~]# kubectl describe node node1 | grep "Taints" -A 5
Taints:             key1=value:NoSchedule

[root@master ~]# kubectl describe node node2 | grep "Taints" -A 5 
Taints:             key2=value:NoExecute

创建一个deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-taint
  labels:
    app: deploy-taint
spec:
  replicas: 6
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-pod
        image: nginx:latest 
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 80
      tolerations:  #容忍度配置
      - key: "key2" #匹配键名为key2的污点
        operator: "Equal" #相当于等于
        value: "value"
        effect: "NoExecute" #允许容忍该类型的污点
        tolerationSeconds: 30 #表示可以在该节点运行30秒
综上所写,pod最终会被调度到node2节点

字段名称的概述
key、value、effect和我们定义污点时写的一样
operator:Equal(等于)、Exists(通配符作用)

Exists的应用
当key值和effect都不指定的时,operator为Exists,表示容忍所有的污点(key、value、effect)
tolerations:
- operator: “Exists”

当不指定effect值时,表示能匹配污点key对应的所有effect情况
tolerations:
- key: “key”
  operator:  “Exists”

三、多污点多容忍的应用

我们可以为node节点设置多个污点,也可以为pod设置多个容忍,k8s处理处理多个污点和容忍的过程就像一个过滤器,在节点定义的污点和在pod容忍里面定义的污点两者相匹配,匹配到相同的污点则过滤掉,未被过滤的就留下作为pod调度到节点的依据,通常有以下三种情况
1.当未被过滤的污点值为NoSchedule时,不会将pod分配到该节点
2.当未被过滤的污点值为PreferNoSchedule时,尽量不要调度到该节点,存在没地方调用在调用到该节点
3.当未被过滤的污点值为NoExecute时,不会将pod分配到该节点(pod还未在节点上运行),如果运行了则从该节点驱逐

为node节点添加污点(上面添加的污点保留)
[root@master ~]# kubectl taint node node1 type=node1:NoExecute
[root@master ~]# kubectl taint node node2 type=node2:PreferNoSchedule

查看污点
[root@master ~]# kubectl describe node node1 | grep "Taints" -A 5    
Taints:             type=node1:NoExecute
                    key1=value:NoSchedule

[root@master ~]# kubectl describe node node2 | grep "Taints" -A 5    
Taints:             key2=value:NoExecute
                    type=node2:PreferNoSchedule

编写一个deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-taint
  labels:
    app: deploy-taint
spec:
  replicas: 6
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-pod
        image: nginx:latest 
        imagePullPolicy: IfNotPresent
        ports:
          - containerPort: 80
      tolerations:
      - key: "key2"  #容忍键名为key2的污点
        operator: "Equal"
        value: "value"
        effect: "NoExecute"  #允许容忍该类型的污点
      - key: "type"  #容忍键名为type的污点
        operator: "Exists" #容忍键名为type的污点,且effect为任何类型

综上所写,pod最终会被调度node2节点
pod第一个污点匹配node2节点
pod第二个污点同时匹配了node1和node2
node1额外多出了一个污点key1=value:NoSchedule表示不会将pod调度到该节点上,所以pod不会被调度到该节点上
只有node2匹配成功,所有最终会被调度到node2节点
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ball-4444

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值