节点亲和性
pod.spec.nodeAffinity
- preferredDuringSchedulingIgnoredDuringExecution:软策略 (百度翻译:优先执行计划)
- requiredDuringSchedulingIgnoredDuringExecution:硬策略(百度翻译:执行期间要求的预定计划)
老师讲解:
- 软策略:有更优的节点就选择更优的节点
- 硬策略:必须选择我指定的将节点
requiredDuringSchedulingIgnoredDuringExecution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| apiVersion: v1 kind: Pod metadata: name: affinity labels: app: node-affinity-pod spec: containers: - name: with-node-affinity image: hub.test.com/library/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: NotIn values: - k8s-node02
|
键值运算关系
- In:label的值在某个列表中
- NotIn:label的值不在某个列表中
- Gt:label的值大于某个值
- Lt:label的值小于某个值
- Exists:某个label存在
- DoesNotExist:某个label不存在
1 2 3 4 5 6
| kubectl create -f pod.yaml
[root@k8s-master affinity] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES affinity-pod 1/1 Running 0 45s 10.244.1.117 k8s-node01 <none> <none>
|
PreferredDuringSchedulingIgnoredDuringExectuion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| apiVersion: v1 kind: Pod metadata: name: affinity-preferred labels: app: node-affinity-pod spec: containers: - name: with-node-affinity image: hub.test.com/library/myapp:v1 affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 preference: matchExpressions: - key: kubernetes.io/hostname operator: In values: - k8s-node03
|
1 2 3 4 5 6 7
| [root@k8s-master affinity] pod/affinity-preferred created [root@k8s-master affinity] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES affinity-pod 1/1 Running 0 19m 10.244.1.117 k8s-node01 <none> <none> affinity-preferred 1/1 Running 0 7s 10.244.2.208 k8s-node02 <none> <none>
|
同时使用软策略和硬策略
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
| apiVersion: v kind: Pod metadata: name: faainity-multi app: node-affinity-pod spec: containers: - name: with-node-affinity image: hub.test.com/library/myapp:v1 affinity: nodeAffinity: requiredDuringSchedulingIgnoreDuringExecution: nodeSelectorTerms: - matchExpressions: - key: kubernetes.io/hostname operator: NotIn values: - k8s-node02 preferredDuringSchedulingIgnoreDuringExecution: - weight: 1 preference: matchExpressions: - key: source operator: In - qikaiak
|
Pod亲和性
Pod.spec.affinity.podAffinity/podAntiAffinity
亲和性/反亲和性
preferredDuringSchedulingIgnoredDuringExection:软策略
requiredDruingSchedulingIgnoredDuringExecution:硬策略
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
| apiVersion: v1 kind: Pod metadata: name: pod-3 labels: app: pod-3 spec: containers: - name: pod-3 image: hub.test.com/library/myapp:v1 affinity: podAffinity: requiredDuringSchdeulingIgnoredDuringExecution: - labelSelector: matchExpressions: - key: app operator: In values: - pod-1 topologyKey: kubernetes.io/hostname podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 1 podAffinityTerm: labelSelector: matchExpressions: - key: app operator: In values: - pod-2 topologyKey: kubernetes.io/hostname
|
这里的podAffinityTerm和nodeSelectorTerms,一个有s,一个没有
1 2 3 4 5 6 7 8 9 10 11 12
| [root@k8s-master affinity] pod/affinity-pod-3 created [root@k8s-master affinity] NAME READY STATUS RESTARTS AGE affinity-pod 1/1 Running 0 115m affinity-pod-3 0/1 Pending 0 5s affinity-preferred 1/1 Running 0 96m [root@k8s-master affinity] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS affinity-pod 1/1 Running 0 116m 10.244.1.117 k8s-node01 <none> <none> app=node-affinity-pod affinity-pod-3 0/1 Pending 0 18s <none> <none> <none> <none> <none> affinity-preferred 1/1 Running 0 96m 10.244.2.208 k8s-node02 <none> <none> <none>
|
可以看到pod-3是pending状态
修改其中一个pod的标签app=pod-1
1 2 3 4 5 6 7
| [root@k8s-master affinity] pod/affinity-pod labeled [root@k8s-master affinity] NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS affinity-pod 1/1 Running 0 136m 10.244.1.117 k8s-node01 <none> <none> app=pod-1 affinity-pod-3 1/1 Running 0 21m 10.244.1.118 k8s-node01 <none> <none> <none> affinity-preferred 1/1 Running 0 117m 10.244.2.208 k8s-node02 <none> <none> <none>
|
此时pod-3的状态就变成Running状态
这里的含义就是:判断在kubernetes.io/hostname
下,app必须包含pod-1,可选不包含pod-2
亲和性/反亲和性调度策略比较如下:
