2016-10-28 00:36:26 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "task Stanza - Job Specification"
|
|
|
|
sidebar_current: "docs-job-specification-task"
|
|
|
|
description: |-
|
|
|
|
The "task" stanza creates an individual unit of work, such as a Docker
|
|
|
|
container, web application, or batch processing.
|
|
|
|
---
|
|
|
|
|
|
|
|
# `task` Stanza
|
|
|
|
|
2018-04-04 22:57:10 +00:00
|
|
|
<table class="table table-bordered table-striped">
|
|
|
|
<tr>
|
|
|
|
<th width="120">Placement</th>
|
|
|
|
<td>
|
|
|
|
<code>job -> group -> **task**</code>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2016-10-28 00:36:26 +00:00
|
|
|
The `task` stanza creates an individual unit of work, such as a Docker
|
|
|
|
container, web application, or batch processing.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
job "docs" {
|
|
|
|
group "example" {
|
|
|
|
task "server" {
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## `task` Parameters
|
|
|
|
|
2016-10-28 03:01:11 +00:00
|
|
|
- `artifact` <code>([Artifact][]: nil)</code> - Defines an artifact to download
|
|
|
|
before running the task. This may be specified multiple times to download
|
|
|
|
multiple artifacts.
|
2016-10-28 00:36:26 +00:00
|
|
|
|
|
|
|
- `config` `(map<string|string>: nil)` - Specifies the driver configuration,
|
|
|
|
which is passed directly to the driver to start the task. The details of
|
|
|
|
configurations are specific to each driver, so please see specific driver
|
|
|
|
documentation for more information.
|
|
|
|
|
2016-10-28 03:01:11 +00:00
|
|
|
- `constraint` <code>([Constraint][]: nil)</code> - Specifies user-defined
|
|
|
|
constraints on the task. This can be provided multiple times to define
|
|
|
|
additional constraints.
|
|
|
|
|
2019-01-24 18:12:40 +00:00
|
|
|
- `affinity` <code>([Affinity][]: nil)</code> - This can be provided
|
|
|
|
multiple times to define preferred placement criteria.
|
|
|
|
|
2017-01-26 06:15:00 +00:00
|
|
|
- `dispatch_payload` <code>([DispatchPayload][]: nil)</code> - Configures the
|
|
|
|
task to have access to dispatch payloads.
|
|
|
|
|
2016-10-28 03:01:11 +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
|
2017-03-28 18:36:47 +00:00
|
|
|
is available. Examples include `docker`, `qemu`, `java` and `exec`.
|
2016-10-28 00:36:26 +00:00
|
|
|
|
|
|
|
- `env` <code>([Env][]: nil)</code> - Specifies environment variables that will
|
|
|
|
be passed to the running process.
|
|
|
|
|
|
|
|
- `kill_timeout` `(string: "5s")` - Specifies the duration to wait for an
|
|
|
|
application to gracefully quit before force-killing. Nomad sends an `SIGINT`.
|
|
|
|
If the task does not exit before the configured timeout, `SIGKILL` is sent to
|
2017-02-21 19:52:29 +00:00
|
|
|
the task. Note that the value set here is capped at the value set for
|
|
|
|
[`max_kill_timeout`][max_kill] on the agent running the task, which has a
|
|
|
|
default value of 30 seconds.
|
2016-10-28 00:36:26 +00:00
|
|
|
|
2017-12-07 17:33:05 +00:00
|
|
|
- `kill_signal` `(string)` - Specifies a configurable kill signal for a task,
|
2017-12-07 18:46:25 +00:00
|
|
|
where the default is SIGINT. Note that this is only supported for drivers
|
2017-12-20 00:19:40 +00:00
|
|
|
sending signals (currently `docker`, `exec`, `raw_exec`, and `java` drivers).
|
2017-12-07 17:33:05 +00:00
|
|
|
|
2017-02-13 18:18:34 +00:00
|
|
|
- `leader` `(bool: false)` - Specifies whether the task is the leader task of
|
|
|
|
the task group. If set to true, when the leader task completes, all other
|
|
|
|
tasks within the task group will be gracefully shutdown.
|
|
|
|
|
2016-10-28 00:36:26 +00:00
|
|
|
- `logs` <code>([Logs][]: nil)</code> - Specifies logging configuration for the
|
|
|
|
`stdout` and `stderr` of the task.
|
|
|
|
|
2016-10-28 03:01:11 +00:00
|
|
|
- `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates
|
|
|
|
with user-defined metadata.
|
|
|
|
|
2016-11-01 12:53:13 +00:00
|
|
|
- `resources` <code>([Resources][]: <required>)</code> - Specifies the minimum
|
2017-03-28 18:36:47 +00:00
|
|
|
resource requirements such as RAM, CPU and network.
|
2016-10-28 03:01:11 +00:00
|
|
|
|
|
|
|
- `service` <code>([Service][]: nil)</code> - Specifies integrations with
|
|
|
|
[Consul][] for service discovery. Nomad automatically registers when a task
|
|
|
|
is started and de-registers it when the task dies.
|
|
|
|
|
2017-08-17 00:54:11 +00:00
|
|
|
- `shutdown_delay` `(string: "0s")` - Specifies the duration to wait when
|
|
|
|
killing a task between removing it from Consul and sending it a shutdown
|
|
|
|
signal. Ideally services would fail healthchecks once they receive a shutdown
|
|
|
|
signal. Alternatively `shutdown_delay` may be set to give in flight requests
|
|
|
|
time to complete before shutting down.
|
|
|
|
|
2016-11-22 18:59:01 +00:00
|
|
|
- `user` `(string: <varies>)` - Specifies the user that will run the task.
|
|
|
|
Defaults to `nobody` for the [`exec`][exec] and [`java`][java] drivers.
|
|
|
|
[Docker][] and [rkt][] images specify their own default users. This can only
|
2016-11-22 19:11:24 +00:00
|
|
|
be set on Linux platforms, and clients can restrict
|
|
|
|
[which drivers][user_drivers] are allowed to run tasks as
|
|
|
|
[certain users][user_blacklist].
|
2016-10-28 03:01:11 +00:00
|
|
|
|
2017-01-24 17:12:42 +00:00
|
|
|
- `template` <code>([Template][]: nil)</code> - Specifies the set of templates
|
|
|
|
to render for the task. Templates can be used to inject both static and
|
|
|
|
dynamic configuration with data populated from environment variables, Consul
|
|
|
|
and Vault.
|
2017-01-24 13:46:46 +00:00
|
|
|
|
2016-11-02 22:29:17 +00:00
|
|
|
- `vault` <code>([Vault][]: nil)</code> - Specifies the set of Vault policies
|
|
|
|
required by the task. This overrides any `vault` block set at the `group` or
|
|
|
|
`job` level.
|
2016-11-02 22:14:49 +00:00
|
|
|
|
2016-10-28 00:36:26 +00:00
|
|
|
## `task` Examples
|
|
|
|
|
2016-10-31 00:41:03 +00:00
|
|
|
The following examples only show the `task` stanzas. Remember that the
|
|
|
|
`task` stanza is only valid in the placements listed above.
|
|
|
|
|
2016-10-28 00:36:26 +00:00
|
|
|
### Docker Container
|
|
|
|
|
|
|
|
This example defines a task that starts a Docker container as a service. Docker
|
|
|
|
is just one of many drivers supported by Nomad. Read more about drivers in the
|
|
|
|
[Nomad drivers documentation](/docs/drivers/index.html).
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
task "server" {
|
|
|
|
driver = "docker"
|
|
|
|
config {
|
|
|
|
image = "hashicorp/http-echo"
|
|
|
|
args = ["-text", "hello world"]
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
|
|
|
network {
|
|
|
|
mbits = 250
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Metadata and Environment Variables
|
|
|
|
|
|
|
|
This example uses custom metadata and environment variables to pass information
|
|
|
|
to the task.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
task "server" {
|
|
|
|
driver = "exec"
|
|
|
|
config {
|
|
|
|
command = "/bin/env"
|
|
|
|
}
|
|
|
|
|
|
|
|
meta {
|
|
|
|
my-key = "my-value"
|
|
|
|
}
|
|
|
|
|
|
|
|
env {
|
|
|
|
MY_KEY = "${meta.my-key}"
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
2016-10-31 00:36:27 +00:00
|
|
|
cpu = 20
|
2016-10-28 00:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Service Discovery
|
|
|
|
|
|
|
|
This example creates a service in Consul. To read more about service discovery
|
2018-10-02 00:19:44 +00:00
|
|
|
in Nomad, please see the [Nomad service discovery documentation][service_discovery].
|
2016-10-28 00:36:26 +00:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
task "server" {
|
|
|
|
driver = "docker"
|
|
|
|
config {
|
|
|
|
image = "hashicorp/http-echo"
|
|
|
|
args = ["-text", "hello world"]
|
|
|
|
}
|
|
|
|
|
|
|
|
service {
|
|
|
|
tags = ["default"]
|
|
|
|
|
|
|
|
check {
|
|
|
|
type = "tcp"
|
|
|
|
interval = "10s"
|
|
|
|
timeout = "2s"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resources {
|
2016-10-31 00:36:27 +00:00
|
|
|
cpu = 20
|
2016-10-28 00:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
[artifact]: /docs/job-specification/artifact.html "Nomad artifact Job Specification"
|
|
|
|
[consul]: https://www.consul.io/ "Consul by HashiCorp"
|
|
|
|
[constraint]: /docs/job-specification/constraint.html "Nomad constraint Job Specification"
|
2019-01-24 18:12:40 +00:00
|
|
|
[affinity]: /docs/job-specification/affinity.html "Nomad affinity Job Specification"
|
2017-01-26 06:16:20 +00:00
|
|
|
[dispatchpayload]: /docs/job-specification/dispatch_payload.html "Nomad dispatch_payload Job Specification"
|
2016-10-28 00:36:26 +00:00
|
|
|
[env]: /docs/job-specification/env.html "Nomad env Job Specification"
|
|
|
|
[meta]: /docs/job-specification/meta.html "Nomad meta Job Specification"
|
|
|
|
[resources]: /docs/job-specification/resources.html "Nomad resources Job Specification"
|
|
|
|
[logs]: /docs/job-specification/logs.html "Nomad logs Job Specification"
|
2018-10-02 00:19:44 +00:00
|
|
|
[service]: /docs/job-specification/service.html "Nomad service Job Specification"
|
2018-10-02 00:13:03 +00:00
|
|
|
[vault]: /docs/job-specification/vault.html "Nomad vault Job Specification"
|
2016-11-22 18:59:01 +00:00
|
|
|
[exec]: /docs/drivers/exec.html "Nomad exec Driver"
|
|
|
|
[java]: /docs/drivers/java.html "Nomad Java Driver"
|
|
|
|
[Docker]: /docs/drivers/docker.html "Nomad Docker Driver"
|
|
|
|
[rkt]: /docs/drivers/rkt.html "Nomad rkt Driver"
|
2018-10-02 00:19:44 +00:00
|
|
|
[service_discovery]: /guides/operations/consul-integration/index.html#service-discovery/index.html "Nomad Service Discovery"
|
2017-01-24 13:46:46 +00:00
|
|
|
[template]: /docs/job-specification/template.html "Nomad template Job Specification"
|
2018-06-22 20:50:42 +00:00
|
|
|
[user_drivers]: /docs/configuration/client.html#_quot_user_checked_drivers_quot_
|
|
|
|
[user_blacklist]: /docs/configuration/client.html#_quot_user_blacklist_quot_
|
|
|
|
[max_kill]: /docs/configuration/client.html#max_kill_timeout
|