2015-11-18 20:14:28 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2016-10-09 07:01:59 +00:00
|
|
|
page_title: "Service Discovery"
|
2016-10-28 00:36:26 +00:00
|
|
|
sidebar_current: "docs-service-discovery"
|
2015-11-18 20:14:28 +00:00
|
|
|
description: |-
|
|
|
|
Learn how to add service discovery to jobs
|
2015-11-18 22:11:23 +00:00
|
|
|
---
|
2015-11-18 20:14:28 +00:00
|
|
|
|
|
|
|
# Service Discovery
|
|
|
|
|
2015-11-18 21:15:47 +00:00
|
|
|
Nomad schedules workloads of various types across a cluster of generic hosts.
|
|
|
|
Because of this, placement is not known in advance and you will need to use
|
|
|
|
service discovery to connect tasks to other services deployed across your
|
2016-10-03 22:23:50 +00:00
|
|
|
cluster. Nomad integrates with [Consul](https://www.consul.io) to provide
|
|
|
|
service discovery and monitoring.
|
2015-11-18 21:15:47 +00:00
|
|
|
|
|
|
|
Note that in order to use Consul with Nomad, you will need to configure and
|
|
|
|
install Consul on your nodes alongside Nomad, or schedule it as a system job.
|
|
|
|
Nomad does not currently run Consul for you.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
2016-06-14 22:21:57 +00:00
|
|
|
To configure Consul integration please see the Agent's configuration
|
|
|
|
[here](/docs/agent/config.html#consul_options).
|
2015-11-25 21:49:31 +00:00
|
|
|
|
2015-11-18 21:15:47 +00:00
|
|
|
## Service Definition Syntax
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-10-11 19:52:50 +00:00
|
|
|
The service block in a task definition defines a service which Nomad will
|
|
|
|
register with Consul. Multiple service blocks are allowed in a task definition,
|
|
|
|
which allow registering multiple services for a task that exposes multiple
|
2015-11-18 21:20:23 +00:00
|
|
|
ports.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2015-11-18 21:20:23 +00:00
|
|
|
### Example
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-10-11 19:52:50 +00:00
|
|
|
A brief example of a service definition in a task
|
2015-11-18 22:22:42 +00:00
|
|
|
|
2016-10-03 22:23:50 +00:00
|
|
|
```hcl
|
2015-11-18 20:14:28 +00:00
|
|
|
group "database" {
|
2016-10-03 22:23:50 +00:00
|
|
|
task "mysql" {
|
|
|
|
driver = "docker"
|
|
|
|
|
|
|
|
service {
|
|
|
|
tags = ["master", "mysql"]
|
|
|
|
|
|
|
|
port = "db"
|
|
|
|
|
|
|
|
check {
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
|
|
|
|
check {
|
|
|
|
type = "script"
|
|
|
|
name = "check_table"
|
|
|
|
command = "/usr/local/bin/check_mysql_table_status"
|
|
|
|
args = ["--verbose"]
|
|
|
|
interval = "60s"
|
|
|
|
timeout = "5s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
cpu = 500
|
|
|
|
memory = 1024
|
|
|
|
|
|
|
|
network {
|
|
|
|
mbits = 10
|
|
|
|
port "db" {}
|
|
|
|
}
|
2015-11-18 20:14:28 +00:00
|
|
|
}
|
2016-10-03 22:23:50 +00:00
|
|
|
}
|
2015-11-18 20:14:28 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-10-11 19:52:50 +00:00
|
|
|
* `Name`: An explicit name for the Service. Nomad will replace `${JOB}`,
|
|
|
|
`${TASKGROUP}` and `${TASK}` by the name of the job, task group or task,
|
|
|
|
respectively. `${BASE}` expands to the equivalent of
|
|
|
|
`${JOB}-${TASKGROUP}-${TASK}`, and is the default name for a Service.
|
|
|
|
Each service defined for a given task must have a distinct name, so if
|
|
|
|
a task has multiple services only one of them can use the default name
|
|
|
|
and the others must be explicitly named. Names must adhere to
|
2016-03-15 00:44:59 +00:00
|
|
|
[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 characters in length.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-03-26 21:45:04 +00:00
|
|
|
* `tags`: A list of tags associated with this Service. String interpolation is
|
|
|
|
supported in tags.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-10-11 19:52:50 +00:00
|
|
|
* `port`: `port` is optional and is used to associate a port with the service.
|
2016-04-14 17:57:42 +00:00
|
|
|
If specified, the port label must match one defined in the resources block.
|
2016-10-11 19:52:50 +00:00
|
|
|
This could be a label of either a dynamic or a static port.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
|
|
|
* `check`: A check block defines a health check associated with the service.
|
2016-03-26 20:02:30 +00:00
|
|
|
Multiple check blocks are allowed for a service. Nomad supports the `script`,
|
|
|
|
`http` and `tcp` Consul Checks. Script checks are not supported for the qemu
|
|
|
|
driver since the Nomad client doesn't have access to the file system of a
|
2016-10-11 19:52:50 +00:00
|
|
|
task using the Qemu driver.
|
2015-11-18 21:20:23 +00:00
|
|
|
|
|
|
|
### Check Syntax
|
2015-11-18 20:14:28 +00:00
|
|
|
|
|
|
|
* `type`: This indicates the check types supported by Nomad. Valid options are
|
2016-04-22 18:12:02 +00:00
|
|
|
currently `script`, `http` and `tcp`.
|
|
|
|
|
|
|
|
* `name`: The name of the health check.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-01-28 09:09:59 +00:00
|
|
|
* `interval`: This indicates the frequency of the health checks that Consul will
|
2015-11-18 20:14:28 +00:00
|
|
|
perform.
|
|
|
|
|
|
|
|
* `timeout`: This indicates how long Consul will wait for a health check query
|
|
|
|
to succeed.
|
|
|
|
|
|
|
|
* `path`: The path of the http endpoint which Consul will query to query the
|
2016-01-27 05:09:03 +00:00
|
|
|
health of a service if the type of the check is `http`. Nomad will add the IP
|
|
|
|
of the service and the port, users are only required to add the relative URL
|
2015-11-18 20:14:28 +00:00
|
|
|
of the health check endpoint.
|
|
|
|
|
2015-11-18 21:20:23 +00:00
|
|
|
* `protocol`: This indicates the protocol for the http checks. Valid options
|
2016-10-11 19:52:50 +00:00
|
|
|
are `http` and `https`. We default it to `http`.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-10-19 18:12:11 +00:00
|
|
|
* `port`: The label of the port on which the check will be performed. The label
|
|
|
|
specified here has to be defined in the resource block of the task.
|
|
|
|
|
2016-03-28 21:25:15 +00:00
|
|
|
* `command`: This is the command that the Nomad client runs for doing script based
|
2016-03-26 20:02:30 +00:00
|
|
|
health check.
|
|
|
|
|
2016-03-28 21:25:15 +00:00
|
|
|
* `args`: Additional arguments to the `command` for script based health checks.
|
2016-03-26 20:02:30 +00:00
|
|
|
|
2016-10-21 00:49:45 +00:00
|
|
|
* `initial_status`: An optional parameter to set the initial status of the check
|
|
|
|
Valid options are the empty string, `passing`, `warning`, and `critical`.
|
|
|
|
|
2015-11-18 21:20:23 +00:00
|
|
|
## Assumptions
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2016-03-28 17:06:44 +00:00
|
|
|
* Consul 0.6.4 or later is needed for using the Script checks.
|
|
|
|
|
2016-01-27 05:09:03 +00:00
|
|
|
* Consul 0.6.0 or later is needed for using the TCP checks.
|
2015-11-18 20:22:48 +00:00
|
|
|
|
2016-01-27 05:09:03 +00:00
|
|
|
* The service discovery feature in Nomad depends on operators making sure that
|
|
|
|
the Nomad client can reach the Consul agent.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
|
|
|
* Nomad assumes that it controls the life cycle of all the externally
|
|
|
|
discoverable services running on a host.
|
|
|
|
|
2016-01-27 05:09:03 +00:00
|
|
|
* Tasks running inside Nomad also need to reach out to the Consul agent if
|
2015-11-18 21:20:23 +00:00
|
|
|
they want to use any of the Consul APIs. Ex: A task running inside a docker
|
|
|
|
container in the bridge mode won't be able to talk to a Consul Agent running
|
|
|
|
on the loopback interface of the host since the container in the bridge mode
|
|
|
|
has it's own network interface and doesn't see interfaces on the global
|
|
|
|
network namespace of the host. There are a couple of ways to solve this, one
|
|
|
|
way is to run the container in the host networking mode, or make the Consul
|
2016-01-27 05:09:03 +00:00
|
|
|
agent listen on an interface in the network namespace of the container.
|