k8s亲和与容忍

操作符介绍

对于 label 的匹配规则,可选的操作符有

操作符规则说明
Inlabel 的值在某个列表中
NotInlabel 的值不在某个列表中
Exists某个 label 存在
DoesNotExist某个 label 不存在
Gtlabel 的值大于某个值(字符串比较)
Ltlabel 的值小于某个值(字符串比较)

节点亲和

  • 软策略
1
2
3
4
5
6
7
8
9
10
11
spec:
template:
spec:
affinity:
nodeAffinity: #节点亲和
preferredDuringSchedulingIgnoredDuringExecution: #软策略
- preference:
matchExpressions: #匹配条件
# 代表倾向于kubernetes.io/hostname=node03的节点(但由于是软策略,不一定就在node03上)
- {key: kubernetes.io/hostname, operator: In, values: ["k8s-master"]}
weight: 50 #如果有多个软策略选项的话,权重越大,优先级越高
  • 硬策略
1
2
3
4
5
6
7
8
9
10
spec:
template:
spec:
affinity:
nodeAffinity: #节点亲和
requiredDuringSchedulingIgnoredDuringExecution: #硬策略
nodeSelectorTerms: #node节点标签选择
- matchExpressions: #匹配规则
# 设置Pod安装到kubernetes.io/hostname的标签值不在values列表中的node上
- {key: kubernetes.io/hostname, operator: NotIn, values: ["node02"]}

POD亲和

  • 硬策略
1
2
3
4
5
6
7
8
9
10
spec:
template:
spec:
affinity:
podAffinity: # 作用域:Pod和Pod之间
requiredDuringSchedulingIgnoredDuringExecution: # Pod亲和性-硬策略
- labelSelector:
matchExpressions:
- {key: app, operator: In, values: ["myapp"]}
topologyKey: kubernetes.io/hostname
  • 软策略
1
2
3
4
5
6
7
8
9
10
11
12
spec:
template:
spec:
affinity:
podAntiAffinity: # 作用域:Pod和Pod之间
preferredDuringSchedulingIgnoredDuringExecution: # Pod非亲和性-软策略
- weight: 50
podAffinityTerm:
labelSelector:
matchExpressions:
- {key: app, operator: NotIn, values: ["myapp"]}
topologyKey: kubernetes.io/hostname

容忍Taint

设置了污点的Node将根据taint的effect:``NoSchedulePreferNoScheduleNoExecutePod之间产生互斥的关系,Pod将在一定程度上不会被调度到Node上。 但我们可以在Pod上设置容忍(Toleration),意思是设置了容忍的Pod将可以容忍污点的存在,可以被调度到存在污点的Node上

Toleration 声明中的 key 和 effect 需要与 Taint 的设置保持一致,并且满足以下条件之一:

  • 1.operator 的值为 Exists,这时无需指定 value
  • 2.operator 的值为 Equal ,这时需要指定 value,并且 value 相等
  • 3.如果不指定 operator,则默认值为 Equal

另外还有如下两个特例

  • 空的 key 配合 Exists 操作符能够匹配所有的键和值,
    • 当不指定key值时,表示容忍所有的污点key,如{effect: NoSchedule, operator: Exists}
  • 空的 effect 匹配所有的 effect
    • 当不指定effect值时,表示容忍所有的污点作用,{key: node-role.kubernetes.io/master, operator: Exists}
    • 当key和effect都不指定是,表示容忍所有的污点key和所有的污点作用,{operator: Exists}

配置示例

1
2
3
4
5
spec:
template:
spec:
tolerations:
- {effect: NoSchedule, key: node-role.kubernetes.io/master, operator: Exists}
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2018-2024 Outsrkem
  • 访问人数: | 浏览次数:

      请我喝杯咖啡吧~

      支付宝
      微信