Difference between revisions of "Kubernetes pod affinity and anti affinity"

From wikieduonline
Jump to navigation Jump to search
Line 1: Line 1:
 
  https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity
 
  https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity
  
== Official example ==
+
== Official examples ==
 
<pre>
 
<pre>
 
apiVersion: v1
 
apiVersion: v1
Line 56: Line 56:
 
</pre>
 
</pre>
  
 +
<pre>
 +
apiVersion: v1
 +
 +
kind: Pod
 +
 +
metadata:
 +
 +
  name: with-pod-affinity
 +
 +
spec:
 +
 +
  affinity:
 +
 +
    podAffinity:
 +
 +
      requiredDuringSchedulingIgnoredDuringExecution:
 +
 +
      - labelSelector:
 +
 +
          matchExpressions:
 +
 +
          - key: security
 +
 +
            operator: In
 +
 +
            values:
 +
 +
            - S1
 +
 +
        topologyKey: topology.kubernetes.io/zone
 +
 +
    podAntiAffinity:
 +
 +
      preferredDuringSchedulingIgnoredDuringExecution:
 +
 +
      - weight: 100
 +
 +
        podAffinityTerm:
 +
 +
          labelSelector:
 +
 +
            matchExpressions:
 +
 +
            - key: security
 +
 +
              operator: In
 +
 +
              values:
 +
 +
              - S2
 +
 +
          topologyKey: topology.kubernetes.io/zone
 +
 +
  containers:
 +
 +
  - name: with-pod-affinity
 +
 +
    image: registry.k8s.io/pause:2.0
 +
</pre>
  
{{K8s pods}}
+
== See also ==
 +
* {{K8s pods}}

Revision as of 16:59, 20 September 2022

https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity

Official examples

apiVersion: v1

kind: Pod

metadata:

  name: with-node-affinity

spec:

  affinity:

    nodeAffinity:

      requiredDuringSchedulingIgnoredDuringExecution:

        nodeSelectorTerms:

        - matchExpressions:

          - key: topology.kubernetes.io/zone

            operator: In

            values:

            - antarctica-east1

            - antarctica-west1

      preferredDuringSchedulingIgnoredDuringExecution:

      - weight: 1

        preference:

          matchExpressions:

          - key: another-node-label-key

            operator: In

            values:

            - another-node-label-value

  containers:

  - name: with-node-affinity

    image: registry.k8s.io/pause:2.0
apiVersion: v1

kind: Pod

metadata:

  name: with-pod-affinity

spec:

  affinity:

    podAffinity:

      requiredDuringSchedulingIgnoredDuringExecution:

      - labelSelector:

          matchExpressions:

          - key: security

            operator: In

            values:

            - S1

        topologyKey: topology.kubernetes.io/zone

    podAntiAffinity:

      preferredDuringSchedulingIgnoredDuringExecution:

      - weight: 100

        podAffinityTerm:

          labelSelector:

            matchExpressions:

            - key: security

              operator: In

              values:

              - S2

          topologyKey: topology.kubernetes.io/zone

  containers:

  - name: with-pod-affinity

    image: registry.k8s.io/pause:2.0

See also

Advertising: