Pod anti-affinity is slightly different than 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.
To reboot a node using pod anti-affinity:
-
Edit the node specification to configure pod anti-affinity:
apiVersion: v1 kind: Pod metadata: name: with-pod-antiaffinity spec: affinity: podAntiAffinity: (1) preferredDuringSchedulingIgnoredDuringExecution: (2) - weight: 100 (3) podAffinityTerm: labelSelector: matchExpressions: - key: registry (4) operator: In (5) values: - default topologyKey: kubernetes.io/hostname
-
Stanza to configure pod anti-affinity.
-
Defines a preferred rule.
-
Specifies a weight for a preferred rule. The node with the highest weight is preferred.
-
Description of the pod label that determines when the anti-affinity rule applies. Specify a key and value for the label.
-
The operator represents 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 beIn
,NotIn
,Exists
, orDoesNotExist
.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.