2015-09-12 00:01:02 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2015-09-20 22:06:56 +00:00
|
|
|
page_title: "Job Specification"
|
2015-11-24 20:30:33 +00:00
|
|
|
sidebar_current: "docs-jobspec-syntax"
|
2015-09-12 00:01:02 +00:00
|
|
|
description: |-
|
2015-09-20 22:06:56 +00:00
|
|
|
Learn about the Job specification used to submit jobs to Nomad.
|
2015-09-12 00:01:02 +00:00
|
|
|
---
|
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
# Job Specification
|
2015-09-12 00:01:02 +00:00
|
|
|
|
2015-09-27 01:26:51 +00:00
|
|
|
Jobs can be specified either in [HCL](https://github.com/hashicorp/hcl) or JSON.
|
2015-10-11 20:05:55 +00:00
|
|
|
HCL is meant to strike a balance between human readable and editable, and machine-friendly.
|
2015-09-24 21:17:13 +00:00
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
For machine-friendliness, Nomad can also read JSON configurations. In general, we recommend
|
2015-09-24 21:17:13 +00:00
|
|
|
using the HCL syntax.
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
## HCL Syntax
|
|
|
|
|
2015-09-24 21:17:13 +00:00
|
|
|
For a detailed description of HCL general syntax, [see this guide](https://github.com/hashicorp/hcl#syntax).
|
|
|
|
Here we cover the details of the Job specification for Nomad:
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
# Define a job called my-service
|
|
|
|
job "my-service" {
|
|
|
|
# Job should run in the US region
|
|
|
|
region = "us"
|
|
|
|
|
|
|
|
# Spread tasks between us-west-1 and us-east-1
|
|
|
|
datacenters = ["us-west-1", "us-east-1"]
|
|
|
|
|
2015-10-19 19:30:21 +00:00
|
|
|
# run this job globally
|
|
|
|
type = "system"
|
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
# Rolling updates should be sequential
|
|
|
|
update {
|
|
|
|
stagger = "30s"
|
|
|
|
max_parallel = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
group "webs" {
|
|
|
|
# We want 5 web servers
|
|
|
|
count = 5
|
|
|
|
|
|
|
|
# Create a web front end using a docker image
|
|
|
|
task "frontend" {
|
|
|
|
driver = "docker"
|
|
|
|
config {
|
|
|
|
image = "hashicorp/web-frontend"
|
|
|
|
}
|
2015-11-18 20:14:28 +00:00
|
|
|
service {
|
|
|
|
port = "http"
|
|
|
|
check {
|
|
|
|
type = "http"
|
|
|
|
path = "/health"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 11:59:23 +00:00
|
|
|
env {
|
|
|
|
DB_HOST = "db01.example.com"
|
|
|
|
DB_USER = "web"
|
|
|
|
DB_PASSWORD = "loremipsum"
|
|
|
|
}
|
2015-09-20 22:06:56 +00:00
|
|
|
resources {
|
|
|
|
cpu = 500
|
|
|
|
memory = 128
|
|
|
|
network {
|
|
|
|
mbits = 100
|
2015-11-18 20:14:28 +00:00
|
|
|
# Request for a dynamic port
|
|
|
|
port "http" {
|
|
|
|
}
|
|
|
|
# Request for a static port
|
|
|
|
port "https" {
|
|
|
|
static = 443
|
|
|
|
}
|
2015-09-20 22:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This is a fairly simple example job, but demonstrates many of the features and syntax
|
|
|
|
of the job specification. The primary "objects" are the job, task group, and task.
|
|
|
|
Each job file has only a single job, however a job may have multiple task groups,
|
|
|
|
and each task group may have multiple tasks. Task groups are a set of tasks that
|
|
|
|
must be co-located on a machine. Groups with a single task and count of one
|
|
|
|
can be declared outside of a group which is created implicitly.
|
|
|
|
|
|
|
|
Constraints can be specified at the job, task group, or task level to restrict
|
|
|
|
where a task is eligible for running. An example constraint looks like:
|
|
|
|
|
|
|
|
```
|
|
|
|
# Restrict to only nodes running linux
|
|
|
|
constraint {
|
2015-11-18 04:06:00 +00:00
|
|
|
attribute = "$attr.kernel.name"
|
2015-09-20 22:06:56 +00:00
|
|
|
value = "linux"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Jobs can also specify additional metadata at the job, task group, or task level.
|
|
|
|
This metadata is opaque to Nomad and can be used for any purpose, including
|
|
|
|
defining constraints on the metadata. Metadata can be specified by:
|
|
|
|
|
|
|
|
```
|
|
|
|
# Setup ELB via metadata and setup foo
|
|
|
|
meta {
|
|
|
|
foo = "bar"
|
|
|
|
elb_mode = "tcp"
|
|
|
|
elb_check_interval = "10s"
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Syntax Reference
|
|
|
|
|
|
|
|
Following is a syntax reference for the possible keys that are supported
|
|
|
|
and their default values if any for each type of object.
|
|
|
|
|
|
|
|
### Job
|
|
|
|
|
|
|
|
The `job` object supports the following keys:
|
|
|
|
|
|
|
|
* `all_at_once` - Controls if the entire set of tasks in the job must
|
|
|
|
be placed atomically or if they can be scheduled incrementally.
|
|
|
|
This should only be used for special circumstances. Defaults to `false`.
|
|
|
|
|
|
|
|
* `constraint` - This can be provided multiple times to define additional
|
|
|
|
constraints. See the constraint reference for more details.
|
|
|
|
|
|
|
|
* `datacenters` - A list of datacenters in the region which are eligible
|
|
|
|
for task placement. This must be provided, and does not have a default.
|
|
|
|
|
|
|
|
* `group` - This can be provided multiple times to define additional
|
|
|
|
task groups. See the task group reference for more details.
|
|
|
|
|
|
|
|
* `meta` - Annotates the job with opaque metadata.
|
|
|
|
|
|
|
|
* `priority` - Specifies the job priority which is used to prioritize
|
|
|
|
scheduling and access to resources. Must be between 1 and 100 inclusively,
|
|
|
|
and defaults to 50.
|
|
|
|
|
|
|
|
* `region` - The region to run the job in, defaults to "global".
|
|
|
|
|
|
|
|
* `task` - This can be specified multiple times to add a task as
|
|
|
|
part of the job. Tasks defined directly in a job are wrapped in
|
|
|
|
a task group of the same name.
|
|
|
|
|
|
|
|
* `type` - Specifies the job type and switches which scheduler
|
2015-10-19 19:30:21 +00:00
|
|
|
is used. Nomad provides the `service`, `system` and `batch` schedulers,
|
2015-11-18 06:35:58 +00:00
|
|
|
and defaults to `service`. To learn more about each scheduler type visit
|
|
|
|
[here](/docs/jobspec/schedulers.html)
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
* `update` - Specifies the task update strategy. This requires providing
|
|
|
|
`max_parallel` as an integer and `stagger` as a time duration. If stagger
|
|
|
|
is provided as an integer, seconds are assumed. Otherwise the "s", "m",
|
|
|
|
and "h" suffix can be used, such as "30s". Both values default to zero,
|
|
|
|
which disables rolling updates.
|
|
|
|
|
|
|
|
### Task Group
|
|
|
|
|
|
|
|
The `group` object supports the following keys:
|
|
|
|
|
|
|
|
* `count` - Specifies the number of the task groups that should
|
|
|
|
be running. Must be positive, defaults to one.
|
|
|
|
|
|
|
|
* `constraint` - This can be provided multiple times to define additional
|
|
|
|
constraints. See the constraint reference for more details.
|
|
|
|
|
2015-11-18 01:41:15 +00:00
|
|
|
* `restart` - Specifies the restart policy to be applied to tasks in this group.
|
|
|
|
If omitted, a default policy for batch and non-batch jobs is used based on the
|
|
|
|
job type. See the restart policy reference for more details.
|
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
* `task` - This can be specified multiple times, to add a task as
|
|
|
|
part of the group.
|
|
|
|
|
|
|
|
* `meta` - Annotates the task group with opaque metadata.
|
|
|
|
|
|
|
|
### Task
|
|
|
|
|
|
|
|
The `task` object supports the following keys:
|
|
|
|
|
2015-09-20 22:51:49 +00:00
|
|
|
* `driver` - Specifies the task driver that should be used to run the
|
|
|
|
task. See the [driver documentation](/docs/drivers/index.html) for what
|
2015-11-18 22:39:33 +00:00
|
|
|
is available. Examples include `docker`, `qemu`, `java`, and `exec`.
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
* `constraint` - This can be provided multiple times to define additional
|
|
|
|
constraints. See the constraint reference for more details.
|
|
|
|
|
|
|
|
* `config` - A map of key/value configuration passed into the driver
|
|
|
|
to start the task. The details of configurations are specific to
|
|
|
|
each driver.
|
|
|
|
|
2015-11-24 19:22:23 +00:00
|
|
|
* `service` - Nomad integrates with Consul for service discovery. A service
|
2015-11-18 20:16:03 +00:00
|
|
|
block represents a routable and discoverable service on the network. Nomad
|
2015-11-24 19:22:23 +00:00
|
|
|
automatically registers when a task is started and de-registers it when the
|
|
|
|
task transitons to the dead state. [Click
|
|
|
|
here](/docs/jobspec/servicediscovery.html) to learn more about services.
|
2015-11-18 20:14:28 +00:00
|
|
|
|
2015-10-01 11:59:23 +00:00
|
|
|
* `env` - A map of key/value representing environment variables that
|
|
|
|
will be passed along to the running process.
|
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
* `resources` - Provides the resource requirements of the task.
|
|
|
|
See the resources reference for more details.
|
|
|
|
|
|
|
|
* `meta` - Annotates the task group with opaque metadata.
|
|
|
|
|
|
|
|
### Resources
|
|
|
|
|
|
|
|
The `resources` object supports the following keys:
|
|
|
|
|
|
|
|
* `cpu` - The CPU required in MHz.
|
|
|
|
|
|
|
|
* `disk` - The disk required in MB.
|
|
|
|
|
2015-10-06 22:44:01 +00:00
|
|
|
* `iops` - The number of IOPS required given as a weight between 10-1000.
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
* `memory` - The memory required in MB.
|
|
|
|
|
|
|
|
* `network` - The network required. Details below.
|
|
|
|
|
2015-09-24 21:17:13 +00:00
|
|
|
The `network` object supports the following keys:
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
* `mbits` - The number of MBits in bandwidth required.
|
|
|
|
|
2015-11-30 22:18:31 +00:00
|
|
|
* `port` - `port` is a repeatable object that can be used to specify both
|
|
|
|
dynamic ports and reserved ports. It has the following format:
|
|
|
|
|
|
|
|
```
|
|
|
|
port "label" {
|
|
|
|
// If the `static` field is omitted, a dynamic port will be assigned.
|
|
|
|
static = 6539
|
|
|
|
}
|
|
|
|
```
|
2015-09-20 22:06:56 +00:00
|
|
|
|
2015-11-18 01:41:15 +00:00
|
|
|
### Restart Policy
|
|
|
|
|
|
|
|
The `restart` object supports the following keys:
|
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
* `attempts` - `attempts` is the number of restarts allowed in an `interval`.
|
2015-11-18 01:41:15 +00:00
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
* `interval` - `interval` is a time duration that can be specified using the
|
|
|
|
`s`, `m`, and `h` suffixes, such as `30s`. The `interval` begins when the
|
|
|
|
first task starts and ensures that only `attempts` number of restarts happens
|
|
|
|
within it. If more than `attempts` number of failures happen, behavior is
|
|
|
|
controlled by `mode`.
|
2015-11-18 01:41:15 +00:00
|
|
|
|
|
|
|
* `delay` - A duration to wait before restarting a task. It is specified as a
|
2015-12-18 00:39:47 +00:00
|
|
|
time duration using the `s`, `m`, and `h` suffixes, such as `30s`. A random
|
|
|
|
jitter of up to 25% is added to the the delay.
|
2015-11-18 01:41:15 +00:00
|
|
|
|
2015-12-18 20:17:13 +00:00
|
|
|
* `on_success` - `on_success` controls whether a task is restarted when the
|
|
|
|
task exits successfully.
|
|
|
|
|
|
|
|
* `mode` - Controls the behavior when the task fails more than `attempts`
|
|
|
|
times in an interval. Possible values are listed below:
|
|
|
|
|
|
|
|
* `delay` - `delay` will delay the next restart until the next `interval` is
|
|
|
|
reached.
|
|
|
|
|
|
|
|
* `fail` - `fail` will not restart the task again.
|
|
|
|
|
2015-11-18 01:41:15 +00:00
|
|
|
The default `batch` restart policy is:
|
|
|
|
|
|
|
|
```
|
|
|
|
restart {
|
|
|
|
attempts = 15
|
|
|
|
delay = "15s"
|
2015-12-18 20:17:13 +00:00
|
|
|
interval = "168h" # 7 days
|
|
|
|
on_success = false
|
|
|
|
mode = "delay"
|
2015-11-18 01:41:15 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-11-18 06:35:58 +00:00
|
|
|
The default non-batch restart policy is:
|
2015-11-18 01:41:15 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
restart {
|
|
|
|
interval = "1m"
|
|
|
|
attempts = 2
|
|
|
|
delay = "15s"
|
2015-12-18 20:17:13 +00:00
|
|
|
on_success = true
|
|
|
|
mode = "delay"
|
2015-11-18 01:41:15 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-09-20 22:06:56 +00:00
|
|
|
### Constraint
|
|
|
|
|
|
|
|
The `constraint` object supports the following keys:
|
|
|
|
|
|
|
|
* `attribute` - Specifies the attribute to examine for the
|
|
|
|
constraint. See the table of attributes below.
|
|
|
|
|
|
|
|
* `operator` - Specifies the comparison operator. Defaults to equality,
|
2015-10-11 19:58:25 +00:00
|
|
|
and can be `=`, `==`, `is`, `!=`, `not`, `>`, `>=`, `<`, `<=`. The
|
|
|
|
ordering is compared lexically.
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
* `value` - Specifies the value to compare the attribute against.
|
|
|
|
This can be a literal value or another attribute.
|
2015-10-11 19:24:16 +00:00
|
|
|
|
|
|
|
* `version` - Specifies a version constraint against the attribute.
|
2015-11-18 22:39:33 +00:00
|
|
|
This sets the operator to `version` and the `value` to what is
|
2015-10-11 19:24:16 +00:00
|
|
|
specified. This supports a comma seperated list of constraints,
|
|
|
|
including the pessimistic operator. See the
|
|
|
|
[go-version](https://github.com/hashicorp/go-version) repository
|
|
|
|
for examples.
|
2015-10-11 19:38:38 +00:00
|
|
|
|
|
|
|
* `regexp` - Specifies a regular expression constraint against
|
|
|
|
the attribute. This sets the operator to "regexp" and the `value`
|
|
|
|
to the regular expression.
|
2015-10-22 23:37:20 +00:00
|
|
|
|
2015-11-18 01:41:15 +00:00
|
|
|
* `distinct_hosts` - `distinct_hosts` accepts a boolean `true`. The default is
|
|
|
|
`false`.
|
2015-10-23 00:40:41 +00:00
|
|
|
|
2015-11-24 19:22:23 +00:00
|
|
|
When `distinct_hosts` is `true` at the Job level, each instance of all task
|
2015-11-18 01:41:15 +00:00
|
|
|
Groups specified in the job is placed on a separate host.
|
2015-10-23 00:40:41 +00:00
|
|
|
|
2015-11-24 19:22:23 +00:00
|
|
|
When `distinct_hosts` is `true` at the task group level with count > 1, each
|
|
|
|
instance of a task group is placed on a separate host. Different task groups in
|
2015-11-18 01:41:15 +00:00
|
|
|
the same job _may_ be co-scheduled.
|
2015-10-23 00:40:41 +00:00
|
|
|
|
2015-11-18 01:41:15 +00:00
|
|
|
Tasks within a task group are always co-scheduled.
|
2015-09-20 22:06:56 +00:00
|
|
|
|
2015-09-24 21:17:13 +00:00
|
|
|
Below is a table documenting the variables that can be interpreted:
|
2015-09-20 22:06:56 +00:00
|
|
|
|
|
|
|
<table class="table table-bordered table-striped">
|
|
|
|
<tr>
|
|
|
|
<th>Variable</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>$node.id</td>
|
|
|
|
<td>The client node identifier</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>$node.datacenter</td>
|
|
|
|
<td>The client node datacenter</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>$node.name</td>
|
|
|
|
<td>The client node name</td>
|
|
|
|
</tr>
|
2015-12-22 01:15:34 +00:00
|
|
|
<tr>
|
|
|
|
<td>$node.class</td>
|
|
|
|
<td>The client node class</td>
|
|
|
|
</tr>
|
2015-09-20 22:06:56 +00:00
|
|
|
<tr>
|
|
|
|
<td>$attr.\<key\></td>
|
|
|
|
<td>The attribute given by `key` on the client node.</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>$meta.\<key\></td>
|
|
|
|
<td>The metadata value given by `key` on the client node.</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
Below is a table documenting common node attributes:
|
|
|
|
|
|
|
|
<table class="table table-bordered table-striped">
|
|
|
|
<tr>
|
|
|
|
<th>Attribute</th>
|
|
|
|
<th>Description</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>arch</td>
|
2015-11-18 22:39:33 +00:00
|
|
|
<td>CPU architecture of the client. Examples: `amd64`, `386`</td>
|
2015-09-20 22:06:56 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>consul.datacenter</td>
|
|
|
|
<td>The Consul datacenter of the client node if Consul found</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>cpu.numcores</td>
|
|
|
|
<td>Number of CPU cores on the client</td>
|
|
|
|
</tr>
|
2015-09-20 22:51:49 +00:00
|
|
|
<tr>
|
|
|
|
<td>driver.\<key\></td>
|
|
|
|
<td>See the [task drivers](/docs/drivers/index.html) for attribute documentation</td>
|
|
|
|
</tr>
|
2015-09-20 22:06:56 +00:00
|
|
|
<tr>
|
|
|
|
<td>hostname</td>
|
|
|
|
<td>Hostname of the client</td>
|
|
|
|
</tr>
|
2015-11-18 04:06:00 +00:00
|
|
|
<tr>
|
|
|
|
<td>kernel.name</td>
|
2015-11-18 22:39:33 +00:00
|
|
|
<td>Kernel of the client. Examples: `linux`, `darwin`</td>
|
2015-11-18 04:06:00 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>kernel.version</td>
|
2015-11-18 22:39:33 +00:00
|
|
|
<td>Version of the client kernel. Examples: `3.19.0-25-generic`, `15.0.0`</td>
|
2015-11-18 04:06:00 +00:00
|
|
|
</tr>
|
2015-09-20 22:06:56 +00:00
|
|
|
<tr>
|
|
|
|
<td>platform.aws.ami-id</td>
|
|
|
|
<td>On EC2, the AMI ID of the client node</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>platform.aws.instance-type</td>
|
|
|
|
<td>On EC2, the instance type of the client node</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>os.name</td>
|
2015-11-18 22:39:33 +00:00
|
|
|
<td>Operating system of the client. Examples: `ubuntu`, `windows`, `darwin`</td>
|
2015-09-20 22:06:56 +00:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>os.version</td>
|
|
|
|
<td>Version of the client OS</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
## JSON Syntax
|
|
|
|
|
|
|
|
Job files can also be specified in JSON. The conversion is straightforward
|
|
|
|
and self-documented. The downsides of JSON are less human readability and
|
|
|
|
the lack of comments. Otherwise, the two are completely interoperable.
|
|
|
|
|
|
|
|
See the API documentation for more details on the JSON structure.
|
2015-09-12 00:01:02 +00:00
|
|
|
|
2015-09-24 21:17:13 +00:00
|
|
|
|