// Module included in the following assemblies:
//
// * nodes/nodes-nodes-rebooting.adoc

:_mod-docs-content-type: PROCEDURE
[id="nodes-nodes-rebooting-affinity_{context}"]
= Rebooting a node using pod anti-affinity

[role="_abstract"]
You can use pod anti-affinity to spread the workloads on a node to other nodes before performing a graceful node restart.

Pod anti-affinity is slightly different from node anti-affinity. Node anti-affinity can be
violated if there are no other suitable locations to deploy a pod. Pod
anti-affinity can be set to either required or preferred.

With this in place, if only two infrastructure nodes are available and one is rebooted, the container image registry
pod is prevented from running on the other node. `*oc get pods*` reports the pod as unready until a suitable node is available.
Once a node is available and all pods are back in ready state, the next node can be restarted.

The following procedure demonstrates how to reboot a node by using pod anti-affinity.

.Procedure

. Edit the node specification to configure pod anti-affinity:
+
[source,yaml]
----
apiVersion: v1
kind: Pod
metadata:
  name: with-pod-antiaffinity
spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: registry
              operator: In
              values:
              - default
          topologyKey: kubernetes.io/hostname
#...
----
+
where:

`spec.affinity.podAntiAffinity`:: Specifies the stanza to configure pod anti-affinity.
`spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:: Specifies a preferred rule.
`spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution.weight`:: Specifies a weight for a preferred rule. The node with the highest weight is preferred.
`spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution.podAffinityTerm.labelSelector.matchExpressions.key`:: Specifies a pod label that determines when the anti-affinity rule applies. Define a key and value for the label.
`spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution.podAffinityTerm.labelSelector.matchExpressions.operator`:: Specifies the relationship between the label on the existing pod and the set of values in the `matchExpression` parameters in the specification for the new pod. Can be `In`, `NotIn`, `Exists`, or `DoesNotExist`.
+
This example assumes the container image registry pod has a label of
`registry=default`. Pod anti-affinity can use any Kubernetes match
expression.

. Enable the `MatchInterPodAffinity` scheduler predicate in the scheduling policy file.
. Perform a graceful restart of the node.
