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

     1  ---
     2  layout: docs
     3  page_title: affinity Stanza - Job Specification
     4  sidebar_title: affinity
     5  description: |-
     6    The "affinity" stanza allows restricting the set of eligible nodes.
     7    Affinities may filter on attributes or metadata. Additionally affinities may
     8    be specified at the job, group, or task levels for ultimate flexibility.
     9  ---
    10  
    11  # `affinity` Stanza
    12  
    13  <Placement
    14    groups={[
    15      ['job', 'affinity'],
    16      ['job', 'group', 'affinity'],
    17      ['job', 'group', 'task', 'affinity']
    18    ]}
    19  />
    20  
    21  The `affinity` stanza allows operators to express placement preference for a set of nodes. Affinities may
    22  be expressed on [attributes][interpolation] or [client metadata][client-meta].
    23  Additionally affinities may be specified at the [job][job], [group][group], or
    24  [task][task] levels for ultimate flexibility.
    25  
    26  ```hcl
    27  job "docs" {
    28    # Prefer nodes in the us-west1 datacenter
    29    affinity {
    30      attribute = "${node.datacenter}"
    31      value     = "us-west1"
    32      weight    = 100
    33    }
    34  
    35    group "example" {
    36      # Prefer the "r1" rack
    37      affinity {
    38        attribute  = "${meta.rack}"
    39        value     = "r1"
    40        weight    = 50
    41      }
    42  
    43      task "server" {
    44        # Prefer nodes where "my_custom_value" is greater than 5
    45        affinity {
    46          attribute = "${meta.my_custom_value}"
    47          operator  = ">"
    48          value     = "3"
    49          weight    = 50
    50        }
    51      }
    52    }
    53  }
    54  ```
    55  
    56  Affinities apply to task groups but may be specified within job and task stanzas as well.
    57  Job affinities apply to all groups within the job. Task affinities apply to the whole task group
    58  that the task is a part of.
    59  
    60  Nomad will use affinities when computing scores for placement. Nodes that match affinities will
    61  have their scores boosted. Affinity scores are combined with other scoring factors such as bin packing.
    62  Operators can use weights to express relative preference across multiple affinities. If no nodes match a given affinity,
    63  placement is still successful. This is different from [constraints][constraint] where placement is
    64  restricted only to nodes that meet the constraint's criteria.
    65  
    66  ## `affinity` Parameters
    67  
    68  - `attribute` `(string: "")` - Specifies the name or reference of the attribute
    69    to examine for the affinity. This can be any of the [Nomad interpolated
    70    values](/docs/runtime/interpolation#interpreted_node_vars).
    71  
    72  - `operator` `(string: "=")` - Specifies the comparison operator. The ordering is
    73    compared lexically. Possible values include:
    74  
    75    ```text
    76    =
    77    !=
    78    >
    79    >=
    80    <
    81    <=
    82    regexp
    83    set_contains_all
    84    set_contains_any
    85    version
    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  - `weight` `(integer: 50)` - Specifies a weight for the affinity. The weight is used
    97    during scoring and must be an integer between -100 to 100. Negative weights act as
    98    anti affinities, causing nodes that match them to be scored lower. Weights can be used
    99    when there is more than one affinity to express relative preference across them.
   100  
   101  ### `operator` Values
   102  
   103  This section details the specific values for the "operator" parameter in the
   104  Nomad job specification for affinities. The operator is always specified as a
   105  string, but the string can take on different values which change the behavior of
   106  the overall affinity evaluation.
   107  
   108  ```hcl
   109  affinity {
   110    operator = "..."
   111  }
   112  ```
   113  
   114  - `"regexp"` - Specifies a regular expression affinity against the attribute.
   115    The syntax of the regular expressions accepted is the same general syntax used
   116    by Perl, Python, and many other languages. More precisely, it is the syntax
   117    accepted by RE2 and described at in the [Google RE2
   118    syntax](https://golang.org/s/re2syntax).
   119  
   120    ```hcl
   121    affinity {
   122      attribute = "..."
   123      operator  = "regexp"
   124      value     = "[a-z0-9]"
   125      weight    = 50
   126    }
   127    ```
   128  
   129  - `"set_contains_all"` - Specifies a contains affinity against the attribute. The
   130    attribute and the list being checked are split using commas. This will check
   131    that the given attribute contains **all** of the specified elements.
   132  
   133    ```hcl
   134    affinity {
   135      attribute = "..."
   136      operator  = "set_contains"
   137      value     = "a,b,c"
   138      weight    = 50
   139    }
   140    ```
   141  
   142  - `"set_contains"` - Same as `set_contains_all`
   143  
   144  - `"set_contains_any"` - Specifies a contains affinity against the attribute. The
   145    attribute and the list being checked are split using commas. This will check
   146    that the given attribute contains **any** of the specified elements.
   147  
   148    ```hcl
   149    affinity {
   150      attribute = "..."
   151      operator  = "set_contains"
   152      value     = "a,b,c"
   153      weight    = 50
   154    }
   155    ```
   156  
   157  - `"version"` - Specifies a version affinity against the attribute. This
   158    supports a comma-separated list of values, including the pessimistic
   159    operator. For more examples please see the [go-version
   160    repository](https://github.com/hashicorp/go-version) for more specific
   161    examples.
   162  
   163    ```hcl
   164    affinity {
   165      attribute = "..."
   166      operator  = "version"
   167      value     = ">= 0.1.0, < 0.2"
   168      weight    = 50
   169    }
   170    ```
   171  
   172  ## `affinity` Examples
   173  
   174  The following examples only show the `affinity` stanzas. Remember that the
   175  `affinity` stanza is only valid in the placements listed above.
   176  
   177  ### Kernel Data
   178  
   179  This example adds a preference for running on nodes which have a kernel version
   180  higher than "3.19".
   181  
   182  ```hcl
   183  affinity {
   184    attribute = "${attr.kernel.version}"
   185    operator  = "version"
   186    value     = "> 3.19"
   187    weight    = 50
   188  }
   189  ```
   190  
   191  ### Operating Systems
   192  
   193  This example adds a preference to running on nodes that are running Ubuntu
   194  14.04
   195  
   196  ```hcl
   197  affinity {
   198    attribute = "${attr.os.name}"
   199    value     = "ubuntu"
   200    weight    = 50
   201  }
   202  
   203  affinity {
   204    attribute = "${attr.os.version}"
   205    value     = "14.04"
   206    weight    = 100
   207  }
   208  ```
   209  
   210  ### Meta Data
   211  
   212  The following example adds a preference to running on nodes with specific rack metadata
   213  
   214  ```hcl
   215  affinity {
   216    attribute = "${meta.rack}"
   217    value     = "rack1"
   218    weight    = 50
   219  }
   220  ```
   221  
   222  The following example adds a preference to running on nodes in a specific datacenter.
   223  
   224  ```hcl
   225  affinity {
   226    attribute = "${node.datacenter}"
   227    value     = "us-west1"
   228    weight    = 50
   229  }
   230  ```
   231  
   232  ### Cloud Metadata
   233  
   234  When possible, Nomad populates node attributes from the cloud environment. These
   235  values are accessible as filters in affinities. This example adds a preference to run this
   236  task on nodes that are memory-optimized on AWS.
   237  
   238  ```hcl
   239  affinity {
   240    attribute = "${attr.platform.aws.instance-type}"
   241    value     = "m4.xlarge"
   242    weight    = 50
   243  }
   244  ```
   245  
   246  [job]: /docs/job-specification/job 'Nomad job Job Specification'
   247  [group]: /docs/job-specification/group 'Nomad group Job Specification'
   248  [client-meta]: /docs/configuration/client#meta 'Nomad meta Job Specification'
   249  [task]: /docs/job-specification/task 'Nomad task Job Specification'
   250  [interpolation]: /docs/runtime/interpolation 'Nomad interpolation'
   251  [node-variables]: /docs/runtime/interpolation#node-variables- 'Nomad interpolation-Node variables'
   252  [constraint]: /docs/job-specification/constraint 'Nomad Constraint job Specification'
   253  
   254  ### Placement Details
   255  
   256  Operators can run `nomad alloc status -verbose` to get more detailed information on various
   257  factors, including affinities that affect the final placement.
   258  
   259  #### Example Placement Metadata
   260  
   261  The following is a snippet from the CLI output of `nomad alloc status -verbose <alloc-id>` showing scoring metadata.
   262  
   263  ```text
   264  Placement Metrics
   265  Node                                  binpack  job-anti-affinity  node-reschedule-penalty  node-affinity  final score
   266  30bd48cc-d760-1096-9bab-13caac424af5  0.225    -0.6               0                        1              0.208
   267  f2aa8b59-96b8-202f-2258-d98c93e360ab  0.225    -0.6               0                        1              0.208
   268  86df0f74-15cc-3a0e-23f0-ad7306131e0d  0.0806   0                  0                        0              0.0806
   269  7d6c2e9e-b080-5995-8b9d-ef1695458b52  0.0806   0                  0                        0              0.0806
   270  ```
   271  
   272  The placement score is affected by the following factors.
   273  
   274  - `bin-packing` - Scores nodes according to how well they fit requirements. Optimizes for using minimal number of nodes.
   275  - `job-anti-affinity` - A penalty added for additional instances of the same job on a node, used to avoid having too many instances
   276    of a job on the same node.
   277  - `node-reschedule-penalty` - Used when the job is being rescheduled. Nomad adds a penalty to avoid placing the job on a node where
   278    it has failed to run before.
   279  - `node-affinity` - Used when the criteria specified in the `affinity` stanza matches the node.