github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/website/source/docs/job-specification/affinity.html.md (about)

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