image.png

image.png

PodAffinity

所有PodAffinityTerm都必须满足

  1. // Pod affinity is a group of inter pod affinity scheduling rules.
  2. type PodAffinity struct {
  3. // NOT YET IMPLEMENTED. TODO: Uncomment field once it is implemented.
  4. // If the affinity requirements specified by this field are not met at
  5. // scheduling time, the pod will not be scheduled onto the node.
  6. // If the affinity requirements specified by this field cease to be met
  7. // at some point during pod execution (e.g. due to a pod label update), the
  8. // system will try to eventually evict the pod from its node.
  9. // When there are multiple elements, the lists of nodes corresponding to each
  10. // podAffinityTerm are intersected, i.e. all terms must be satisfied.
  11. // +optional
  12. // RequiredDuringSchedulingRequiredDuringExecution []PodAffinityTerm `json:"requiredDuringSchedulingRequiredDuringExecution,omitempty"`
  13. // If the affinity requirements specified by this field are not met at
  14. // scheduling time, the pod will not be scheduled onto the node.
  15. // If the affinity requirements specified by this field cease to be met
  16. // at some point during pod execution (e.g. due to a pod label update), the
  17. // system may or may not try to eventually evict the pod from its node.
  18. // When there are multiple elements, the lists of nodes corresponding to each
  19. // podAffinityTerm are intersected, i.e. all terms must be satisfied.
  20. // +optional
  21. RequiredDuringSchedulingIgnoredDuringExecution []PodAffinityTerm `json:"requiredDuringSchedulingIgnoredDuringExecution,omitempty" protobuf:"bytes,1,rep,name=requiredDuringSchedulingIgnoredDuringExecution"`
  22. // The scheduler will prefer to schedule pods to nodes that satisfy
  23. // the affinity expressions specified by this field, but it may choose
  24. // a node that violates one or more of the expressions. The node that is
  25. // most preferred is the one with the greatest sum of weights, i.e.
  26. // for each node that meets all of the scheduling requirements (resource
  27. // request, requiredDuringScheduling affinity expressions, etc.),
  28. // compute a sum by iterating through the elements of this field and adding
  29. // "weight" to the sum if the node has pods which matches the corresponding podAffinityTerm; the
  30. // node(s) with the highest sum are the most preferred.
  31. // +optional
  32. PreferredDuringSchedulingIgnoredDuringExecution []WeightedPodAffinityTerm `json:"preferredDuringSchedulingIgnoredDuringExecution,omitempty" protobuf:"bytes,2,rep,name=preferredDuringSchedulingIgnoredDuringExecution"`
  33. }

PodAffinityTerm

一个PodAffinity包含多个PodAffinityTerm,多个PodAffinityTerm是AND关系。
一个PodAffinityTerm对应一个LabelSelector

  1. // Defines a set of pods (namely those matching the labelSelector
  2. // relative to the given namespace(s)) that this pod should be
  3. // co-located (affinity) or not co-located (anti-affinity) with,
  4. // where co-located is defined as running on a node whose value of
  5. // the label with key <topologyKey> matches that of any node on which
  6. // a pod of the set of pods is running
  7. type PodAffinityTerm struct {
  8. // A label query over a set of resources, in this case pods.
  9. // +optional
  10. LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty" protobuf:"bytes,1,opt,name=labelSelector"`
  11. // namespaces specifies which namespaces the labelSelector applies to (matches against);
  12. // null or empty list means "this pod's namespace"
  13. // +optional
  14. Namespaces []string `json:"namespaces,omitempty" protobuf:"bytes,2,rep,name=namespaces"`
  15. // This pod should be co-located (affinity) or not co-located (anti-affinity) with the pods matching
  16. // the labelSelector in the specified namespaces, where co-located is defined as running on a node
  17. // whose value of the label with key topologyKey matches that of any node on which any of the
  18. // selected pods is running.
  19. // Empty topologyKey is not allowed.
  20. TopologyKey string `json:"topologyKey" protobuf:"bytes,3,opt,name=topologyKey"`
  21. }

LabelSelector

matchLabels 是由 {key,value} 对组成的映射。 matchLabels 映射中的单个 {key,value } 等同于 matchExpressions 的元素, 其 key 字段为 “key”,operator 为 “In”,而 values 数组仅包含 “value”。

matchExpressions 是 Pod 选择算符需求的列表。 有效的运算符包括 InNotInExistsDoesNotExist。 在 InNotIn 的情况下,设置的值必须是非空的。 matchExpressions是数组:[]LabelSelectorRequirement,多个LabelSelectorRequirement之间是AND关系

