Kubernetes 亲和性

Posted by Vito on January 26, 2024

节点亲和性

  • 亲和性都是根据 label 来匹配的

  • requiredDuringSchedulingIgnoredDuringExecution :硬亲和性,类似于nodeSelector
  • preferredDuringSchedulingIgnoredDuringExecution :软亲和性
  • operatorInNotInExistsDoesNotExistGtLt
  • weight:取值范围 [1,100]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-node-affinity-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: node-affinity-test
  template: 
    metadata:
      labels:
        app: node-affinity-test
    spec:
      affinity: # 亲和性
        nodeAffinity: # 节点亲和性
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 10
            preference:
              matchExpressions:
              - key: label-1
                operator: In
                values:
                - value-1
          - weight: 50
            preference:
              matchExpressions:
              - key: label-2
                operator: In
                values:
                - value-2
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: IfNotPresent
        name: deploy-node-affinity-test-c

Pod 亲和性

  • topologyKey
    • 顾名思义,topology 就是 拓扑 的意思,这里指的是一个 拓扑域,是指一个范围的概念,比如一个 Node、一个机柜、一个机房或者是一个地区(如杭州、上海)等,实际上对应的还是 Node 上的标签。
    • 这里的 topologyKey 对应的是 Node 上的标签的 Key(不关注 Value),可以看出,其实 topologyKey 就是用于筛选 Node 的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-pod-affinity-test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-affinity-test
  template: 
    metadata:
      labels:
        app: pod-affinity-test
    spec:
      affinity: # 亲和性
        podAffinity: # Pod 亲和性
          preferredDuringSchedulingIgnoredDuringExecution: # 软亲和性
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: security # 如果某个节点已存在具有 security=S1 的标签的 Pod 时,尽量将本 Pod 调度到该节点
                  operator: In
                  values:
                  - S1
              topologyKey: gz.fdr.kubernetes.io/zone # 尽量调度到具有 key 为 `gz.fdr.kubernetes.io/zone` 的标签的节点上
        podAntiAffinity:  # Pod 反亲和性
          requiredDuringSchedulingIgnoredDuringExecution: # 硬亲和性
          - labelSelector:
              matchExpressions:
              - key: security # 如果某个节点已存在具有 security=S2 的标签的 Pod 时,一定不要将本 Pod 调度到该节点
                operator: In
                values:
                - S2
            topologyKey: sh.fdr.kubernetes.io/zone # 一定不要调度到具有 key 为 `sh.fdr.kubernetes.io/zone` 的标签的节点上
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: IfNotPresent
        name: deploy-pod-affinity-test-c