github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/constraint.mdx (about) 1 --- 2 layout: docs 3 page_title: constraint Stanza - Job Specification 4 description: |- 5 The "constraint" stanza allows restricting the set of eligible nodes. 6 Constraints may filter on attributes or metadata. Additionally constraints may 7 be specified at the job, group, or task levels for ultimate flexibility. 8 --- 9 10 # `constraint` Stanza 11 12 <Placement 13 groups={[ 14 ['job', 'constraint'], 15 ['job', 'group', 'constraint'], 16 ['job', 'group', 'task', 'constraint'], 17 ]} 18 /> 19 20 The `constraint` allows restricting the set of eligible nodes. Constraints may 21 filter on [attributes][interpolation] or [client metadata][client-meta]. 22 Additionally constraints may be specified at the [job][job], [group][group], or 23 [task][task] levels for ultimate flexibility. 24 25 ~> **It is possible to define irreconcilable constraints in a job.** 26 For example, because all [tasks within a group are scheduled on the same client node][group], 27 specifying different [`${attr.unique.hostname}`][node-variables] constraints at 28 the task level will cause a job to be unplaceable. 29 30 ```hcl 31 job "docs" { 32 # All tasks in this job must run on linux. 33 constraint { 34 attribute = "${attr.kernel.name}" 35 value = "linux" 36 } 37 38 group "example" { 39 # All groups in this job should be scheduled on different hosts. 40 constraint { 41 operator = "distinct_hosts" 42 value = "true" 43 } 44 45 task "server" { 46 # All tasks must run where "my_custom_value" is greater than 3. 47 constraint { 48 attribute = "${meta.my_custom_value}" 49 operator = ">" 50 value = "3" 51 } 52 } 53 } 54 } 55 ``` 56 57 Placing constraints at both the job level and at the group level is redundant 58 since constraints are applied hierarchically. The job constraints will affect 59 all groups (and tasks) in the job. 60 61 ## `constraint` Parameters 62 63 - `attribute` `(string: "")` - Specifies the name or reference of the attribute 64 to examine for the constraint. This can be any of the [Nomad interpolated 65 values](/docs/runtime/interpolation#interpreted_node_vars). 66 67 - `operator` `(string: "=")` - Specifies the comparison operator. If the operator 68 is one of `>, >=, <, <=`, the ordering is compared numerically if the operands 69 are both integers or both floats, and lexically otherwise. Possible values include: 70 71 ```text 72 = 73 != 74 > 75 >= 76 < 77 <= 78 distinct_hosts 79 distinct_property 80 regexp 81 set_contains 82 set_contains_any 83 version 84 semver 85 is_set 86 is_not_set 87 ``` 88 89 For a detailed explanation of these values and their behavior, please see 90 the [operator values section](#operator-values). 91 92 - `value` `(string: "")` - Specifies the value to compare the attribute against 93 using the specified operation. This can be a literal value, another attribute, 94 or any [Nomad interpolated 95 values](/docs/runtime/interpolation#interpreted_node_vars). 96 97 ### `operator` Values 98 99 This section details the specific values for the "operator" parameter in the 100 Nomad job specification for constraints. The operator is always specified as a 101 string, but the string can take on different values which change the behavior of 102 the overall constraint evaluation. 103 104 ```hcl 105 constraint { 106 operator = "..." 107 } 108 ``` 109 110 - `"distinct_hosts"` - Instructs the scheduler to not co-locate any groups on 111 the same machine. When specified as a job constraint, it applies to all groups 112 in the job. When specified as a group constraint, the effect is constrained to 113 that group. This constraint can not be specified at the task level. Note that 114 the `attribute` parameter should be omitted when using this constraint. 115 116 ```hcl 117 constraint { 118 operator = "distinct_hosts" 119 value = "true" 120 } 121 ``` 122 123 The constraint may also be specified as follows for a more compact 124 representation: 125 126 ```hcl 127 constraint { 128 distinct_hosts = true 129 } 130 ``` 131 132 - `"distinct_property"` - Instructs the scheduler to select nodes that have a 133 distinct value of the specified property. The `value` parameter specifies how 134 many allocations are allowed to share the value of a property. The `value` 135 must be 1 or greater and if omitted, defaults to 1. When specified as a job 136 constraint, it applies to all groups in the job. When specified as a group 137 constraint, the effect is constrained to that group. This constraint can not 138 be specified at the task level. 139 140 ```hcl 141 constraint { 142 operator = "distinct_property" 143 attribute = "${meta.rack}" 144 value = "3" 145 } 146 ``` 147 148 The constraint may also be specified as follows for a more compact 149 representation: 150 151 ```hcl 152 constraint { 153 distinct_property = "${meta.rack}" 154 value = "3" 155 } 156 ``` 157 158 - `"regexp"` - Specifies a regular expression constraint against the attribute. 159 The syntax of the regular expressions accepted is the same general syntax used 160 by Perl, Python, and many other languages. More precisely, it is the syntax 161 accepted by RE2 and described at in the [Google RE2 162 syntax](https://golang.org/s/re2syntax). 163 164 ```hcl 165 constraint { 166 attribute = "..." 167 operator = "regexp" 168 value = "[a-z0-9]" 169 } 170 ``` 171 172 - `"set_contains"` - Specifies a contains constraint against the attribute. The 173 attribute and the list being checked are split using commas. This will check 174 that the given attribute contains **all** of the specified elements. 175 176 ```hcl 177 constraint { 178 attribute = "..." 179 operator = "set_contains" 180 value = "a,b,c" 181 } 182 ``` 183 184 - `"set_contains_any"` - Specifies a contains constraint against the attribute. The 185 attribute and the list being checked are split using commas. This will check 186 that the given attribute contains **any** of the specified elements. 187 188 ```hcl 189 constraint { 190 attribute = "..." 191 operator = "set_contains_any" 192 value = "a,b,c" 193 } 194 ``` 195 196 - `"version"` - Specifies a version constraint against the attribute. This 197 supports a comma-separated list of constraints, including the pessimistic 198 operator. `version` will not consider a prerelease (eg `1.6.0-beta`) 199 sufficient to match a non-prerelease constraint (eg `>= 1.0`). Use the 200 `semver` constraint for strict [Semantic Versioning 2.0][semver2] ordering. 201 For more examples please see the [go-version 202 repository](https://github.com/hashicorp/go-version) for more specific 203 examples. 204 205 ```hcl 206 constraint { 207 attribute = "..." 208 operator = "version" 209 value = ">= 0.1.0, < 0.2" 210 } 211 ``` 212 213 - `"semver"` - Specifies a version constraint against the attribute. Only 214 [Semantic Versioning 2.0][semver2] compliant versions and comparison 215 operators are supported, so there is no pessimistic operator. Unlike `version`, 216 this operator considers prereleases (eg `1.6.0-beta`) sufficient to satisfy 217 non-prerelease constraints (eg `>= 1.0`). _Added in Nomad v0.10.2._ 218 219 ```hcl 220 constraint { 221 attribute = "..." 222 operator = "semver" 223 value = ">= 0.1.0, < 0.2" 224 } 225 ``` 226 227 - `"is_set"` - Specifies that a given attribute must be present. This can be 228 combined with the `"!="` operator to require that an attribute has been set 229 before checking for equality. The default behavior for `"!="` is to include 230 nodes that don't have that attribute set. 231 232 - `"is_not_set"` - Specifies that a given attribute must not be present. 233 234 ## `constraint` Examples 235 236 The following examples only show the `constraint` stanzas. Remember that the 237 `constraint` stanza is only valid in the placements listed above. 238 239 ### Kernel Data 240 241 This example restricts the task to running on nodes which have a kernel version 242 higher than "3.19". 243 244 ```hcl 245 constraint { 246 attribute = "${attr.kernel.version}" 247 operator = "version" 248 value = "> 3.19" 249 } 250 ``` 251 252 ### Distinct Property 253 254 A potential use case of the `distinct_property` constraint is to spread a 255 service with `count > 1` across racks to minimize correlated failure. Nodes can 256 be annotated with which rack they are on using [custom client 257 metadata][client-meta] with values such as "rack-12-1", "rack-12-2", etc. 258 The following constraint would assure that an individual rack is not running 259 more than 2 instances of the task group. 260 261 ```hcl 262 constraint { 263 distinct_property = "${meta.rack}" 264 value = "2" 265 } 266 ``` 267 268 ### Operating Systems 269 270 This example restricts the task to running on nodes that are running Ubuntu 271 14.04 272 273 ```hcl 274 constraint { 275 attribute = "${attr.os.name}" 276 value = "ubuntu" 277 } 278 279 constraint { 280 attribute = "${attr.os.version}" 281 value = "14.04" 282 } 283 ``` 284 285 ### Cloud Metadata 286 287 When possible, Nomad populates node attributes from the cloud environment. These 288 values are accessible as filters in constraints. This example constrains this 289 task to only run on nodes that are memory-optimized on AWS. 290 291 ```hcl 292 constraint { 293 attribute = "${attr.platform.aws.instance-type}" 294 value = "m4.xlarge" 295 } 296 ``` 297 298 ### User-Specified Metadata 299 300 This example restricts the task to running on nodes where the binaries for 301 redis, cypress, and nginx are all cached locally. This particular example is 302 utilizing [custom client metadata][client-meta]. 303 304 ```hcl 305 constraint { 306 attribute = "${meta.cached_binaries}" 307 set_contains = "redis,cypress,nginx" 308 } 309 ``` 310 311 [job]: /docs/job-specification/job 'Nomad job Job Specification' 312 [group]: /docs/job-specification/group 'Nomad group Job Specification' 313 [client-meta]: /docs/configuration/client#meta 'Nomad meta Job Specification' 314 [task]: /docs/job-specification/task 'Nomad task Job Specification' 315 [interpolation]: /docs/runtime/interpolation 'Nomad interpolation' 316 [node-variables]: /docs/runtime/interpolation#node-variables- 'Nomad interpolation-Node variables' 317 [client-meta]: /docs/configuration/client#custom-metadata-network-speed-and-node-class 'Nomad Custom Metadata, Network Speed, and Node Class' 318 [semver2]: https://semver.org/spec/v2.0.0.html 'Semantic Versioning 2.0'