github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/affinity.mdx (about) 1 --- 2 layout: docs 3 page_title: affinity Stanza - Job Specification 4 description: |- 5 The "affinity" stanza allows restricting the set of eligible nodes. 6 Affinities may filter on attributes or metadata. Additionally affinities may 7 be specified at the job, group, or task levels for ultimate flexibility. 8 --- 9 10 # `affinity` Stanza 11 12 <Placement 13 groups={[ 14 ['job', 'affinity'], 15 ['job', 'group', 'affinity'], 16 ['job', 'group', 'task', 'affinity'], 17 ]} 18 /> 19 20 The `affinity` stanza allows operators to express placement preference for a set of nodes. Affinities may 21 be expressed on [attributes][interpolation] or [client metadata][client-meta]. 22 Additionally affinities may be specified at the [job][job], [group][group], or 23 [task][task] levels for ultimate flexibility. 24 25 ```hcl 26 job "docs" { 27 # Prefer nodes in the us-west1 datacenter 28 affinity { 29 attribute = "${node.datacenter}" 30 value = "us-west1" 31 weight = 100 32 } 33 34 group "example" { 35 # Prefer the "r1" rack 36 affinity { 37 attribute = "${meta.rack}" 38 value = "r1" 39 weight = 50 40 } 41 42 task "server" { 43 # Prefer nodes where "my_custom_value" is greater than 3 44 affinity { 45 attribute = "${meta.my_custom_value}" 46 operator = ">" 47 value = "3" 48 weight = 50 49 } 50 } 51 } 52 } 53 ``` 54 55 Affinities apply to task groups but may be specified within job and task stanzas as well. 56 Job affinities apply to all groups within the job. Task affinities apply to the whole task group 57 that the task is a part of. 58 59 Nomad will use affinities when computing scores for placement. Nodes that match affinities will 60 have their scores boosted. Affinity scores are combined with other scoring factors such as bin packing. 61 Operators can use weights to express relative preference across multiple affinities. If no nodes match a given affinity, 62 placement is still successful. This is different from [constraints][constraint] where placement is 63 restricted only to nodes that meet the constraint's criteria. 64 65 ## `affinity` Parameters 66 67 - `attribute` `(string: "")` - Specifies the name or reference of the attribute 68 to examine for the affinity. This can be any of the [Nomad interpolated 69 values](/docs/runtime/interpolation#interpreted_node_vars). 70 71 - `operator` `(string: "=")` - Specifies the comparison operator. The ordering is 72 compared lexically. Possible values include: 73 74 ```text 75 = 76 != 77 > 78 >= 79 < 80 <= 81 regexp 82 set_contains_all 83 set_contains_any 84 version 85 ``` 86 87 For a detailed explanation of these values and their behavior, please see 88 the [operator values section](#operator-values). 89 90 - `value` `(string: "")` - Specifies the value to compare the attribute against 91 using the specified operation. This can be a literal value, another attribute, 92 or any [Nomad interpolated 93 values](/docs/runtime/interpolation#interpreted_node_vars). 94 95 - `weight` `(integer: 50)` - Specifies a weight for the affinity. The weight is used 96 during scoring and must be an integer between -100 to 100. Negative weights act as 97 anti affinities, causing nodes that match them to be scored lower. Weights can be used 98 when there is more than one affinity to express relative preference across them. 99 100 ### `operator` Values 101 102 This section details the specific values for the "operator" parameter in the 103 Nomad job specification for affinities. The operator is always specified as a 104 string, but the string can take on different values which change the behavior of 105 the overall affinity evaluation. 106 107 ```hcl 108 affinity { 109 operator = "..." 110 } 111 ``` 112 113 - `"regexp"` - Specifies a regular expression affinity against the attribute. 114 The syntax of the regular expressions accepted is the same general syntax used 115 by Perl, Python, and many other languages. More precisely, it is the syntax 116 accepted by RE2 and described at in the [Google RE2 117 syntax](https://golang.org/s/re2syntax). 118 119 ```hcl 120 affinity { 121 attribute = "..." 122 operator = "regexp" 123 value = "[a-z0-9]" 124 weight = 50 125 } 126 ``` 127 128 - `"set_contains_all"` - Specifies a contains affinity against the attribute. The 129 attribute and the list being checked are split using commas. This will check 130 that the given attribute contains **all** of the specified elements. 131 132 ```hcl 133 affinity { 134 attribute = "..." 135 operator = "set_contains_all" 136 value = "a,b,c" 137 weight = 50 138 } 139 ``` 140 141 - `"set_contains"` - Same as `set_contains_all` 142 143 - `"set_contains_any"` - Specifies a contains affinity against the attribute. The 144 attribute and the list being checked are split using commas. This will check 145 that the given attribute contains **any** of the specified elements. 146 147 ```hcl 148 affinity { 149 attribute = "..." 150 operator = "set_contains_any" 151 value = "a,b,c" 152 weight = 50 153 } 154 ``` 155 156 - `"version"` - Specifies a version affinity against the attribute. This 157 supports a comma-separated list of values, including the pessimistic 158 operator. For more examples please see the [go-version 159 repository](https://github.com/hashicorp/go-version) for more specific 160 examples. 161 162 ```hcl 163 affinity { 164 attribute = "..." 165 operator = "version" 166 value = ">= 0.1.0, < 0.2" 167 weight = 50 168 } 169 ``` 170 171 ## `affinity` Examples 172 173 The following examples only show the `affinity` stanzas. Remember that the 174 `affinity` stanza is only valid in the placements listed above. 175 176 ### Kernel Data 177 178 This example adds a preference for running on nodes which have a kernel version 179 higher than "3.19". 180 181 ```hcl 182 affinity { 183 attribute = "${attr.kernel.version}" 184 operator = "version" 185 value = "> 3.19" 186 weight = 50 187 } 188 ``` 189 190 ### Operating Systems 191 192 This example adds a preference to running on nodes that are running Ubuntu 193 14.04 194 195 ```hcl 196 affinity { 197 attribute = "${attr.os.name}" 198 value = "ubuntu" 199 weight = 50 200 } 201 202 affinity { 203 attribute = "${attr.os.version}" 204 value = "14.04" 205 weight = 100 206 } 207 ``` 208 209 ### Meta Data 210 211 The following example adds a preference to running on nodes with specific rack metadata 212 213 ```hcl 214 affinity { 215 attribute = "${meta.rack}" 216 value = "rack1" 217 weight = 50 218 } 219 ``` 220 221 The following example adds a preference to running on nodes in a specific datacenter. 222 223 ```hcl 224 affinity { 225 attribute = "${node.datacenter}" 226 value = "us-west1" 227 weight = 50 228 } 229 ``` 230 231 ### Cloud Metadata 232 233 When possible, Nomad populates node attributes from the cloud environment. These 234 values are accessible as filters in affinities. This example adds a preference to run this 235 task on nodes that are memory-optimized on AWS. 236 237 ```hcl 238 affinity { 239 attribute = "${attr.platform.aws.instance-type}" 240 value = "m4.xlarge" 241 weight = 50 242 } 243 ``` 244 245 [job]: /docs/job-specification/job 'Nomad job Job Specification' 246 [group]: /docs/job-specification/group 'Nomad group Job Specification' 247 [client-meta]: /docs/configuration/client#meta 'Nomad meta Job Specification' 248 [task]: /docs/job-specification/task 'Nomad task Job Specification' 249 [interpolation]: /docs/runtime/interpolation 'Nomad interpolation' 250 [node-variables]: /docs/runtime/interpolation#node-variables- 'Nomad interpolation-Node variables' 251 [constraint]: /docs/job-specification/constraint 'Nomad Constraint job Specification' 252 253 ### Placement Details 254 255 Operators can run `nomad alloc status -verbose` to get more detailed information on various 256 factors, including affinities that affect the final placement. 257 258 #### Example Placement Metadata 259 260 The following is a snippet from the CLI output of `nomad alloc status -verbose <alloc-id>` showing scoring metadata. 261 262 ```text 263 Placement Metrics 264 Node binpack job-anti-affinity node-reschedule-penalty node-affinity final score 265 30bd48cc-d760-1096-9bab-13caac424af5 0.225 -0.6 0 1 0.208 266 f2aa8b59-96b8-202f-2258-d98c93e360ab 0.225 -0.6 0 1 0.208 267 86df0f74-15cc-3a0e-23f0-ad7306131e0d 0.0806 0 0 0 0.0806 268 7d6c2e9e-b080-5995-8b9d-ef1695458b52 0.0806 0 0 0 0.0806 269 ``` 270 271 The placement score is affected by the following factors. 272 273 - `bin-packing` - Scores nodes according to how well they fit requirements. Optimizes for using minimal number of nodes. 274 - `job-anti-affinity` - A penalty added for additional instances of the same job on a node, used to avoid having too many instances 275 of a job on the same node. 276 - `node-reschedule-penalty` - Used when the job is being rescheduled. Nomad adds a penalty to avoid placing the job on a node where 277 it has failed to run before. 278 - `node-affinity` - Used when the criteria specified in the `affinity` stanza matches the node.