来自 matchLabelsmatchExpressions 的所有要求都按逻辑与的关系组合到一起 — 它们必须都满足才能匹配。**

  1. // Note:
  2. // There are two different styles of label selectors used in versioned types:
  3. // an older style which is represented as just a string in versioned types, and a
  4. // newer style that is structured. LabelSelector is an internal representation for the
  5. // latter style.
  6. // A label selector is a label query over a set of resources. The result of matchLabels and
  7. // matchExpressions are ANDed. An empty label selector matches all objects. A null
  8. // label selector matches no objects.
  9. type LabelSelector struct {
  10. // matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels
  11. // map is equivalent to an element of matchExpressions, whose key field is "key", the
  12. // operator is "In", and the values array contains only "value". The requirements are ANDed.
  13. // +optional
  14. MatchLabels map[string]string `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"`
  15. // matchExpressions is a list of label selector requirements. The requirements are ANDed.
  16. // +optional
  17. MatchExpressions []LabelSelectorRequirement `json:"matchExpressions,omitempty" protobuf:"bytes,2,rep,name=matchExpressions"`
  18. }

NodeAffinity

  1. // Node affinity is a group of node affinity scheduling rules.
  2. type NodeAffinity struct {
  3. // NOT YET IMPLEMENTED. TODO: Uncomment field once it is implemented.
  4. // If the affinity requirements specified by this field are not met at
  5. // scheduling time, the pod will not be scheduled onto the node.
  6. // If the affinity requirements specified by this field cease to be met
  7. // at some point during pod execution (e.g. due to an update), the system
  8. // will try to eventually evict the pod from its node.
  9. // +optional
  10. // RequiredDuringSchedulingRequiredDuringExecution *NodeSelector `json:"requiredDuringSchedulingRequiredDuringExecution,omitempty"`
  11. // If the affinity requirements specified by this field are not met at
  12. // scheduling time, the pod will not be scheduled onto the node.
  13. // If the affinity requirements specified by this field cease to be met
  14. // at some point during pod execution (e.g. due to an update), the system
  15. // may or may not try to eventually evict the pod from its node.
  16. // +optional
  17. RequiredDuringSchedulingIgnoredDuringExecution *NodeSelector `json:"requiredDuringSchedulingIgnoredDuringExecution,omitempty" protobuf:"bytes,1,opt,name=requiredDuringSchedulingIgnoredDuringExecution"`
  18. // The scheduler will prefer to schedule pods to nodes that satisfy
  19. // the affinity expressions specified by this field, but it may choose
  20. // a node that violates one or more of the expressions. The node that is
  21. // most preferred is the one with the greatest sum of weights, i.e.
  22. // for each node that meets all of the scheduling requirements (resource
  23. // request, requiredDuringScheduling affinity expressions, etc.),
  24. // compute a sum by iterating through the elements of this field and adding
  25. // "weight" to the sum if the node matches the corresponding matchExpressions; the
  26. // node(s) with the highest sum are the most preferred.
  27. // +optional
  28. PreferredDuringSchedulingIgnoredDuringExecution []PreferredSchedulingTerm `json:"preferredDuringSchedulingIgnoredDuringExecution,omitempty" protobuf:"bytes,2,rep,name=preferredDuringSchedulingIgnoredDuringExecution"`
  29. }

NodeSelector

一个NodeAffinity对一个NodeSelector。
一个NodeSelector可以配置多个NodeSelectorTerm,多个NodeSelectorTerm之间是OR关系

  1. // A node selector represents the union of the results of one or more label queries
  2. // over a set of nodes; that is, it represents the OR of the selectors represented
  3. // by the node selector terms.
  4. type NodeSelector struct {
  5. //Required. A list of node selector terms. The terms are ORed.
  6. NodeSelectorTerms []NodeSelectorTerm `json:"nodeSelectorTerms" protobuf:"bytes,1,rep,name=nodeSelectorTerms"`
  7. }

NodeSelectorTerm

每个NodeSelectorTerm有一个属性matchExpressions,matchExpression数组是AND关系

  1. // A null or empty node selector term matches no objects. The requirements of
  2. // them are ANDed.
  3. // The TopologySelectorTerm type implements a subset of the NodeSelectorTerm.
  4. type NodeSelectorTerm struct {
  5. // A list of node selector requirements by node's labels.
  6. // +optional
  7. MatchExpressions []NodeSelectorRequirement `json:"matchExpressions,omitempty" protobuf:"bytes,1,rep,name=matchExpressions"`
  8. // A list of node selector requirements by node's fields.
  9. // +optional
  10. MatchFields []NodeSelectorRequirement `json:"matchFields,omitempty" protobuf:"bytes,2,rep,name=matchFields"`
  11. }