Write the pod node affinity in k8s

In Kubernetes, Pod affinity (Pod Affinity) can be used to define the association rules between Pod and other Pods to control their scheduling on the same or different nodes.

  1. Pod affinity type:

    • requiredDuringSchedulingIgnoredDuringExecution: The Pod must meet the affinity rules, otherwise it cannot be scheduled.
    • preferredDuringSchedulingIgnoredDuringExecution: Pods are more inclined to meet affinity rules, but are not required to do so.
  2. Use Pod affinity in the Pod's YAML file:

    • In the Pod's YAML file, affinityspecify Pod affinity rules using fields.
    • In affinityfields, use podAffinityor podAntiAffinitysubfields to define Pod affinity rules.

Here's an example showing how to use Pod affinity in a Pod's YAML file:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx:latest
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - frontend
        topologyKey: kubernetes.io/hostname

In the above example, podAffinitythe field defines a required Pod affinity rule, that is, the Pod needs to be scheduled on the same app=frontendnode as other Pods with labels, and kubernetes.io/hostnamethe topology labels are used for matching.

Note that Pod affinity rules can use other operators ( NotIn, Exists, DoesNotExistetc.) and multiple match expressions to define more complex rules.

Pod affinity can be used to control the association between pods and other pods when scheduling pods to meet requirements such as deployment strategies, data locality, or load balancing.

Guess you like

Origin blog.csdn.net/qq_44370158/article/details/132170600