2016-10-31 04:00:35 +00:00
|
|
|
---
|
2020-02-06 23:45:31 +00:00
|
|
|
layout: docs
|
|
|
|
page_title: service Stanza - Job Specification
|
|
|
|
sidebar_title: service
|
2016-10-31 04:00:35 +00:00
|
|
|
description: |-
|
2016-10-31 20:46:08 +00:00
|
|
|
The "service" stanza instructs Nomad to register the task as a service using
|
|
|
|
the service discovery integration.
|
2016-10-31 04:00:35 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# `service` Stanza
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
<Placement
|
|
|
|
groups={[
|
|
|
|
['job', 'group', 'service'],
|
2020-09-30 13:48:40 +00:00
|
|
|
['job', 'group', 'task', 'service'],
|
2020-02-06 23:45:31 +00:00
|
|
|
]}
|
|
|
|
/>
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2019-09-06 21:13:05 +00:00
|
|
|
The `service` stanza instructs Nomad to register a service with Consul. This
|
|
|
|
section of the documentation will discuss the configuration, but please also
|
|
|
|
read the [Nomad service discovery documentation][service-discovery] for more
|
|
|
|
detailed information about the integration.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "docs" {
|
|
|
|
group "example" {
|
|
|
|
task "server" {
|
|
|
|
service {
|
|
|
|
tags = ["leader", "mysql"]
|
|
|
|
|
2016-10-31 20:47:08 +00:00
|
|
|
port = "db"
|
|
|
|
|
2019-08-23 16:49:02 +00:00
|
|
|
meta {
|
|
|
|
meta = "for your service"
|
|
|
|
}
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
check {
|
|
|
|
type = "tcp"
|
|
|
|
port = "db"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
|
|
|
|
check {
|
|
|
|
type = "script"
|
|
|
|
name = "check_table"
|
|
|
|
command = "/usr/local/bin/check_mysql_table_status"
|
|
|
|
args = ["--verbose"]
|
|
|
|
interval = "60s"
|
|
|
|
timeout = "5s"
|
2017-09-11 04:38:31 +00:00
|
|
|
|
|
|
|
check_restart {
|
|
|
|
limit = 3
|
|
|
|
grace = "90s"
|
|
|
|
ignore_warnings = false
|
|
|
|
}
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-09-06 21:13:05 +00:00
|
|
|
This section of the documentation only cover the job file fields and stanzas
|
|
|
|
for service discovery. For more details on using Nomad with Consul please see
|
|
|
|
the [Consul integration documentation][service-discovery].
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2019-09-09 03:39:30 +00:00
|
|
|
Nomad 0.10 also allows specifying the `service` stanza at the task group level.
|
2019-10-22 22:57:14 +00:00
|
|
|
This enables services in the same task group to opt into [Consul
|
|
|
|
Connect][connect] integration.
|
2019-09-09 03:39:30 +00:00
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
## `service` Parameters
|
|
|
|
|
2016-10-31 20:47:57 +00:00
|
|
|
- `check` <code>([Check](#check-parameters): nil)</code> - Specifies a health
|
|
|
|
check associated with the service. This can be specified multiple times to
|
|
|
|
define multiple checks for the service. At this time, Nomad supports the
|
2018-05-03 23:45:07 +00:00
|
|
|
`grpc`, `http`, `script`<sup><small>1</small></sup>, and `tcp` checks.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2019-10-22 22:57:14 +00:00
|
|
|
- `connect` - Configures the [Consul Connect][connect] integration. Only
|
|
|
|
available on group services.
|
|
|
|
|
2018-01-17 20:10:41 +00:00
|
|
|
- `name` `(string: "<job>-<group>-<task>")` - Specifies the name this service
|
2020-02-06 23:45:31 +00:00
|
|
|
will be advertised as in Consul. If not supplied, this will default to the
|
2018-01-17 20:10:41 +00:00
|
|
|
name of the job, group, and task concatenated together with a dash, like
|
|
|
|
`"docs-example-server"`. Each service must have a unique name within the
|
|
|
|
cluster. Names must adhere to [RFC-1123
|
|
|
|
§2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited to
|
|
|
|
alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64
|
2016-10-31 04:00:35 +00:00
|
|
|
characters in length.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
In addition to the standard [Nomad interpolation][interpolation], the
|
|
|
|
following keys are also available:
|
|
|
|
|
|
|
|
- `${JOB}` - the name of the job
|
|
|
|
- `${GROUP}` - the name of the group
|
|
|
|
- `${TASK}` - the name of the task
|
|
|
|
- `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}`
|
|
|
|
|
|
|
|
Validation of the name occurs in two parts. When the job is registered, an initial validation pass checks that
|
|
|
|
the service name adheres to RFC-1123 §2.1 and the length limit, excluding any variables requiring interpolation.
|
|
|
|
Once the client receives the service and all interpretable values are available, the service name will be
|
|
|
|
interpolated and revalidated. This can cause certain service names to pass validation at submit time but fail
|
|
|
|
at runtime.
|
|
|
|
|
2018-03-27 21:58:08 +00:00
|
|
|
- `port` `(string: <optional>)` - Specifies the port to advertise for this
|
|
|
|
service. The value of `port` depends on which [`address_mode`](#address_mode)
|
|
|
|
is being used:
|
|
|
|
|
2021-01-07 13:53:54 +00:00
|
|
|
- `alloc` - Advertise the mapped `to` value of the labeled port and the allocation address.
|
|
|
|
If a `to` value is not set, the port falls back to using the allocated host port. The `port`
|
2021-01-06 18:52:48 +00:00
|
|
|
field may be a numeric port or a port label specified in the same group's network stanza.
|
|
|
|
|
2020-12-07 20:41:44 +00:00
|
|
|
- `driver` - Advertise the port determined by the driver (e.g. Docker or rkt).
|
2018-03-27 21:58:08 +00:00
|
|
|
The `port` may be a numeric port or a port label specified in the driver's
|
2021-01-06 18:52:48 +00:00
|
|
|
`ports` field.
|
2018-03-27 21:58:08 +00:00
|
|
|
|
|
|
|
- `host` - Advertise the host port for this service. `port` must match a port
|
|
|
|
_label_ specified in the [`network`][network] stanza.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
- `tags` `(array<string>: [])` - Specifies the list of tags to associate with
|
|
|
|
this service. If this is not supplied, no tags will be assigned to the service
|
|
|
|
when it is registered.
|
|
|
|
|
2018-05-23 20:07:47 +00:00
|
|
|
- `canary_tags` `(array<string>: [])` - Specifies the list of tags to associate with
|
|
|
|
this service when the service is part of an allocation that is currently a
|
|
|
|
canary. Once the canary is promoted, the registered tags will be updated to
|
|
|
|
those specified in the `tags` parameter. If this is not supplied, the
|
2019-11-13 03:27:54 +00:00
|
|
|
registered tags will be equal to that of the `tags` parameter.
|
2018-05-23 20:07:47 +00:00
|
|
|
|
2020-02-13 19:23:51 +00:00
|
|
|
- `enable_tag_override` `(bool: false)` - Enables users of Consul's Catalog API
|
|
|
|
to make changes to the tags of a service without having those changes be
|
|
|
|
overwritten by Consul's anti-entropy mechanism. See Consul
|
2020-09-29 16:48:32 +00:00
|
|
|
[documentation](https://www.consul.io/docs/internals/anti-entropy#enable-tag-override)
|
2020-02-13 19:23:51 +00:00
|
|
|
for more information.
|
|
|
|
|
2020-12-07 20:41:44 +00:00
|
|
|
- `address_mode` `(string: "auto")` - Specifies which address (host, alloc or
|
2020-02-06 23:45:31 +00:00
|
|
|
driver-specific) this service should advertise. This setting is supported in
|
2017-12-08 20:39:50 +00:00
|
|
|
Docker since Nomad 0.6 and rkt since Nomad 0.7. See [below for
|
|
|
|
examples.](#using-driver-address-mode) Valid options are:
|
|
|
|
|
2020-10-15 19:32:21 +00:00
|
|
|
- `alloc` - For allocations which create a network namespace, this address mode
|
|
|
|
uses the IP address inside the namespace. Can only be used with "bridge" and "cni"
|
|
|
|
[networking modes][network_mode]. A numeric port may be specified for situations
|
|
|
|
where no port mapping is necessary. This mode can only be set for services which
|
|
|
|
are defined in a "group" block.
|
|
|
|
|
2017-12-08 20:39:50 +00:00
|
|
|
- `auto` - Allows the driver to determine whether the host or driver address
|
|
|
|
should be used. Defaults to `host` and only implemented by Docker. If you
|
|
|
|
use a Docker network plugin such as weave, Docker will automatically use
|
|
|
|
its address.
|
|
|
|
|
|
|
|
- `driver` - Use the IP specified by the driver, and the port specified in a
|
|
|
|
port map. A numeric port may be specified since port maps aren't required
|
|
|
|
by all network plugins. Useful for advertising SDN and overlay network
|
|
|
|
addresses. Task will fail if driver network cannot be determined. Only
|
2020-10-15 19:32:21 +00:00
|
|
|
implemented for Docker and rkt. This mode can only be set for services
|
|
|
|
which are defined in a "task" block.
|
2017-12-08 20:39:50 +00:00
|
|
|
|
|
|
|
- `host` - Use the host IP and port.
|
2020-02-06 23:45:31 +00:00
|
|
|
|
2020-12-07 20:41:44 +00:00
|
|
|
- `task` `(string: "")` - Specifies the name of the Nomad task associated with
|
2020-06-22 17:55:59 +00:00
|
|
|
this service definition. Only available on group services. Must be set if this
|
2020-12-07 20:41:44 +00:00
|
|
|
service definition represents a Consul Connect-native service and there is more
|
2020-07-08 15:19:36 +00:00
|
|
|
than one task in the task group.
|
2020-06-22 17:55:59 +00:00
|
|
|
|
2019-08-23 16:49:02 +00:00
|
|
|
- `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates
|
|
|
|
the Consul service with user-defined metadata.
|
2017-06-20 00:17:40 +00:00
|
|
|
|
2019-11-13 03:27:54 +00:00
|
|
|
- `canary_meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that
|
|
|
|
annotates the Consul service with user-defined metadata when the service is
|
|
|
|
part of an allocation that is currently a canary. Once the canary is
|
|
|
|
promoted, the registered meta will be updated to those specified in the
|
2020-01-08 15:18:07 +00:00
|
|
|
`meta` parameter. If this is not supplied, the registered meta will be set to
|
2019-11-13 03:27:54 +00:00
|
|
|
that of the `meta` parameter.
|
|
|
|
|
2021-01-22 19:45:26 +00:00
|
|
|
- `on_update` `(string: "require_healthy")` - Specifies how checks should be
|
|
|
|
evaluated when determining deployment health (including a job's initial
|
|
|
|
deployment). This allows job submitters to define certain checks as readiness
|
|
|
|
checks, progressing a deployment even if the Service's checks are not yet
|
2021-02-08 18:36:59 +00:00
|
|
|
healthy. Checks inherit the Service's value by default. The check status is
|
|
|
|
not altered in Consul and is only used to determine the check's health during
|
2021-02-04 15:18:03 +00:00
|
|
|
an update.
|
2021-01-22 19:45:26 +00:00
|
|
|
|
|
|
|
- `require_healthy` - In order for Nomad to consider the check healthy during
|
|
|
|
an update it must report as healthy.
|
|
|
|
|
|
|
|
- `ignore_warnings` - If a Service Check reports as warning, Nomad will treat
|
|
|
|
the check as healthy. The Check will still be in a warning state in Consul.
|
|
|
|
|
|
|
|
- `ignore` - Any status will be treated as healthy.
|
|
|
|
|
|
|
|
~> **Caveat:** `on_update` is only compatible with certain
|
2021-02-04 15:18:03 +00:00
|
|
|
[`check_restart`][check_restart_stanza] configurations. `on_update =
|
|
|
|
"ignore_warnings"` requires that `check_restart.ignore_warnings = true`.
|
|
|
|
`check_restart` can however specify `ignore_warnings = true` with `on_update
|
|
|
|
= "require_healthy"`. If `on_update` is set to `ignore`, `check_restart` must
|
|
|
|
be omitted entirely.
|
2021-01-22 19:45:26 +00:00
|
|
|
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
### `check` Parameters
|
|
|
|
|
2017-06-29 08:54:23 +00:00
|
|
|
Note that health checks run inside the task. If your task is a Docker container,
|
|
|
|
the script will run inside the Docker container. If your task is running in a
|
|
|
|
chroot, it will run in the chroot. Please keep this in mind when authoring check
|
|
|
|
scripts.
|
|
|
|
|
2017-12-07 00:02:24 +00:00
|
|
|
- `address_mode` `(string: "host")` - Same as `address_mode` on `service`.
|
|
|
|
Unlike services, checks do not have an `auto` address mode as there's no way
|
|
|
|
for Nomad to know which is the best address to use for checks. Consul needs
|
2017-12-08 05:32:52 +00:00
|
|
|
access to the address for any HTTP or TCP checks. Added in Nomad 0.7.1. See
|
2017-12-08 20:39:50 +00:00
|
|
|
[below for details.](#using-driver-address-mode) Unlike `port`, this setting
|
2020-02-06 23:45:31 +00:00
|
|
|
is _not_ inherited from the `service`.
|
2017-12-07 00:02:24 +00:00
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `args` `(array<string>: [])` - Specifies additional arguments to the
|
|
|
|
`command`. This only applies to script-based health checks.
|
|
|
|
|
2017-09-14 23:44:27 +00:00
|
|
|
- `check_restart` - See [`check_restart` stanza][check_restart_stanza].
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `command` `(string: <varies>)` - Specifies the command to run for performing
|
|
|
|
the health check. The script must exit: 0 for passing, 1 for warning, or any
|
|
|
|
other value for a failing health check. This is required for script-based
|
|
|
|
health checks.
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
~> **Caveat:** The command must be the path to the command on disk, and no
|
|
|
|
shell exists by default. That means operators like `||` or `&&` are not
|
|
|
|
available. Additionally, all arguments must be supplied via the `args`
|
|
|
|
parameter. To achieve the behavior of shell operators, specify the command
|
|
|
|
as a shell, like `/bin/bash` and then use `args` to run the check.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2018-05-03 23:45:07 +00:00
|
|
|
- `grpc_service` `(string: <optional>)` - What service, if any, to specify in
|
|
|
|
the gRPC health check. gRPC health checks require Consul 1.0.5 or later.
|
|
|
|
|
|
|
|
- `grpc_use_tls` `(bool: false)` - Use TLS to perform a gRPC health check. May
|
|
|
|
be used with `tls_skip_verify` to use TLS but skip certificate verification.
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `initial_status` `(string: <enum>)` - Specifies the originating status of the
|
|
|
|
service. Valid options are the empty string, `passing`, `warning`, and
|
|
|
|
`critical`.
|
|
|
|
|
2020-08-10 19:05:41 +00:00
|
|
|
- `success_before_passing` `(int:0)` - The number of consecutive successful checks
|
|
|
|
required before Consul will transition the service status to [`passing`][consul_passfail].
|
2020-08-08 01:22:06 +00:00
|
|
|
|
2020-08-10 19:05:41 +00:00
|
|
|
- `failures_before_critical` `(int:0)` - The number of consecutive failing checks
|
|
|
|
required before Consul will transition the service status to [`critical`][consul_passfail].
|
2020-08-08 01:22:06 +00:00
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `interval` `(string: <required>)` - Specifies the frequency of the health checks
|
|
|
|
that Consul will perform. This is specified using a label suffix like "30s"
|
2020-12-07 20:41:44 +00:00
|
|
|
or "1h". This must be greater than or equal to "1s".
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2017-08-17 23:05:19 +00:00
|
|
|
- `method` `(string: "GET")` - Specifies the HTTP method to use for HTTP
|
|
|
|
checks.
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `name` `(string: "service: <name> check")` - Specifies the name of the health
|
2018-02-12 17:21:50 +00:00
|
|
|
check. If the name is not specified Nomad generates one based on the service name.
|
|
|
|
If you have more than one check you must specify the name.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2016-10-31 20:48:04 +00:00
|
|
|
- `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which
|
2016-10-31 04:00:35 +00:00
|
|
|
Consul will query to query the health of a service. Nomad will automatically
|
|
|
|
add the IP of the service and the port, so this is just the relative URL to
|
|
|
|
the health check endpoint. This is required for http-based health checks.
|
|
|
|
|
2020-03-20 18:22:14 +00:00
|
|
|
- `expose` `(bool: false)` - Specifies whether an [Expose Path](/docs/job-specification/expose#path-parameters)
|
|
|
|
should be automatically generated for this check. Only compatible with
|
2020-12-18 22:34:47 +00:00
|
|
|
Connect-enabled task-group services using the default Connect proxy. If set, check
|
|
|
|
[`type`][type] must be `http` or `grpc`, and check `name` must be set.
|
2020-03-20 18:22:14 +00:00
|
|
|
|
2017-12-19 00:18:42 +00:00
|
|
|
- `port` `(string: <varies>)` - Specifies the label of the port on which the
|
2016-10-31 04:00:35 +00:00
|
|
|
check will be performed. Note this is the _label_ of the port and not the port
|
2017-12-08 20:39:50 +00:00
|
|
|
number unless `address_mode = driver`. The port label must match one defined
|
|
|
|
in the [`network`][network] stanza. If a port value was declared on the
|
|
|
|
`service`, this will inherit from that value if not supplied. If supplied,
|
|
|
|
this value takes precedence over the `service.port` value. This is useful for
|
2018-05-03 23:45:07 +00:00
|
|
|
services which operate on multiple ports. `grpc`, `http`, and `tcp` checks
|
|
|
|
require a port while `script` checks do not. Checks will use the host IP and
|
|
|
|
ports by default. In Nomad 0.7.1 or later numeric ports may be used if
|
2017-12-08 20:39:50 +00:00
|
|
|
`address_mode="driver"` is set on the check.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
- `protocol` `(string: "http")` - Specifies the protocol for the http-based
|
|
|
|
health checks. Valid options are `http` and `https`.
|
|
|
|
|
2019-09-09 17:26:08 +00:00
|
|
|
- `task` `(string: <required>)` - Specifies the task associated with this
|
2019-09-06 21:13:05 +00:00
|
|
|
check. Scripts are executed within the task's environment, and
|
|
|
|
`check_restart` stanzas will apply to the specified task. For `checks` on group
|
2020-09-28 15:48:28 +00:00
|
|
|
level `services` only. Inherits the [`service.task`][service_task] value if not
|
|
|
|
set.
|
2019-09-06 21:13:05 +00:00
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `timeout` `(string: <required>)` - Specifies how long Consul will wait for a
|
|
|
|
health check query to succeed. This is specified using a label suffix like
|
|
|
|
"30s" or "1h". This must be greater than or equal to "1s"
|
|
|
|
|
2020-05-20 14:28:14 +00:00
|
|
|
~> **Caveat:** Script checks use the task driver to execute in the task's
|
|
|
|
environment. For task drivers with namespace isolation such as `docker` or
|
|
|
|
`exec`, setting up the context for the script check may take an unexpectedly
|
|
|
|
long amount of time (a full second or two), especially on busy hosts. The
|
|
|
|
timeout configuration must allow for both this setup and the execution of
|
|
|
|
the script. Operators should use long timeouts (5 or more seconds) for script
|
|
|
|
checks, and monitor telemetry for
|
|
|
|
`client.allocrunner.taskrunner.tasklet_timeout`.
|
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
- `type` `(string: <required>)` - This indicates the check types supported by
|
2018-05-03 23:45:07 +00:00
|
|
|
Nomad. Valid options are `grpc`, `http`, `script`, and `tcp`. gRPC health
|
|
|
|
checks require Consul 1.0.5 or later.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2017-04-19 19:27:07 +00:00
|
|
|
- `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS
|
|
|
|
checks. Requires Consul >= 0.7.2.
|
|
|
|
|
2021-01-22 19:45:26 +00:00
|
|
|
- `on_update` `(string: "require_healthy")` - Specifies how checks should be
|
2021-02-04 15:18:03 +00:00
|
|
|
evaluated when determining deployment health (including a job's initial
|
2021-01-22 19:45:26 +00:00
|
|
|
deployment). This allows job submitters to define certain checks as readiness
|
|
|
|
checks, progressing a deployment even if the Service's checks are not yet
|
2021-02-08 18:36:59 +00:00
|
|
|
healthy. Checks inherit the Service's value by default. The check status is
|
|
|
|
not altered in Consul and is only used to determine the check's health during
|
2021-02-04 15:18:03 +00:00
|
|
|
an update.
|
2021-01-22 19:45:26 +00:00
|
|
|
|
|
|
|
- `require_healthy` - In order for Nomad to consider the check healthy during
|
|
|
|
an update it must report as healthy.
|
|
|
|
|
|
|
|
- `ignore_warnings` - If a Service Check reports as warning, Nomad will treat
|
|
|
|
the check as healthy. The Check will still be in a warning state in Consul.
|
|
|
|
|
|
|
|
- `ignore` - Any status will be treated as healthy.
|
|
|
|
|
|
|
|
~> **Caveat:** `on_update` is only compatible with certain
|
2021-02-04 15:18:03 +00:00
|
|
|
[`check_restart`][check_restart_stanza] configurations. `on_update =
|
|
|
|
"ignore_warnings"` requires that `check_restart.ignore_warnings = true`.
|
|
|
|
`check_restart` can however specify `ignore_warnings = true` with `on_update
|
|
|
|
= "require_healthy"`. If `on_update` is set to `ignore`, `check_restart` must
|
|
|
|
be omitted entirely.
|
2021-01-22 19:45:26 +00:00
|
|
|
|
2017-08-17 23:05:19 +00:00
|
|
|
#### `header` Stanza
|
|
|
|
|
|
|
|
HTTP checks may include a `header` stanza to set HTTP headers. The `header`
|
|
|
|
stanza parameters have lists of strings as values. Multiple values will cause
|
|
|
|
the header to be set multiple times, once for each value.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
2017-09-11 04:38:31 +00:00
|
|
|
# ...
|
2017-08-17 23:05:19 +00:00
|
|
|
check {
|
|
|
|
type = "http"
|
|
|
|
port = "lb"
|
|
|
|
path = "/_healthz"
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
|
|
|
header {
|
|
|
|
Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-02-05 19:25:45 +00:00
|
|
|
## `service` Lifecycle
|
|
|
|
|
|
|
|
Nomad manages registering, updating, and deregistering services with Consul. It
|
|
|
|
is important to understand when each of these steps happens and how they can be
|
|
|
|
customized.
|
|
|
|
|
2020-09-30 13:48:40 +00:00
|
|
|
**Registration**: Nomad will register `group` services and checks _before_
|
2020-02-19 16:29:58 +00:00
|
|
|
starting any tasks. Services and checks for a specific `task` are registered
|
2020-09-30 13:48:40 +00:00
|
|
|
_after_ the task has started.
|
2020-02-05 19:25:45 +00:00
|
|
|
|
2020-02-19 16:29:58 +00:00
|
|
|
**Updating**: If a service or check definition is updated, Nomad will update
|
|
|
|
the service in Consul as well. Consul is updated without restarting a task.
|
2020-02-05 19:25:45 +00:00
|
|
|
|
|
|
|
**Deregistering**: If a running task with a service stanza exits, the services
|
|
|
|
and checks are immediately deregistered from Consul without delay. If however
|
|
|
|
Nomad needs to kill a running task, the task is killed in the following order:
|
|
|
|
|
|
|
|
1. Immediately remove the services and checks from Consul. This stops new
|
|
|
|
traffic from being routed to the task that is being killed.
|
|
|
|
2. If [`shutdown_delay`][shutdowndelay] is set, wait the configured duration
|
|
|
|
before proceeding to step 3. Setting a [`shutdown_delay`][shutdowndelay] can
|
|
|
|
be useful if the application itself doesn't handle graceful shutdowns based
|
|
|
|
on the [`kill_signal`][killsignal]. The configured delay will provide a
|
|
|
|
period of time in which the service is no longer registered in Consul, and
|
|
|
|
thus is not receiving additional requests, but hasn't been signalled to
|
|
|
|
shutdown. This allows the application time to complete the requests and
|
|
|
|
become idle.
|
|
|
|
3. Send the [`kill_signal`][killsignal] to the task and wait for the task to
|
|
|
|
exit. The task should use this time to gracefully drain and finish any
|
|
|
|
existing requests.
|
2020-02-19 16:29:58 +00:00
|
|
|
4. If the task has not exited after the [`kill_timeout`][killtimeout], Nomad
|
|
|
|
will force kill the application.
|
2020-02-05 19:25:45 +00:00
|
|
|
|
2016-10-31 04:00:35 +00:00
|
|
|
## `service` Examples
|
|
|
|
|
|
|
|
The following examples only show the `service` stanzas. Remember that the
|
|
|
|
`service` stanza is only valid in the placements listed above.
|
|
|
|
|
|
|
|
### Basic Service
|
|
|
|
|
|
|
|
This example registers a service named "load-balancer" with no health checks.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
|
|
|
name = "load-balancer"
|
|
|
|
port = "lb"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This example must be accompanied by a [`network`][network] stanza which defines
|
|
|
|
a static or dynamic port labeled "lb". For example:
|
|
|
|
|
|
|
|
```hcl
|
2021-02-02 13:54:07 +00:00
|
|
|
network {
|
|
|
|
port "lb" {}
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-07-22 10:34:59 +00:00
|
|
|
### Script Checks with Shells
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2019-07-22 10:34:59 +00:00
|
|
|
This example shows a service with a script check that is evaluated and interpolated in a shell; it
|
|
|
|
tests whether a file is present at `${HEALTH_CHECK_FILE}` environment variable:
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
|
|
|
check {
|
|
|
|
type = "script"
|
2019-07-22 10:34:59 +00:00
|
|
|
command = "/bin/bash"
|
|
|
|
args = ["-c", "test -f ${HEALTH_CHECK_FILE}"]
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-07-22 10:34:59 +00:00
|
|
|
Using `/bin/bash` (or another shell) is required here to interpolate the `${HEALTH_CHECK_FILE}` value.
|
|
|
|
|
|
|
|
The following examples of `command` fields **will not work**:
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
```hcl
|
2019-07-22 10:34:59 +00:00
|
|
|
# invalid because command is not a path
|
|
|
|
check {
|
|
|
|
type = "script"
|
|
|
|
command = "test -f /tmp/file.txt"
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 10:34:59 +00:00
|
|
|
# invalid because path will not be interpolated
|
|
|
|
check {
|
|
|
|
type = "script"
|
|
|
|
command = "/bin/test"
|
|
|
|
args = ["-f", "${HEALTH_CHECK_FILE}"]
|
|
|
|
}
|
|
|
|
```
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
### HTTP Health Check
|
|
|
|
|
|
|
|
This example shows a service with an HTTP health check. This will query the
|
|
|
|
service on the IP and port registered with Nomad at `/_healthz` every 5 seconds,
|
2017-08-17 23:05:19 +00:00
|
|
|
giving the service a maximum of 2 seconds to return a response, and include an
|
|
|
|
Authorization header. Any non-2xx code is considered a failure.
|
2016-10-31 04:00:35 +00:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
|
|
|
check {
|
|
|
|
type = "http"
|
|
|
|
port = "lb"
|
|
|
|
path = "/_healthz"
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
2017-08-17 23:05:19 +00:00
|
|
|
header {
|
|
|
|
Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="]
|
|
|
|
}
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Multiple Health Checks
|
|
|
|
|
|
|
|
This example shows a service with multiple health checks defined. All health
|
|
|
|
checks must be passing in order for the service to register as healthy.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
|
|
|
check {
|
2018-02-12 18:19:16 +00:00
|
|
|
name = "HTTP Check"
|
2016-10-31 04:00:35 +00:00
|
|
|
type = "http"
|
|
|
|
port = "lb"
|
|
|
|
path = "/_healthz"
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
|
|
|
|
check {
|
2018-02-12 18:19:16 +00:00
|
|
|
name = "HTTPS Check"
|
2016-11-16 22:44:55 +00:00
|
|
|
type = "http"
|
|
|
|
protocol = "https"
|
2016-10-31 04:00:35 +00:00
|
|
|
port = "lb"
|
|
|
|
path = "/_healthz"
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
2017-08-17 23:05:19 +00:00
|
|
|
method = "POST"
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
check {
|
2021-01-22 19:45:26 +00:00
|
|
|
name = "Postgres Check"
|
|
|
|
type = "script"
|
|
|
|
command = "/usr/local/bin/pg-tools"
|
|
|
|
args = ["verify", "database", "prod", "up"]
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
|
|
|
on_update = "ignore_warnings"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Readiness and Liveness Checks
|
|
|
|
|
|
|
|
Multiple checks for a service can be composed to create liveness and readiness
|
|
|
|
checks by configuring [`on_update`][on_update] for the check.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
2021-02-08 18:36:59 +00:00
|
|
|
# This is a liveness check that will be used to verify the service
|
|
|
|
# is up and able to serve traffic
|
2021-01-22 19:45:26 +00:00
|
|
|
check {
|
|
|
|
name = "tcp_validate"
|
|
|
|
type = "tcp"
|
|
|
|
port = 6379
|
|
|
|
interval = "10s"
|
2016-10-31 04:00:35 +00:00
|
|
|
timeout = "2s"
|
|
|
|
}
|
2021-01-22 19:45:26 +00:00
|
|
|
|
2021-02-08 18:36:59 +00:00
|
|
|
# This is a readiness check that is used to verify that, for example, the
|
|
|
|
# application has elected a leader between allocations. Warnings from
|
|
|
|
# this check will be ignored during updates.
|
2021-01-22 19:45:26 +00:00
|
|
|
check {
|
|
|
|
name = "leader-check"
|
|
|
|
type = "script"
|
|
|
|
command = "/bin/bash"
|
|
|
|
interval = "30s"
|
|
|
|
timeout = "10s"
|
|
|
|
task = "server"
|
|
|
|
on_update = "ignore_warnings"
|
|
|
|
|
|
|
|
args = [
|
|
|
|
"-c",
|
|
|
|
"echo 'service is not the leader'; exit 1;",
|
|
|
|
]
|
|
|
|
}
|
2016-10-31 04:00:35 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-05-03 23:45:07 +00:00
|
|
|
### gRPC Health Check
|
|
|
|
|
|
|
|
gRPC health checks use the same host and port behavior as `http` and `tcp`
|
|
|
|
checks, but gRPC checks also have an optional gRPC service to health check. Not
|
|
|
|
all gRPC applications require a service to health check. gRPC health checks
|
|
|
|
require Consul 1.0.5 or later.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
service {
|
|
|
|
check {
|
|
|
|
type = "grpc"
|
|
|
|
port = "rpc"
|
|
|
|
interval = "5s"
|
|
|
|
timeout = "2s"
|
2018-05-04 18:14:20 +00:00
|
|
|
grpc_service = "example.Service"
|
2018-05-03 23:45:07 +00:00
|
|
|
grpc_use_tls = true
|
|
|
|
tls_skip_verify = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-05-04 18:14:20 +00:00
|
|
|
In this example Consul would health check the `example.Service` service on the
|
2020-02-06 23:45:31 +00:00
|
|
|
`rpc` port defined in the task's [network resources][network] stanza. See
|
2018-05-04 18:14:20 +00:00
|
|
|
[Using Driver Address Mode](#using-driver-address-mode) for details on address
|
2018-05-03 23:45:07 +00:00
|
|
|
selection.
|
|
|
|
|
2017-12-08 05:32:52 +00:00
|
|
|
### Using Driver Address Mode
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
The [Docker](/docs/drivers/docker#network_mode) and
|
|
|
|
[rkt](/docs/drivers/rkt#net) drivers support the `driver` setting for the
|
2017-12-08 05:32:52 +00:00
|
|
|
`address_mode` parameter in both `service` and `check` stanzas. The driver
|
|
|
|
address mode allows advertising and health checking the IP and port assigned to
|
2020-12-07 20:41:44 +00:00
|
|
|
a task by the driver. This way, if you're using a network plugin like Weave with
|
2017-12-08 05:32:52 +00:00
|
|
|
Docker, you can advertise the Weave address in Consul instead of the host's
|
|
|
|
address.
|
|
|
|
|
|
|
|
For example if you were running the example Redis job in an environment with
|
|
|
|
Weave but Consul was running on the host you could use the following
|
|
|
|
configuration:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "example" {
|
|
|
|
datacenters = ["dc1"]
|
2021-02-02 13:54:07 +00:00
|
|
|
|
|
|
|
network {
|
|
|
|
port "db" {
|
|
|
|
to = 6379
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 05:32:52 +00:00
|
|
|
group "cache" {
|
|
|
|
|
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
network_mode = "weave"
|
2021-02-02 13:54:07 +00:00
|
|
|
ports = ["db"]
|
2017-12-08 05:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 500 # 500 MHz
|
|
|
|
memory = 256 # 256MB
|
|
|
|
}
|
|
|
|
|
|
|
|
service {
|
|
|
|
name = "weave-redis"
|
|
|
|
port = "db"
|
|
|
|
check {
|
|
|
|
name = "host-redis-check"
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-03 00:02:03 +00:00
|
|
|
No explicit `address_mode` required.
|
2017-12-08 05:32:52 +00:00
|
|
|
|
|
|
|
Services default to the `auto` address mode. When a Docker network mode other
|
|
|
|
than "host" or "bridge" is used, services will automatically advertise the
|
|
|
|
driver's address (in this case Weave's). The service will advertise the
|
|
|
|
container's port: 6379.
|
|
|
|
|
|
|
|
However since Consul is often run on the host without access to the Weave
|
|
|
|
network, `check` stanzas default to `host` address mode. The TCP check will run
|
|
|
|
against the host's IP and the dynamic host port assigned by Nomad.
|
|
|
|
|
|
|
|
Note that the `check` still inherits the `service` stanza's `db` port label,
|
|
|
|
but each will resolve the port label according to their address mode.
|
|
|
|
|
|
|
|
If Consul has access to the Weave network the job could be configured like
|
|
|
|
this:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "example" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
group "cache" {
|
|
|
|
|
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
network_mode = "weave"
|
2020-12-03 00:02:03 +00:00
|
|
|
# No port map required.
|
2017-12-08 05:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 500 # 500 MHz
|
|
|
|
memory = 256 # 256MB
|
|
|
|
}
|
|
|
|
|
|
|
|
service {
|
|
|
|
name = "weave-redis"
|
|
|
|
port = 6379
|
|
|
|
address_mode = "driver"
|
|
|
|
check {
|
|
|
|
name = "host-redis-check"
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
port = 6379
|
2020-02-06 23:45:31 +00:00
|
|
|
|
2017-12-08 05:32:52 +00:00
|
|
|
address_mode = "driver"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
In this case Nomad doesn't need to assign Redis any host ports. The `service`
|
|
|
|
and `check` stanzas can both specify the port number to advertise and check
|
|
|
|
directly since Nomad isn't managing any port assignments.
|
|
|
|
|
2018-01-31 23:01:25 +00:00
|
|
|
### IPv6 Docker containers
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
The [Docker](/docs/drivers/docker#advertise_ipv6_address) driver supports the
|
2019-05-08 16:54:44 +00:00
|
|
|
`advertise_ipv6_address` parameter in its configuration.
|
2018-01-24 13:39:50 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
Services will automatically advertise the IPv6 address when `advertise_ipv6_address`
|
2018-02-01 13:25:14 +00:00
|
|
|
is used.
|
2018-01-24 13:39:50 +00:00
|
|
|
|
2018-01-31 23:01:25 +00:00
|
|
|
Unlike services, checks do not have an `auto` address mode as there's no way
|
|
|
|
for Nomad to know which is the best address to use for checks. Consul needs
|
|
|
|
access to the address for any HTTP or TCP checks.
|
2018-01-24 13:39:50 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
So you have to set `address_mode` parameter in the `check` stanza to `driver`.
|
2018-01-24 13:39:50 +00:00
|
|
|
|
2018-01-31 23:01:25 +00:00
|
|
|
For example using `auto` address mode:
|
2018-01-24 13:39:50 +00:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "example" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
group "cache" {
|
|
|
|
|
2021-02-02 13:54:07 +00:00
|
|
|
network {
|
|
|
|
port "db" {
|
|
|
|
to = 6379
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-24 13:39:50 +00:00
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
2018-01-31 23:01:25 +00:00
|
|
|
advertise_ipv6_address = true
|
2021-02-02 13:54:07 +00:00
|
|
|
ports = ["db"]
|
2018-01-31 23:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 500 # 500 MHz
|
|
|
|
memory = 256 # 256MB
|
|
|
|
}
|
|
|
|
|
|
|
|
service {
|
|
|
|
name = "ipv6-redis"
|
2019-12-05 11:33:49 +00:00
|
|
|
port = "db"
|
2018-01-31 23:01:25 +00:00
|
|
|
check {
|
|
|
|
name = "ipv6-redis-check"
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
2019-12-05 11:33:49 +00:00
|
|
|
port = "db"
|
2018-01-31 23:01:25 +00:00
|
|
|
address_mode = "driver"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Or using `address_mode=driver` for `service` and `check` with numeric ports:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "example" {
|
|
|
|
datacenters = ["dc1"]
|
|
|
|
group "cache" {
|
|
|
|
|
|
|
|
task "redis" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
config {
|
|
|
|
image = "redis:3.2"
|
|
|
|
advertise_ipv6_address = true
|
2020-12-03 00:02:03 +00:00
|
|
|
# No port map required.
|
2018-01-24 13:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 500 # 500 MHz
|
|
|
|
memory = 256 # 256MB
|
|
|
|
}
|
|
|
|
|
|
|
|
service {
|
|
|
|
name = "ipv6-redis"
|
|
|
|
port = 6379
|
|
|
|
address_mode = "driver"
|
|
|
|
check {
|
|
|
|
name = "ipv6-redis-check"
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
port = 6379
|
|
|
|
address_mode = "driver"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
The `service` and `check` stanzas can both specify the port number to
|
2018-01-31 23:01:25 +00:00
|
|
|
advertise and check directly since Nomad isn't managing any port assignments.
|
2018-01-24 13:39:50 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
---
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2020-02-06 23:45:31 +00:00
|
|
|
<sup>
|
|
|
|
<small>1</small>
|
|
|
|
</sup>
|
|
|
|
<small>
|
|
|
|
{' '}
|
2020-12-07 20:41:44 +00:00
|
|
|
Script checks are not supported for the QEMU driver since the Nomad
|
2020-02-06 23:45:31 +00:00
|
|
|
client does not have access to the file system of a task for that driver.
|
|
|
|
</small>
|
2016-10-31 04:00:35 +00:00
|
|
|
|
2020-09-29 16:48:32 +00:00
|
|
|
[check_restart_stanza]: /docs/job-specification/check_restart
|
|
|
|
[consul_grpc]: https://www.consul.io/api/agent/check#grpc
|
2020-08-08 01:22:06 +00:00
|
|
|
[consul_passfail]: https://www.consul.io/docs/agent/checks#success-failures-before-passing-critical
|
2020-03-20 21:00:59 +00:00
|
|
|
[service-discovery]: /docs/integrations/consul-integration#service-discovery 'Nomad Service Discovery'
|
2020-02-06 23:45:31 +00:00
|
|
|
[interpolation]: /docs/runtime/interpolation 'Nomad Runtime Interpolation'
|
|
|
|
[network]: /docs/job-specification/network 'Nomad network Job Specification'
|
2020-12-03 00:02:03 +00:00
|
|
|
[qemu]: /docs/drivers/qemu 'Nomad QEMU Driver'
|
2020-02-06 23:45:31 +00:00
|
|
|
[restart_stanza]: /docs/job-specification/restart 'restart stanza'
|
|
|
|
[connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration'
|
2020-03-20 18:22:14 +00:00
|
|
|
[type]: /docs/job-specification/service#type
|
2020-03-26 20:21:24 +00:00
|
|
|
[shutdowndelay]: /docs/job-specification/task#shutdown_delay
|
|
|
|
[killsignal]: /docs/job-specification/task#kill_signal
|
|
|
|
[killtimeout]: /docs/job-specification/task#kill_timeout
|
2020-09-28 15:48:28 +00:00
|
|
|
[service_task]: /docs/job-specification/service#task-1
|
2020-12-03 00:02:03 +00:00
|
|
|
[network_mode]: /docs/job-specification/network#mode
|
2021-01-22 19:45:26 +00:00
|
|
|
[on_update]: /docs/job-specification/service#on_update
|