kubernetes之Taints污点和Tolerations容忍

介绍说明

nodeaffinity节点亲和性是pod上定义的一种属性, 使得pod能够被调度到某些node上运行,
taint污点正好相反, 它让node拒绝pod运行,
除非pod明确声明能够容忍这些污点,否则无法再这些node上运行, tolerations容忍是pod属性
污点(Taint):给某个Node节点设置污点,Node被设置上污点之后就和Pod之间存在了一种相斥的关系,可以让Node拒绝Pod的调度执行,甚至将Node已经存在的Pod驱逐出去。
容忍(Tolerations) :可以在Pod上设置容忍(Toleration),意思是设置了容忍的Pod将可以容忍污点的存在,可以被调度到存在污点的Node上。

污点

1.给node节点设置污点

kubectl taint nodes <node-name>  key=value:effect

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

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

3.设置与去除污点

# 设置污点
kubectl taint nodes node1 key1=value1:NoSchedule

# 去除污点
kubectl taint nodes node1 key1:NoSchedule-

容忍

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

tolerations:
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoSchedule"
  tolerationSeconds: 3600
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoExecute"
- key: "key2"
  operator: "Exists"
  effect: "NoSchedule"

其中key, vaule, effect要与Node上设置的taint保持一致
operator的值为Exists将会忽略value值, 如果是Equal需要value相等
tolerationSeconds用于描述当Pod需要被驱逐时可以在Pod上继续保留运行的时间

特例: 空的key配合Exists操作符能够匹配所有的键和值
空的effect匹配所有的effect

常见用例

1.对具有特殊资源的节点打上污点
给指定的节点设置污点

kubectl taint nodes 192.168.1.82  keytest=valuetest:NoSchedule

给pod设置容忍, 让pod可以调度到该节点上


apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-test2
spec:
  selector:
    matchLabels:
      app: nginx-test2
  replicas: 4
  template:
    metadata:
      labels:
        app: nginx-test2
        security: s1
    spec:
      containers:
      - name: nginx
        image: nginx:1.11
        ports:
        - containerPort: 80
      tolerations:
      - key: "keytest"
        operator: "Equal"
        value: "valuetest"
        effect: "NoSchedule"

猜你喜欢

转载自www.cnblogs.com/lovelinux199075/p/11295711.html