github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/docs/job-specification/task.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: task Stanza - Job Specification
     4  sidebar_title: task
     5  description: |-
     6    The "task" stanza creates an individual unit of work, such as a Docker
     7    container, web application, or batch processing.
     8  ---
     9  
    10  # `task` Stanza
    11  
    12  <Placement groups={['job', 'group', 'task']} />
    13  
    14  The `task` stanza creates an individual unit of work, such as a Docker
    15  container, web application, or batch processing.
    16  
    17  ```hcl
    18  job "docs" {
    19    group "example" {
    20      task "server" {
    21        # ...
    22      }
    23    }
    24  }
    25  ```
    26  
    27  ## `task` Parameters
    28  
    29  - `artifact` <code>([Artifact][]: nil)</code> - Defines an artifact to download
    30    before running the task. This may be specified multiple times to download
    31    multiple artifacts.
    32  
    33  - `config` `(map<string|string>: nil)` - Specifies the driver configuration,
    34    which is passed directly to the driver to start the task. The details of
    35    configurations are specific to each driver, so please see specific driver
    36    documentation for more information.
    37  
    38  - `constraint` <code>([Constraint][]: nil)</code> - Specifies user-defined
    39    constraints on the task. This can be provided multiple times to define
    40    additional constraints.
    41  
    42  - `affinity` <code>([Affinity][]: nil)</code> - This can be provided
    43    multiple times to define preferred placement criteria.
    44  
    45  - `dispatch_payload` <code>([DispatchPayload][]: nil)</code> - Configures the
    46    task to have access to dispatch payloads.
    47  
    48  - `driver` - Specifies the task driver that should be used to run the
    49    task. See the [driver documentation](/docs/drivers) for what
    50    is available. Examples include `docker`, `qemu`, `java` and `exec`.
    51  
    52  - `env` <code>([Env][]: nil)</code> - Specifies environment variables that will
    53    be passed to the running process.
    54  
    55  - `kill_timeout` `(string: "5s")` - Specifies the duration to wait for an
    56    application to gracefully quit before force-killing. Nomad sends an `SIGINT`.
    57    If the task does not exit before the configured timeout, `SIGKILL` is sent to
    58    the task. Note that the value set here is capped at the value set for
    59    [`max_kill_timeout`][max_kill] on the agent running the task, which has a
    60    default value of 30 seconds.
    61  
    62  - `kill_signal` `(string)` - Specifies a configurable kill signal for a task,
    63    where the default is SIGINT. Note that this is only supported for drivers
    64    sending signals (currently `docker`, `exec`, `raw_exec`, and `java` drivers).
    65  
    66  - `leader` `(bool: false)` - Specifies whether the task is the leader task of
    67    the task group. If set to true, when the leader task completes, all other
    68    tasks within the task group will be gracefully shutdown.
    69  
    70  - `lifecycle` <code>([Lifecycle][]: nil)</code> - Specifies when a task is run
    71    within the lifecycle of a task group. Added in Nomad v0.11.
    72  
    73  - `logs` <code>([Logs][]: nil)</code> - Specifies logging configuration for the
    74    `stdout` and `stderr` of the task.
    75  
    76  - `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates
    77    with user-defined metadata.
    78  
    79  - `resources` <code>([Resources][]: &lt;required&gt;)</code> - Specifies the minimum
    80    resource requirements such as RAM, CPU and network.
    81  
    82  - `service` <code>([Service][]: nil)</code> - Specifies integrations with
    83    [Consul][] for service discovery. Nomad automatically registers when a task
    84    is started and de-registers it when the task dies.
    85  
    86  - `shutdown_delay` `(string: "0s")` - Specifies the duration to wait when
    87    killing a task between removing it from Consul and sending it a shutdown
    88    signal. Ideally services would fail healthchecks once they receive a shutdown
    89    signal. Alternatively `shutdown_delay` may be set to give in flight requests
    90    time to complete before shutting down. In addition, task groups may have their
    91    own [`shutdown_delay`](/docs/job-specification/group#shutdown_delay)
    92    which waits between deregistering group services and stopping tasks.
    93  
    94  - `user` `(string: <varies>)` - Specifies the user that will run the task.
    95    Defaults to `nobody` for the [`exec`][exec] and [`java`][java] drivers.
    96    [Docker][] and [rkt][] images specify their own default users. This can only
    97    be set on Linux platforms, and clients can restrict
    98    [which drivers][user_drivers] are allowed to run tasks as
    99    [certain users][user_blacklist].
   100  
   101  - `template` <code>([Template][]: nil)</code> - Specifies the set of templates
   102    to render for the task. Templates can be used to inject both static and
   103    dynamic configuration with data populated from environment variables, Consul
   104    and Vault.
   105  
   106  - `vault` <code>([Vault][]: nil)</code> - Specifies the set of Vault policies
   107    required by the task. This overrides any `vault` block set at the `group` or
   108    `job` level.
   109  
   110  - `kind` `(string: <varies>)` - Used internally to manage tasks according to
   111    the value of this field. Initial use case is for Consul Connect.
   112  
   113  ## `task` Examples
   114  
   115  The following examples only show the `task` stanzas. Remember that the
   116  `task` stanza is only valid in the placements listed above.
   117  
   118  ### Docker Container
   119  
   120  This example defines a task that starts a Docker container as a service. Docker
   121  is just one of many drivers supported by Nomad. Read more about drivers in the
   122  [Nomad drivers documentation](/docs/drivers).
   123  
   124  ```hcl
   125  task "server" {
   126    driver = "docker"
   127    config {
   128      image = "hashicorp/http-echo"
   129      args  = ["-text", "hello world"]
   130    }
   131  
   132    resources {
   133      network {
   134        mbits = 250
   135      }
   136    }
   137  }
   138  ```
   139  
   140  ### Metadata and Environment Variables
   141  
   142  This example uses custom metadata and environment variables to pass information
   143  to the task.
   144  
   145  ```hcl
   146  task "server" {
   147    driver = "exec"
   148    config {
   149      command = "/bin/env"
   150    }
   151  
   152    meta {
   153      my-key = "my-value"
   154    }
   155  
   156    env {
   157      MY_KEY = "${meta.my-key}"
   158    }
   159  
   160    resources {
   161      cpu = 20
   162    }
   163  }
   164  ```
   165  
   166  ### Service Discovery
   167  
   168  This example creates a service in Consul. To read more about service discovery
   169  in Nomad, please see the [Nomad service discovery documentation][service_discovery].
   170  
   171  ```hcl
   172  task "server" {
   173    driver = "docker"
   174    config {
   175      image = "hashicorp/http-echo"
   176      args  = ["-text", "hello world"]
   177    }
   178  
   179    service {
   180      tags = ["default"]
   181  
   182      check {
   183        type     = "tcp"
   184        interval = "10s"
   185        timeout  = "2s"
   186      }
   187    }
   188  
   189    resources {
   190      cpu = 20
   191    }
   192  }
   193  ```
   194  
   195  [artifact]: /docs/job-specification/artifact 'Nomad artifact Job Specification'
   196  [consul]: https://www.consul.io/ 'Consul by HashiCorp'
   197  [constraint]: /docs/job-specification/constraint 'Nomad constraint Job Specification'
   198  [affinity]: /docs/job-specification/affinity 'Nomad affinity Job Specification'
   199  [dispatchpayload]: /docs/job-specification/dispatch_payload 'Nomad dispatch_payload Job Specification'
   200  [env]: /docs/job-specification/env 'Nomad env Job Specification'
   201  [meta]: /docs/job-specification/meta 'Nomad meta Job Specification'
   202  [resources]: /docs/job-specification/resources 'Nomad resources Job Specification'
   203  [lifecycle]: /docs/job-specification/lifecycle 'Nomad lifecycle Job Specification'
   204  [logs]: /docs/job-specification/logs 'Nomad logs Job Specification'
   205  [service]: /docs/job-specification/service 'Nomad service Job Specification'
   206  [vault]: /docs/job-specification/vault 'Nomad vault Job Specification'
   207  [exec]: /docs/drivers/exec 'Nomad exec Driver'
   208  [java]: /docs/drivers/java 'Nomad Java Driver'
   209  [docker]: /docs/drivers/docker 'Nomad Docker Driver'
   210  [rkt]: /docs/drivers/rkt 'Nomad rkt Driver'
   211  [service_discovery]: /docs/integrations/consul-integration#service-discovery 'Nomad Service Discovery'
   212  [template]: /docs/job-specification/template 'Nomad template Job Specification'
   213  [user_drivers]: /docs/configuration/client#user-checked_drivers
   214  [user_blacklist]: /docs/configuration/client#user-blacklist
   215  [max_kill]: /docs/configuration/client#max_kill_timeout