Write node affinity in k8s

In Kubernetes, node affinity (Node Affinity) can be used to specify the association rules between Pods and nodes to determine which nodes the Pods should be scheduled on.

  1. Node affinity type:

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

    • In the Pod's YAML file, use affinityfields to specify node affinity rules.
    • In affinityfields, use nodeAffinitysubfields to define node affinity rules.

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

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: nginx:latest
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: env
            operator: In
            values:
            - production

In the above example, nodeAffinitythe field defines a required node affinity rule, that is, the Pod needs to be scheduled env=productionon the node with the label.

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

Using node affinity, pods can be scheduled to specific nodes based on the node's labels and conditions to meet the needs of the application or to take advantage of resources or environments on specific nodes.

Guess you like

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