亲和性分为NODE亲和性和POD亲和性

  • requiredDuringSchedulingIgnoredDuringExecution 必须满足的条件
  • preferredDuringSchedulingIgnoredDuringExecution 尽量满足的条件

Node亲和性

决定了一个POD能在哪些NODE节点上运行,取决于Node节点设置的标签

  1. spec:
  2. affinity:
  3. nodeAffinity:
  4. preferredDuringSchedulingIgnoredDuringExecution:
  5. - preference: {}
  6. weight: 100
  7. requiredDuringSchedulingIgnoredDuringExecution:
  8. nodeSelectorTerms:
  9. - matchExpressions:
  10. - key: SC
  11. operator: In
  12. values:
  13. - '1'

这里的含义就是这个POD必须运行在一个包含Key为SC并且Value为1的Node节点上.
反过来说就是如果一个Node节点的存在一个Key为SC并且Value为1的标签,那么这个node就能够跑POD.

Pod亲和性

相比较于Node的亲和性只能选择固定的节点,Pod的亲和性是针对运行在node节点上的pod来决定的.

  1. spec:
  2. affinity:
  3. podAntiAffinity:
  4. preferredDuringSchedulingIgnoredDuringExecution:
  5. - podAffinityTerm:
  6. labelSelector:
  7. matchExpressions:
  8. - key: app
  9. operator: In
  10. values:
  11. - nginx
  12. topologyKey: kubernetes.io/hostname
  13. weight: 100

这里(In)的含义是如果一个node上存在一个 app=nginx 标签的POD,那么后续的POD尽量不要放在这个节点.
NotIn反之:如果一个node上存在app=nginx 标签的POD,那么后续的POD尽量放在这个节点

topologyKey

拓扑域

附录

  • nginx#YAML文件
    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. labels:
    5. app: nginx
    6. name: nginx-deployment-basic
    7. namespace: default
    8. spec:
    9. progressDeadlineSeconds: 600
    10. replicas: 1
    11. revisionHistoryLimit: 10
    12. selector:
    13. matchLabels:
    14. app: nginx
    15. strategy:
    16. rollingUpdate:
    17. maxSurge: 25%
    18. maxUnavailable: 25%
    19. type: RollingUpdate
    20. template:
    21. metadata:
    22. labels:
    23. app: nginx
    24. spec:
    25. containers:
    26. - image: 'nginx:1.7.9'
    27. imagePullPolicy: IfNotPresent
    28. name: nginx
    29. ports:
    30. - containerPort: 80
    31. protocol: TCP
    32. readinessProbe:
    33. failureThreshold: 3
    34. httpGet:
    35. path: /
    36. port: 80
    37. scheme: HTTP
    38. initialDelaySeconds: 10
    39. periodSeconds: 10
    40. successThreshold: 1
    41. timeoutSeconds: 1
    42. resources: {}
    43. terminationMessagePath: /dev/termination-log
    44. terminationMessagePolicy: File
    45. dnsPolicy: ClusterFirst
    46. restartPolicy: Always
    47. schedulerName: default-scheduler
    48. securityContext: {}
    49. terminationGracePeriodSeconds: 30