github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/website/source/docs/job-specification/service.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "service Stanza - Job Specification"
     4  sidebar_current: "docs-job-specification-service"
     5  description: |-
     6    The "service" stanza instructs Nomad to register the task as a service using
     7    the service discovery integration.
     8  ---
     9  
    10  # `service` Stanza
    11  
    12  <table class="table table-bordered table-striped">
    13    <tr>
    14      <th width="120">Placement</th>
    15      <td>
    16        <code>job -> group -> task -> **service**</code>
    17      </td>
    18    </tr>
    19  </table>
    20  
    21  The `service` stanza instructs Nomad to register the task as a service using the
    22  service discovery integration. This section of the documentation will discuss the
    23  configuration, but please also read the
    24  [Nomad service discovery documentation][service-discovery] for more detailed
    25  information about the integration.
    26  
    27  ```hcl
    28  job "docs" {
    29    group "example" {
    30      task "server" {
    31        service {
    32          tags = ["leader", "mysql"]
    33  
    34          port = "db"
    35  
    36          check {
    37            type     = "tcp"
    38            port     = "db"
    39            interval = "10s"
    40            timeout  = "2s"
    41          }
    42  
    43          check {
    44            type     = "script"
    45            name     = "check_table"
    46            command  = "/usr/local/bin/check_mysql_table_status"
    47            args     = ["--verbose"]
    48            interval = "60s"
    49            timeout  = "5s"
    50          }
    51        }
    52      }
    53    }
    54  }
    55  ```
    56  
    57  This section of the documentation only covers the job file options for
    58  configuring service discovery. For more information on the setup and
    59  configuration to integrate Nomad with service discovery, please see the
    60  [Nomad service discovery documentation][service-discovery]. There are steps you
    61  must take to configure Nomad. Simply adding this configuration to your job file
    62  does not automatically enable service discovery.
    63  
    64  ## `service` Parameters
    65  
    66  - `check` <code>([Check](#check-parameters): nil)</code> - Specifies a health
    67    check associated with the service. This can be specified multiple times to
    68    define multiple checks for the service. At this time, Nomad supports the
    69    `script`<sup><small>1</small></sup>, `http` and `tcp` checks.
    70  
    71  - `name` `(string: "<job>-<group>-<task>")` - Specifies the name of this
    72    service. If not supplied, this will default to the name of the job, group, and
    73    task concatenated together with a dash, like `"docs-example-server"`. Each
    74    service must have a unique name within the cluster. Names must adhere to
    75    [RFC-1123 ยง2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited
    76    to alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64
    77    characters in length.
    78  
    79      In addition to the standard [Nomad interpolation][interpolation], the
    80      following keys are also available:
    81  
    82      - `${JOB}` - the name of the job
    83      - `${GROUP}` - the name of the group
    84      - `${TASK}` - the name of the task
    85      - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}`
    86  
    87  - `port` `(string: <required>)` - Specifies the label of the port on which this
    88    service is running. Note this is the _label_ of the port and not the port
    89    number. The port label must match one defined in the [`network`][network]
    90    stanza.
    91  
    92  - `tags` `(array<string>: [])` - Specifies the list of tags to associate with
    93    this service. If this is not supplied, no tags will be assigned to the service
    94    when it is registered.
    95  
    96  ### `check` Parameters
    97  
    98  - `args` `(array<string>: [])` - Specifies additional arguments to the
    99    `command`. This only applies to script-based health checks.
   100  
   101  - `command` `(string: <varies>)` - Specifies the command to run for performing
   102    the health check. The script must exit: 0 for passing, 1 for warning, or any
   103    other value for a failing health check. This is required for script-based
   104    health checks.
   105  
   106      ~> **Caveat:** The command must be the path to the command on disk, and no
   107      shell exists by default. That means operators like `||` or `&&` are not
   108      available. Additionally, all arguments must be supplied via the `args`
   109      parameter. To achieve the behavior of shell operators, specify the command
   110      as a shell, like `/bin/bash` and then use `args` to run the check.
   111  
   112  - `initial_status` `(string: <enum>)` - Specifies the originating status of the
   113    service. Valid options are the empty string, `passing`, `warning`, and
   114    `critical`.
   115  
   116  - `interval` `(string: <required>)` - Specifies the frequency of the health checks
   117    that Consul will perform. This is specified using a label suffix like "30s"
   118    or "1h". This must be greater than or equal to "1s"
   119  
   120  - `name` `(string: "service: <name> check")` - Specifies the name of the health
   121    check.
   122  
   123  - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which
   124    Consul will query to query the health of a service. Nomad will automatically
   125    add the IP of the service and the port, so this is just the relative URL to
   126    the health check endpoint. This is required for http-based health checks.
   127  
   128  - `port` `(string: <required>)` - Specifies the label of the port on which the
   129    check will be performed. Note this is the _label_ of the port and not the port
   130    number. The port label must match one defined in the [`network`][network]
   131    stanza. If a port value was declared on the `service`, this will inherit from
   132    that value if not supplied. If supplied, this value takes precedence over the
   133    `service.port` value. This is useful for services which operate on multiple
   134    ports.
   135  
   136  - `protocol` `(string: "http")` - Specifies the protocol for the http-based
   137    health checks. Valid options are `http` and `https`.
   138  
   139  - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a
   140    health check query to succeed. This is specified using a label suffix like
   141    "30s" or "1h". This must be greater than or equal to "1s"
   142  
   143  - `type` `(string: <required>)` - This indicates the check types supported by
   144    Nomad. Valid options are `script`, `http`, and `tcp`.
   145  
   146  - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS
   147    checks. Requires Consul >= 0.7.2.
   148  
   149  
   150  ## `service` Examples
   151  
   152  The following examples only show the `service` stanzas. Remember that the
   153  `service` stanza is only valid in the placements listed above.
   154  
   155  ### Basic Service
   156  
   157  This example registers a service named "load-balancer" with no health checks.
   158  
   159  ```hcl
   160  service {
   161    name = "load-balancer"
   162    port = "lb"
   163  }
   164  ```
   165  
   166  This example must be accompanied by a [`network`][network] stanza which defines
   167  a static or dynamic port labeled "lb". For example:
   168  
   169  ```hcl
   170  resources {
   171    network {
   172      mbits = 10
   173      port "lb" {}
   174    }
   175  }
   176  ```
   177  
   178  ### Check with Bash-isms
   179  
   180  This example shows a common mistake and correct behavior for custom checks.
   181  Suppose a health check like this:
   182  
   183  ```shell
   184  $ test -f /tmp/file.txt
   185  ```
   186  
   187  In this example `test` is not actually a command (binary) on the system; it is a
   188  built-in shell function to bash. Thus, the following **would not work**:
   189  
   190  ```hcl
   191  service {
   192    check {
   193      type    = "script"
   194      command = "test -f /tmp/file.txt" # THIS IS NOT CORRECT
   195    }
   196  }
   197  ```
   198  
   199  Nomad will attempt to find an executable named `test` on your system, but it
   200  does not exist. It is actually just a function of bash. Additionally, it is not
   201  possible to specify the arguments in a single string. Here is the correct
   202  solution:
   203  
   204  ```hcl
   205  service {
   206    check {
   207      type    = "script"
   208      command = "/bin/bash"
   209      args    = ["-c", "test -f /tmp/file.txt"]
   210    }
   211  }
   212  ```
   213  
   214  The `command` is actually `/bin/bash`, since that is the actual process we are
   215  running. The arguments to that command are the script itself, which each
   216  argument provided as a value to the `args` array.
   217  
   218  ### HTTP Health Check
   219  
   220  This example shows a service with an HTTP health check. This will query the
   221  service on the IP and port registered with Nomad at `/_healthz` every 5 seconds,
   222  giving the service a maximum of 2 seconds to return a response. Any non-2xx code
   223  is considered a failure.
   224  
   225  ```hcl
   226  service {
   227    check {
   228      type     = "http"
   229      port     = "lb"
   230      path     = "/_healthz"
   231      interval = "5s"
   232      timeout  = "2s"
   233    }
   234  }
   235  ```
   236  
   237  ### Multiple Health Checks
   238  
   239  This example shows a service with multiple health checks defined. All health
   240  checks must be passing in order for the service to register as healthy.
   241  
   242  ```hcl
   243  service {
   244    check {
   245      type     = "http"
   246      port     = "lb"
   247      path     = "/_healthz"
   248      interval = "5s"
   249      timeout  = "2s"
   250    }
   251  
   252    check {
   253      type     = "http"
   254      protocol = "https"
   255      port     = "lb"
   256      path     = "/_healthz"
   257      interval = "5s"
   258      timeout  = "2s"
   259    }
   260  
   261    check {
   262      type     = "script"
   263      command  = "/usr/local/bin/pg-tools"
   264      args     = ["verify", "database" "prod", "up"]
   265      interval = "5s"
   266      timeout  = "2s"
   267    }
   268  }
   269  ```
   270  
   271  - - -
   272  
   273  <sup><small>1</small></sup><small> Script checks are not supported for the
   274  [qemu driver][qemu] since the Nomad client does not have access to the file
   275  system of a task for that driver.</small>
   276  
   277  [service-discovery]: /docs/service-discovery/index.html "Nomad Service Discovery"
   278  [interpolation]: /docs/runtime/interpolation.html "Nomad Runtime Interpolation"
   279  [network]: /docs/job-specification/network.html "Nomad network Job Specification"
   280  [qemu]: /docs/drivers/qemu.html "Nomad qemu Driver"