// Module included in the following assemblies:
//
// * nodes/nodes-scheduler-node-affinity.adoc

:_mod-docs-content-type: REFERENCE
[id="nodes-scheduler-node-affinity-example_{context}"]
= Sample node affinity rules

The following examples demonstrate node affinity.

[id="admin-guide-sched-affinity-examples1_{context}"]
== Node affinity with matching labels

The following example demonstrates node affinity for a node and pod with matching labels:

* The Node1 node has the label `zone:us`:
+
[source,terminal]
----
$ oc label node node1 zone=us
----
+
[TIP]
====
You can alternatively apply the following YAML to add the label:

[source,yaml]
----
kind: Node
apiVersion: v1
metadata:
  name: <node_name>
  labels:
    zone: us
#...
----
====

*  The pod-s1 pod has the `zone` and `us` key/value pair under a required node affinity rule:
+
[source,terminal]
----
$ cat pod-s1.yaml
----
+
.Example output
[source,yaml]
----
apiVersion: v1
kind: Pod
metadata:
  name: pod-s1
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
    - image: "docker.io/ocpqe/hello-pod"
      name: hello-pod
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: [ALL]
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
            - key: "zone"
              operator: In
              values:
              - us
#...
----

* The pod-s1 pod can be scheduled on Node1:
+
[source,terminal]
----
$ oc get pod -o wide
----
+
.Example output
[source,terminal]
----
NAME     READY     STATUS       RESTARTS   AGE      IP      NODE
pod-s1   1/1       Running      0          4m       IP1     node1
----

[id="admin-guide-sched-affinity-examples2_{context}"]
== Node affinity with no matching labels

The following example demonstrates node affinity for a node and pod without matching labels:

* The Node1 node has the label `zone:emea`:
+
[source,terminal]
----
$ oc label node node1 zone=emea
----
+
[TIP]
====
You can alternatively apply the following YAML to add the label:

[source,yaml]
----
kind: Node
apiVersion: v1
metadata:
  name: <node_name>
  labels:
    zone: emea
#...
----
====

*  The pod-s1 pod has the `zone` and `us` key/value pair under a required node affinity rule:
+
[source,terminal]
----
$ cat pod-s1.yaml
----
+
.Example output
[source,yaml]
----
apiVersion: v1
kind: Pod
metadata:
  name: pod-s1
spec:
  securityContext:
    runAsNonRoot: true
    seccompProfile:
      type: RuntimeDefault
  containers:
    - image: "docker.io/ocpqe/hello-pod"
      name: hello-pod
      securityContext:
        allowPrivilegeEscalation: false
        capabilities:
          drop: [ALL]
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
            - key: "zone"
              operator: In
              values:
              - us
#...
----

* The pod-s1 pod cannot be scheduled on Node1:
+
[source,terminal]
----
$ oc describe pod pod-s1
----
+
.Example output
[source,terminal]
----
...

Events:
 FirstSeen LastSeen Count From              SubObjectPath  Type                Reason
 --------- -------- ----- ----              -------------  --------            ------
 1m        33s      8     default-scheduler Warning        FailedScheduling    No nodes are available that match all of the following predicates:: MatchNodeSelector (1).
----
