Node affinity

The description of a required statement is called nodeSelectorTerms, and is composed of one or more matchExpressions. matchExpressions, which is similar to the matchExpressions that is used by other Kubernetes controllers such as Deployment and StatefulSets, but in this case, the matchExpressions node supports the following operators: In, NotIn, Exists, DoesNotExist, Gt, and Lt.

A node affinity requirement looks as follows:

...
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: <key_1>
operator: <In, NotIn, Exists, DoesNotExist. Gt, or Lt>
values:
- <value_1>
- <value_2>
- ...
- key: <key_2>
...
  - matchExpressions:
...
...

For conditions that have multiple nodeSelectorTerms defined (each term is a matchExpression object), the required statement will be evaluated as true if any nodeSelectorTerm is met. But for multiple expressions in a matchExpression object, the term will be evaluated as true if all matchExpressions are satisfied, for instance, if we have the following configuration and their results:

nodeSelectorTerms:
- matchExpressions: <- <nodeSelectorTerm_A>
- <
matchExpression_A1> : true
- <matchExpression_A2> : true
- matchExpressions: <- <nodeSelectorTerm_B>
- <matchExpression_B1> : false
- <matchExpression_B2> : true
- <matchExpression_B3> : false

The evaluation result of the nodeSelectorTerms would be true after applying the previous AND/OR rules:

Term_A = matchExpression_A1 && matchExpression_A2
Term_B = matchExpression_B1 && matchExpression_B2 && matchExpression_B3
nodeSelectorTerms = Term_A || Term_B
>> (true && true) || (false && true && false)
>> true

The In and NotIn operators can match multiple values, while Exists and DoesNotExist don't take any value (values: []); Gt and Lt only take a single integer value in the string type (values: ["123"]).

The require statement can be a replacement for nodeSelector. For instance, the affinity section and the following nodeSelector section are equivalent:

...
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: purpose
operator: In
values: ["sandbox"]
- key: owner
operator: In
values: ["alpha"]
nodeSelector:
purpose: sandbox
owner: alpha
...
As well as matchExpressions, there's another term, matchFields, for selecting values outside labels. As of Kubernetes 1.13, the only supported field is metadata.name, which is used to pick a node whose name isn't equal to the value of the kubernetes.io/hostname label. Its syntax is basically the same as matchExpression{"matchFields":[{"key": "metadata.name", "operator": "In", "values": ["target-name"]}]}.

The configuration of preferences is akin to the required statement as they share matchExpressions to express relations. One difference is that a preference has a weight field to denote its importance, and the range of a weight field is 1-100:

...
preferredDuringSchedulingIgnoredDuringExecution:
- weight: <1-100>
preference:
- matchExpressions:
- key: <key_1>
operator: <In, NotIn, Exists, DoesNotExist. Gt, or Lt>
values:
- <value_1>
- <value_2>
- ...
- key: <key_2>
...
- matchExpressions:
...
...

If we write down the condition specified in the diagram that we used in the previous section in the preference configuration, it would look as follows:

...
preferredDuringSchedulingIgnoredDuringExecution:

- weight: 5
preference:
matchExpressions:
- key: instance_type
operator: In
values:
- medium
- weight: 10
preference:
matchExpressions:
- key: instance_type
operator: In
values:
- large
- weight: 10
preference:
matchExpressions:
- key: region
operator: In
values:
- NA
...
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset