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