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