kubernetes(k8s)Pod污点与容忍

污点(taints)与容忍(tolerations)

对于nodeAffinity无论是硬策略还是软策略方式,都是调度 pod 到预期节点上,而Taints恰好与之相反,如果一个节点标记为 Taints ,除非 pod 也被标识为可以容忍污点节点,否则该 Taints 节点不会被调度 pod。

比如用户希望把 Master 节点保留给 Kubernetes 系统组件使用,或者把一组具有特殊资源预留给某些 pod,则污点就很有用了,pod 不会再被调度到 taint 标记过的节点。我们使用kubeadm搭建的集群默认就给 master 节点添加了一个污点标记,所以我们看到我们平时的 pod 都没有被调度到 master 上去:

$ kubectl describe node master
Name:               master
Roles:              master
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/hostname=master
                    node-role.kubernetes.io/master=
......
Taints:             node-role.kubernetes.io/master:NoSchedule
Unschedulable:      false
......

我们可以使用上面的命令查看 master 节点的信息,其中有一条关于 Taints 的信息:node-role.kubernetes.io/master:NoSchedule,就表示给 master 节点打了一个污点的标记,其中影响的参数是NoSchedule,表示 pod 不会被调度到标记为 taints 的节点,除了 NoSchedule 外,还有另外两个选项:

PreferNoSchedule:NoSchedule 的软策略版本,表示尽量不调度到污点节点上去
NoExecute:该选项意味着一旦 Taint 生效,如该节点内正在运行的 pod 没有对应 Tolerate 设置,会直接被逐出
污点 taint 标记节点的命令如下:

$ kubectl taint nodes node02 test=node02:NoSchedule
node "node02" tainted

上面的命名将 node02 节点标记为了污点,影响策略是 NoSchedule,只会影响新的 pod 调度,如果仍然希望某个 pod 调度到 taint 节点上,则必须在 Spec 中做出Toleration定义,才能调度到该节点,比如现在我们想要将一个 pod 调度到 master 节点:(taint-demo.yaml)

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: taint
  labels:
    app: taint
spec:
  replicas: 3
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        app: taint
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - name: http
          containerPort: 80
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"

由于 master 节点被标记为了污点节点,所以我们这里要想 pod 能够调度到 master 节点去,就需要增加容忍的声明:

tolerations:
- key: "node-role.kubernetes.io/master"
  operator: "Exists"
  effect: "NoSchedule"

然后创建上面的资源,查看结果:

$ kubectl create -f taint-demo.yaml
deployment.apps "taint" created
$ kubectl get pods -o wide
NAME                                      READY     STATUS             RESTARTS   AGE       IP             NODE
......
taint-845d8bb4fb-57mhm                    1/1       Running            0          1m        10.244.4.247   node02
taint-845d8bb4fb-bbvmp                    1/1       Running            0          1m        10.244.0.33    master
taint-845d8bb4fb-zb78x                    1/1       Running            0          1m        10.244.4.246   node02
......

我们可以看到有一个 pod 副本被调度到了 master 节点,这就是容忍的使用方法。

对于 tolerations 属性的写法,其中pod的  key、value、effect 与 Node 的 Taint 设置需保持一致, 还有以下几点说明:

如果 operator 的值是 Exists,则 value 属性可省略
如果 operator 的值是 Equal,则表示其 key 与 value 之间的关系是 equal(等于)
如果不指定 operator 属性,则默认值为 Equal
另外,还有两个特殊值:

$ kubectl taint nodes node01 key=value:NoSchedule

    tolerations:
    - key: "key"
      operator: "Equal"
      value: "value"
      effect: "NoScheduale"

空的 key(是指key没有指定,而不是指key为空字符串) 如果再配合operator Exists 就能匹配所有的 key 与 value,也是能容忍所有 node 的所有 Taints
空的 effect 匹配所有的 effect


最后,如果我们要取消节点的污点标记,可以使用下面的命令:

$ kubectl taint nodes node02 test-
node "node02" untainted

这就是污点和容忍的使用方法。

原文:https://www.jianshu.com/p/0d08337e5943

原文地址:https://www.cnblogs.com/linyouyi/p/11545211.html