github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/docs/job-specification/constraint.mdx (about)

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