github.com/zhizhiboom/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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 ``` 89 90 For a detailed explanation of these values and their behavior, please see 91 the [operator values section](#operator-values). 92 93 - `value` `(string: "")` - Specifies the value to compare the attribute against 94 using the specified operation. This can be a literal value, another attribute, 95 or any [Nomad interpolated 96 values](/docs/runtime/interpolation.html#interpreted_node_vars). 97 98 ### `operator` Values 99 100 This section details the specific values for the "operator" parameter in the 101 Nomad job specification for constraints. The operator is always specified as a 102 string, but the string can take on different values which change the behavior of 103 the overall constraint evaluation. 104 105 ```hcl 106 constraint { 107 operator = "..." 108 } 109 ``` 110 111 - `"distinct_hosts"` - Instructs the scheduler to not co-locate any groups on 112 the same machine. When specified as a job constraint, it applies to all groups 113 in the job. When specified as a group constraint, the effect is constrained to 114 that group. This constraint can not be specified at the task level. Note that 115 the `attribute` parameter should be omitted when using this constraint. 116 117 ```hcl 118 constraint { 119 operator = "distinct_hosts" 120 value = "true" 121 } 122 ``` 123 124 The constraint may also be specified as follows for a more compact 125 representation: 126 127 ```hcl 128 constraint { 129 distinct_hosts = true 130 } 131 ``` 132 133 - `"distinct_property"` - Instructs the scheduler to select nodes that have a 134 distinct value of the specified property. The `value` parameter specifies how 135 many allocations are allowed to share the value of a property. The `value` 136 must be 1 or greater and if omitted, defaults to 1. When specified as a job 137 constraint, it applies to all groups in the job. When specified as a group 138 constraint, the effect is constrained to that group. This constraint can not 139 be specified at the task level. 140 141 ```hcl 142 constraint { 143 operator = "distinct_property" 144 attribute = "${meta.rack}" 145 value = "3" 146 } 147 ``` 148 149 The constraint may also be specified as follows for a more compact 150 representation: 151 152 ```hcl 153 constraint { 154 distinct_property = "${meta.rack}" 155 value = "3" 156 } 157 ``` 158 159 - `"regexp"` - Specifies a regular expression constraint against the attribute. 160 The syntax of the regular expressions accepted is the same general syntax used 161 by Perl, Python, and many other languages. More precisely, it is the syntax 162 accepted by RE2 and described at in the [Google RE2 163 syntax](https://golang.org/s/re2syntax). 164 165 ```hcl 166 constraint { 167 attribute = "..." 168 operator = "regexp" 169 value = "[a-z0-9]" 170 } 171 ``` 172 173 - `"set_contains"` - Specifies a contains constraint against the attribute. The 174 attribute and the list being checked are split using commas. This will check 175 that the given attribute contains **all** of the specified elements. 176 177 ```hcl 178 constraint { 179 attribute = "..." 180 operator = "set_contains" 181 value = "a,b,c" 182 } 183 ``` 184 185 - `"version"` - Specifies a version constraint against the attribute. This 186 supports a comma-separated list of constraints, including the pessimistic 187 operator. For more examples please see the [go-version 188 repository](https://github.com/hashicorp/go-version) for more specific 189 examples. 190 191 ```hcl 192 constraint { 193 attribute = "..." 194 operator = "version" 195 value = ">= 0.1.0, < 0.2" 196 } 197 ``` 198 199 ## `constraint` Examples 200 201 The following examples only show the `constraint` stanzas. Remember that the 202 `constraint` stanza is only valid in the placements listed above. 203 204 ### Kernel Data 205 206 This example restricts the task to running on nodes which have a kernel version 207 higher than "3.19". 208 209 ```hcl 210 constraint { 211 attribute = "${attr.kernel.version}" 212 operator = "version" 213 value = "> 3.19" 214 } 215 ``` 216 217 ### Distinct Property 218 219 A potential use case of the `distinct_property` constraint is to spread a 220 service with `count > 1` across racks to minimize correlated failure. Nodes can 221 be annotated with which rack they are on using [client 222 metadata][client-meta] with values such as "rack-12-1", "rack-12-2", etc. 223 The following constraint would assure that an individual rack is not running 224 more than 2 instances of the task group. 225 226 ```hcl 227 constraint { 228 distinct_property = "${meta.rack}" 229 value = "2" 230 } 231 ``` 232 233 ### Operating Systems 234 235 This example restricts the task to running on nodes that are running Ubuntu 236 14.04 237 238 ```hcl 239 constraint { 240 attribute = "${attr.os.name}" 241 value = "ubuntu" 242 } 243 244 constraint { 245 attribute = "${attr.os.version}" 246 value = "14.04" 247 } 248 ``` 249 250 ### Cloud Metadata 251 252 When possible, Nomad populates node attributes from the cloud environment. These 253 values are accessible as filters in constraints. This example constrains this 254 task to only run on nodes that are memory-optimized on AWS. 255 256 ```hcl 257 constraint { 258 attribute = "${attr.platform.aws.instance-type}" 259 value = "m4.xlarge" 260 } 261 ``` 262 263 ### User-Specified Metadata 264 265 This example restricts the task to running on nodes where the binaries for 266 redis, cypress, and nginx are all cached locally. This particular example is 267 utilizing node [metadata][meta]. 268 269 ```hcl 270 constraint { 271 attribute = "${meta.cached_binaries}" 272 set_contains = "redis,cypress,nginx" 273 } 274 ``` 275 276 [job]: /docs/job-specification/job.html "Nomad job Job Specification" 277 [group]: /docs/job-specification/group.html "Nomad group Job Specification" 278 [client-meta]: /docs/configuration/client.html#meta "Nomad meta Job Specification" 279 [task]: /docs/job-specification/task.html "Nomad task Job Specification" 280 [interpolation]: /docs/runtime/interpolation.html "Nomad interpolation" 281 [node-variables]: /docs/runtime/interpolation.html#node-variables- "Nomad interpolation-Node variables"