Kubernetes affinity

Regarding affinity (Affinity) in Kubernetes, the main difference between strong affinity (Hard Affinity) and weak affinity (Soft Affinity) is whether the container requires absolute compliance with the affinity rules for scheduling.

Strong affinity (Hard Affinity) :

Example configuration (YAML):

  • Requirement to meet the rules : Strong affinity requires that the container must be located on a node that meets the affinity rule, otherwise the container will not be scheduled into the cluster.
  • Application case : Strong affinity is suitable for situations where containers must be required to run on specific nodes. For example, you might want a critical database container to always run on a specific high-performance node to ensure fast data access and low latency.
affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: special-node
          operator: In
          values:
          - database

Weak affinity (Soft Affinity) :

  • Do not enforce compliance with rules : Weak affinity allows containers to be scheduled into the cluster without meeting the affinity rules, but will try to give priority to nodes that meet the rules.
  • Use case : weak affinity is suitable for situations where you want the container to try to satisfy the rules, but do not enforce them. This can be used for some non-critical application containers to make them more likely to run on nodes that meet the conditions, but without making them unschedulable to other nodes.

Example configuration (YAML):

affinity:
  nodeAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
    - weight: 50
      preference:
        matchExpressions:
        - key: special-node
          operator: In
          values:
          - application

In the above example, weightrepresents the weight of the container in selecting nodes, a higher weight will make it more likely that the container will run on a node that matches the rule, but will not enforce this.

おすすめ

転載: blog.csdn.net/summer_fish/article/details/132720300