当前位置:  首页>> 技术小册>> Kubernets合辑5-Pod控制器

节点亲和性分为强制选择(硬亲和)和优先选择(软亲和):

  • 强制选择requiredDuringSchedulingIgnoredDuringExecution
    • 满足则调度(案例一)
    • 不满足则Pending(案例二)
  • 优先选择preferredDuringSchedulingIgnoredDuringExecution
    • 不满足则调度到其它节点(案例三)
    • 满足则优先调度到该节点,并不是一定调度到该节点,而且该节点优先级较高(案例四)

案例一

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. spec:
  6. replicas: 5
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx-demo
  17. image: linuxmaxiaoke/nginx:v1.0.0
  18. affinity:
  19. nodeAffinity:
  20. requiredDuringSchedulingIgnoredDuringExecution:
  21. nodeSelectorTerms:
  22. - matchExpressions:
  23. - key: ssd
  24. operator: DoesNotExist
  25. - key: cpu
  26. operator: In
  27. values: ["high"]
  1. [root@maxiaoke ~]# kubectl get pod -o wide # 不存在ssd标签,并且cpu值为 high
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-deploy-6f8b6d748c-4pt2x 1/1 Running 0 8s 172.16.5.20 centos-7-56 <none> <none>
  4. nginx-deploy-6f8b6d748c-m9kb4 1/1 Running 0 8s 172.16.5.19 centos-7-56 <none> <none>
  5. nginx-deploy-6f8b6d748c-st8mw 1/1 Running 0 5s 172.16.5.22 centos-7-56 <none> <none>
  6. nginx-deploy-6f8b6d748c-w4mc9 1/1 Running 0 5s 172.16.5.21 centos-7-56 <none> <none>
  7. nginx-deploy-6f8b6d748c-wjvxx 1/1 Running 0 8s 172.16.5.18 centos-7-56 <none> <none>

案例二

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. spec:
  6. replicas: 5
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx-demo
  17. image: linuxmaxiaoke/nginx:v1.0.0
  18. affinity:
  19. nodeAffinity:
  20. requiredDuringSchedulingIgnoredDuringExecution:
  21. nodeSelectorTerms:
  22. - matchExpressions:
  23. - key: ssd
  24. operator: DoesNotExist
  25. # 此处改为cpu不等于high
  26. - key: cpu
  27. operator: NotIn
  28. values: ["high"]
  1. [root@maxiaoke ~]# kubectl describe pod nginx-deploy-746f88c86-96dbp # 注意message
  2. Name: nginx-deploy-746f88c86-96dbp
  3. Namespace: default
  4. Priority: 0
  5. Node: <none>
  6. ......
  7. Status: Pending
  8. ......
  9. Events:
  10. Type Reason Age From Message
  11. ---- ------ ---- ---- -------
  12. Warning FailedScheduling 30s (x3 over 32s) default-scheduler 0/6 nodes are available: 3 node(s) didn't match node selector, 3 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.

案例三

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. spec:
  6. replicas: 5
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx-demo
  17. image: linuxmaxiaoke/nginx:v1.0.0
  18. affinity:
  19. nodeAffinity:
  20. preferredDuringSchedulingIgnoredDuringExecution:
  21. - preference:
  22. matchExpressions:
  23. # 系统中并没有这个的Node
  24. - key: ssd
  25. operator: DoesNotExist
  26. - key: cpu
  27. operator: NotIn
  28. values: ["high"]
  29. weight: 5

案例四

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deploy
  5. spec:
  6. replicas: 5
  7. selector:
  8. matchLabels:
  9. app: nginx
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx-demo
  17. image: linuxmaxiaoke/nginx:v1.0.0
  18. affinity:
  19. nodeAffinity:
  20. preferredDuringSchedulingIgnoredDuringExecution:
  21. # 满足该条件的节点为 centos-7-56,当并不是一把梭全部压在该节点,而且该节点的优先级更高,通过weight可以增加优先级
  22. - preference:
  23. matchExpressions:
  24. - key: ssd
  25. operator: DoesNotExist
  26. - key: cpu
  27. operator: In
  28. values:
  29. - high
  30. weight: 10
  1. [root@maxiaoke local-k8s-yaml]# kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. nginx-deploy-5996df99f8-4cmfv 1/1 Running 0 7s 172.16.4.4 centos-7-55 <none> <none>
  4. nginx-deploy-5996df99f8-77k5l 1/1 Running 0 7s 172.16.5.38 centos-7-56 <none> <none>
  5. nginx-deploy-5996df99f8-8kxvc 1/1 Running 0 7s 172.16.5.37 centos-7-56 <none> <none>
  6. nginx-deploy-5996df99f8-t55hj 1/1 Running 0 7s 172.16.3.161 centos-7-54 <none> <none>
  7. nginx-deploy-5996df99f8-zbpf2 1/1 Running 0 7s 172.16.5.39 centos-7-56 <none> <none>

该分类下的相关小册推荐: