k8s 亲和性调度

目录
警告
本文最后更新于 2020-12-29 11:57,文中内容可能已过时。

node 亲和性 (nodeAffinity):选择调度到相同或不同的 node 节点

pod 亲和性 (podAffinity):和某些 pod 调度到同一节点

pod 反亲和性 (podAntiAffinity):和某些 pod 调度在不同节点

指定了将 pod 调度到一个节点上必须满足的规则,不满足则会处于 pending 状态,一直进行重试,直到满足为止。

优先满足指定的规则,如果不满足则会调度到其他节点上。如果有多个软策略还可以设置优先级 (weight)

使用节点亲和性硬策略将 pod 调度到 kubernetes.io/hostname 标签不是 test-k8s-node2 的节点上,软策略将 pod 尽量调度到 test-k8s-node3 节点上

 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
kind: Deployment
apiVersion: apps/v1
metadata:
  name: affinity
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-affinity
  template:
    metadata:
      labels:
        app: nginx-affinity
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: web
          containerPort: 80
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: NotIn
                values:
                - test-k8s-node2
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1 
              preference: 
                matchExpressions:
                - key: kubernetes.io/hostname
                  operator: In
                  values:
                  - test-k8s-node3
> operator 可选操作:`In`,`NotIn`,`Exists`,`DoesNotExist`,`Gt`,`Lt`

我这里的区域标记是:

1
2
3
4
kubectl label nodes test-k8s-node1 zone=1
kubectl label nodes test-k8s-node3 zone=1
kubectl label nodes test-k8s-node2 zone=2
kubectl label nodes test-k8s-node4 zone=2

首先找到 pod 中包含 app=nginx 节点,查看 zone 标签的值 (topologyKey 的作用),假如 zone=2,那么该 pod 会部署在 zone=2 的区域中。

 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
kind: Deployment
apiVersion: apps/v1
metadata:
  name: affinity
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-affinity
  template:
    metadata:
      labels:
        app: nginx-affinity
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: web
          containerPort: 80
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: zone

直接部署此文件:

均处于 pending 状态,

创建一个标签为 app=nginx 的 pod

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1
kind: Pod
metadata:
  name: test-nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - name: http
      containerPort: 80

可以看到这个 pod 调度到了 test-k8s-node1 节点上,node1 节点的 zone 值为 1,所以刚才的 pod 会调度到 zone=1 的节点上

当前 app=nginx 的 pod 被调度在 node4 节点上

创建反亲和性的 pod

 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
kind: Deployment
apiVersion: apps/v1
metadata:
  name: affinity
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx-affinity
  template:
    metadata:
      labels:
        app: nginx-affinity
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: web
          containerPort: 80
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - topologyKey: kubernetes.io/hostname
            labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx

可以看到所有 pod 都不会被调度到 node4 节点上了

请我喝杯水
SoulChild 微信号 微信号
SoulChild 微信打赏 微信打赏
0%