添加标签到节点

  1. kubectl label nodes <node-name> <label-key>=<label-value>

添加节点选择器

  1. apiversion: app/v1
  2. kind: pod
  3. metadata:
  4. name: nginx
  5. label:
  6. env: test
  7. spec:
  8. containers:
  9. - name: nginx
  10. image: nginx
  11. imagePullPolicy: IfNotPresent
  12. nodeSelector:
  13. disktype: ssd

节点间的亲和性

亲和性 : affinity

  1. apiversion: app/v1
  2. kind: pod
  3. metadata:
  4. name: nginx
  5. label:
  6. env: test
  7. spec:
  8. containers:
  9. - name: nginx
  10. image: nginx
  11. imagePullPolicy: IfNotPresent
  12. affinity:
  13. nodeAffinity:
  14. reuiqredDuringSchedulingIgnoreDuringExecution:
  15. nodeSelectorTerms:
  16. - matchExpressions:
  17. - key: key
  18. operator: In
  19. values:
  20. - val1
  21. - val2
  22. preferredDuringSchedulingIgnoredDuringExecution:
  23. - weight: 1 # 权重评分,通过遍历所有的节点计算总分,总分最高获胜
  24. preference:
  25. matchExpressions:
  26. - key: another-node-label-key
  27. operator: In
  28. values:
  29. - another-node-label-value
  • In:label 的值在某个列表中
  • NotIn:label 的值不在某个列表中
  • Gt:label 的值大于某个值
  • Lt:label 的值小于某个值
  • Exists:某个 label 存在
  • DoesNotExist:某个 label 不存在

    pod 间的亲和性和反亲和性

    1. apiVersion: v1
    2. kind: Pod
    3. metadata:
    4. name: with-pod-affinity
    5. labels:
    6. app: pod-affinity-pod
    7. spec:
    8. containers:
    9. - name: with-pod-affinity
    10. image: nginx
    11. affinity:
    12. podAffinity:
    13. requiredDuringSchedulingIgnoredDuringExecution:
    14. - labelSelector:
    15. matchExpressions:
    16. - key: app
    17. operator: In
    18. values:
    19. - busybox-pod
    20. topologyKey: kubernetes.io/hostname
    21. podAntiAffinity:
    22. preferredDuringSchedulingIgnoredDuringExecution:
    23. - weight: 1
    24. podAffinityTerm:
    25. labelSelector:
    26. matchExpressions:
    27. - key: app
    28. operator: In
    29. values:
    30. - node-affinity-pod
    31. topologyKey: kubernetes.io/hostname
    第一个 pod 最好调度某个指定的主机上,上面至少一个节点运行这样的 pod: app=busybox-pod
    第二个则是最好不要调度到这样的节点: app=node-affinity-pod