2018-11-05 20:45:52 +00:00
|
|
|
---
|
2020-02-06 23:45:31 +00:00
|
|
|
layout: docs
|
2023-01-30 14:48:43 +00:00
|
|
|
page_title: affinity Block - Job Specification
|
2018-11-05 20:45:52 +00:00
|
|
|
description: |-
|
2023-01-30 14:48:43 +00:00
|
|
|
The "affinity" block allows restricting the set of eligible nodes.
|
2018-11-05 20:45:52 +00:00
|
|
|
Affinities may filter on attributes or metadata. Additionally affinities may
|
|
|
|
be specified at the job, group, or task levels for ultimate flexibility.
|
|
|
|
---
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
# `affinity` Block
|
2018-11-05 20:45:52 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
<Placement
|
|
|
|
groups={[
|
|
|
|
['job', 'affinity'],
|
|
|
|
['job', 'group', 'affinity'],
|
2020-09-30 13:48:40 +00:00
|
|
|
['job', 'group', 'task', 'affinity'],
|
2020-02-06 23:45:31 +00:00
|
|
|
]}
|
|
|
|
/>
|
2018-11-05 20:45:52 +00:00
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
The `affinity` block allows operators to express placement preference for a set of nodes. Affinities may
|
2018-11-05 20:45:52 +00:00
|
|
|
be expressed on [attributes][interpolation] or [client metadata][client-meta].
|
|
|
|
Additionally affinities may be specified at the [job][job], [group][group], or
|
|
|
|
[task][task] levels for ultimate flexibility.
|
|
|
|
|
2019-01-24 18:12:40 +00:00
|
|
|
```hcl
|
|
|
|
job "docs" {
|
|
|
|
# Prefer nodes in the us-west1 datacenter
|
|
|
|
affinity {
|
|
|
|
attribute = "${node.datacenter}"
|
|
|
|
value = "us-west1"
|
|
|
|
weight = 100
|
|
|
|
}
|
|
|
|
|
|
|
|
group "example" {
|
|
|
|
# Prefer the "r1" rack
|
|
|
|
affinity {
|
2019-01-31 15:15:00 +00:00
|
|
|
attribute = "${meta.rack}"
|
2019-01-24 18:12:40 +00:00
|
|
|
value = "r1"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
task "server" {
|
2022-12-01 00:01:56 +00:00
|
|
|
# Prefer nodes where "my_custom_value" is greater than 3
|
2019-01-24 18:12:40 +00:00
|
|
|
affinity {
|
|
|
|
attribute = "${meta.my_custom_value}"
|
|
|
|
operator = ">"
|
|
|
|
value = "3"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
Affinities apply to task groups but may be specified within job and task blocks as well.
|
2019-01-24 22:50:23 +00:00
|
|
|
Job affinities apply to all groups within the job. Task affinities apply to the whole task group
|
|
|
|
that the task is a part of.
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
Nomad will use affinities when computing scores for placement. Nodes that match affinities will
|
|
|
|
have their scores boosted. Affinity scores are combined with other scoring factors such as bin packing.
|
|
|
|
Operators can use weights to express relative preference across multiple affinities. If no nodes match a given affinity,
|
|
|
|
placement is still successful. This is different from [constraints][constraint] where placement is
|
|
|
|
restricted only to nodes that meet the constraint's criteria.
|
|
|
|
|
|
|
|
## `affinity` Parameters
|
|
|
|
|
|
|
|
- `attribute` `(string: "")` - Specifies the name or reference of the attribute
|
|
|
|
to examine for the affinity. This can be any of the [Nomad interpolated
|
2023-01-25 17:31:14 +00:00
|
|
|
values](/nomad/docs/runtime/interpolation#interpreted_node_vars).
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
- `operator` `(string: "=")` - Specifies the comparison operator. The ordering is
|
|
|
|
compared lexically. Possible values include:
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```text
|
|
|
|
=
|
|
|
|
!=
|
|
|
|
>
|
|
|
|
>=
|
|
|
|
<
|
|
|
|
<=
|
|
|
|
regexp
|
|
|
|
set_contains_all
|
|
|
|
set_contains_any
|
|
|
|
version
|
|
|
|
```
|
|
|
|
|
|
|
|
For a detailed explanation of these values and their behavior, please see
|
|
|
|
the [operator values section](#operator-values).
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
- `value` `(string: "")` - Specifies the value to compare the attribute against
|
|
|
|
using the specified operation. This can be a literal value, another attribute,
|
|
|
|
or any [Nomad interpolated
|
2023-01-25 17:31:14 +00:00
|
|
|
values](/nomad/docs/runtime/interpolation#interpreted_node_vars).
|
2018-11-05 20:45:52 +00:00
|
|
|
|
2019-01-30 20:20:38 +00:00
|
|
|
- `weight` `(integer: 50)` - Specifies a weight for the affinity. The weight is used
|
2018-11-05 20:45:52 +00:00
|
|
|
during scoring and must be an integer between -100 to 100. Negative weights act as
|
|
|
|
anti affinities, causing nodes that match them to be scored lower. Weights can be used
|
|
|
|
when there is more than one affinity to express relative preference across them.
|
|
|
|
|
|
|
|
### `operator` Values
|
|
|
|
|
|
|
|
This section details the specific values for the "operator" parameter in the
|
|
|
|
Nomad job specification for affinities. The operator is always specified as a
|
|
|
|
string, but the string can take on different values which change the behavior of
|
|
|
|
the overall affinity evaluation.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
operator = "..."
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- `"regexp"` - Specifies a regular expression affinity against the attribute.
|
|
|
|
The syntax of the regular expressions accepted is the same general syntax used
|
|
|
|
by Perl, Python, and many other languages. More precisely, it is the syntax
|
|
|
|
accepted by RE2 and described at in the [Google RE2
|
|
|
|
syntax](https://golang.org/s/re2syntax).
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "..."
|
|
|
|
operator = "regexp"
|
|
|
|
value = "[a-z0-9]"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
- `"set_contains_all"` - Specifies a contains affinity against the attribute. The
|
|
|
|
attribute and the list being checked are split using commas. This will check
|
|
|
|
that the given attribute contains **all** of the specified elements.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "..."
|
2022-03-07 18:55:57 +00:00
|
|
|
operator = "set_contains_all"
|
2020-02-06 23:45:31 +00:00
|
|
|
value = "a,b,c"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- `"set_contains"` - Same as `set_contains_all`
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
- `"set_contains_any"` - Specifies a contains affinity against the attribute. The
|
|
|
|
attribute and the list being checked are split using commas. This will check
|
|
|
|
that the given attribute contains **any** of the specified elements.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "..."
|
2022-03-07 18:55:57 +00:00
|
|
|
operator = "set_contains_any"
|
2020-02-06 23:45:31 +00:00
|
|
|
value = "a,b,c"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
- `"version"` - Specifies a version affinity against the attribute. This
|
|
|
|
supports a comma-separated list of values, including the pessimistic
|
|
|
|
operator. For more examples please see the [go-version
|
|
|
|
repository](https://github.com/hashicorp/go-version) for more specific
|
|
|
|
examples.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "..."
|
|
|
|
operator = "version"
|
|
|
|
value = ">= 0.1.0, < 0.2"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
## `affinity` Examples
|
|
|
|
|
2023-01-30 14:48:43 +00:00
|
|
|
The following examples only show the `affinity` blocks. Remember that the
|
|
|
|
`affinity` block is only valid in the placements listed above.
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
### Kernel Data
|
|
|
|
|
|
|
|
This example adds a preference for running on nodes which have a kernel version
|
|
|
|
higher than "3.19".
|
|
|
|
|
|
|
|
```hcl
|
2019-01-24 18:12:40 +00:00
|
|
|
affinity {
|
2018-11-05 20:45:52 +00:00
|
|
|
attribute = "${attr.kernel.version}"
|
2019-02-06 19:13:40 +00:00
|
|
|
operator = "version"
|
|
|
|
value = "> 3.19"
|
2018-11-05 20:45:52 +00:00
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Operating Systems
|
|
|
|
|
|
|
|
This example adds a preference to running on nodes that are running Ubuntu
|
|
|
|
14.04
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "${attr.os.name}"
|
|
|
|
value = "ubuntu"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
affinity {
|
|
|
|
attribute = "${attr.os.version}"
|
|
|
|
value = "14.04"
|
|
|
|
weight = 100
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Meta Data
|
|
|
|
|
|
|
|
The following example adds a preference to running on nodes with specific rack metadata
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "${meta.rack}"
|
|
|
|
value = "rack1"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The following example adds a preference to running on nodes in a specific datacenter.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "${node.datacenter}"
|
|
|
|
value = "us-west1"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Cloud Metadata
|
|
|
|
|
|
|
|
When possible, Nomad populates node attributes from the cloud environment. These
|
|
|
|
values are accessible as filters in affinities. This example adds a preference to run this
|
|
|
|
task on nodes that are memory-optimized on AWS.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
affinity {
|
|
|
|
attribute = "${attr.platform.aws.instance-type}"
|
|
|
|
value = "m4.xlarge"
|
|
|
|
weight = 50
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-01-25 17:31:14 +00:00
|
|
|
[job]: /nomad/docs/job-specification/job 'Nomad job Job Specification'
|
|
|
|
[group]: /nomad/docs/job-specification/group 'Nomad group Job Specification'
|
|
|
|
[client-meta]: /nomad/docs/configuration/client#meta 'Nomad meta Job Specification'
|
|
|
|
[task]: /nomad/docs/job-specification/task 'Nomad task Job Specification'
|
|
|
|
[interpolation]: /nomad/docs/runtime/interpolation 'Nomad interpolation'
|
|
|
|
[node-variables]: /nomad/docs/runtime/interpolation#node-variables- 'Nomad interpolation-Node variables'
|
|
|
|
[constraint]: /nomad/docs/job-specification/constraint 'Nomad Constraint job Specification'
|
2018-11-05 20:45:52 +00:00
|
|
|
|
|
|
|
### Placement Details
|
2020-02-06 23:45:31 +00:00
|
|
|
|
2018-11-05 20:45:52 +00:00
|
|
|
Operators can run `nomad alloc status -verbose` to get more detailed information on various
|
|
|
|
factors, including affinities that affect the final placement.
|
|
|
|
|
2018-11-15 17:32:22 +00:00
|
|
|
#### Example Placement Metadata
|
2020-02-06 23:45:31 +00:00
|
|
|
|
2018-11-05 20:45:52 +00:00
|
|
|
The following is a snippet from the CLI output of `nomad alloc status -verbose <alloc-id>` showing scoring metadata.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
```text
|
2018-11-05 20:45:52 +00:00
|
|
|
Placement Metrics
|
|
|
|
Node binpack job-anti-affinity node-reschedule-penalty node-affinity final score
|
|
|
|
30bd48cc-d760-1096-9bab-13caac424af5 0.225 -0.6 0 1 0.208
|
|
|
|
f2aa8b59-96b8-202f-2258-d98c93e360ab 0.225 -0.6 0 1 0.208
|
|
|
|
86df0f74-15cc-3a0e-23f0-ad7306131e0d 0.0806 0 0 0 0.0806
|
|
|
|
7d6c2e9e-b080-5995-8b9d-ef1695458b52 0.0806 0 0 0 0.0806
|
|
|
|
```
|
|
|
|
|
|
|
|
The placement score is affected by the following factors.
|
|
|
|
|
|
|
|
- `bin-packing` - Scores nodes according to how well they fit requirements. Optimizes for using minimal number of nodes.
|
|
|
|
- `job-anti-affinity` - A penalty added for additional instances of the same job on a node, used to avoid having too many instances
|
|
|
|
of a job on the same node.
|
|
|
|
- `node-reschedule-penalty` - Used when the job is being rescheduled. Nomad adds a penalty to avoid placing the job on a node where
|
|
|
|
it has failed to run before.
|
2023-01-30 14:48:43 +00:00
|
|
|
- `node-affinity` - Used when the criteria specified in the `affinity` block matches the node.
|