github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/index.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: Job Specification
     4  sidebar_title: Job Specification
     5  description: Learn about the Job specification used to submit jobs to Nomad.
     6  ---
     7  
     8  # Job Specification
     9  
    10  The Nomad job specification (or "jobspec" for short) defines the schema for
    11  Nomad jobs. Nomad jobs are specified in [HCL][], which aims to strike a balance
    12  between human readable and editable, and machine-friendly.
    13  
    14  The job specification is broken down into smaller pieces, which you will find
    15  expanded in the navigation menu. We recommend getting started at the [job][]
    16  stanza. Alternatively, you can keep reading to see a few examples.
    17  
    18  For machine-friendliness, Nomad can also read JSON-equivalent configurations. In
    19  general, we recommend using the HCL syntax.
    20  
    21  The general hierarchy for a job is:
    22  
    23  ```text
    24  job
    25    \_ group
    26          \_ task
    27  ```
    28  
    29  Each job file has only a single job, however a job may have multiple groups, and
    30  each group may have multiple tasks. Groups contain a set of tasks that are
    31  co-located on a machine.
    32  
    33  ## Example
    34  
    35  This example shows a sample job file. We tried to keep it as simple as possible,
    36  while still showcasing the power of Nomad. For a more detailed explanation of
    37  any of these fields, please use the navigation to dive deeper.
    38  
    39  ```hcl
    40  # This declares a job named "docs". There can be exactly one
    41  # job declaration per job file.
    42  job "docs" {
    43    # Specify this job should run in the region named "us". Regions
    44    # are defined by the Nomad servers' configuration.
    45    region = "us"
    46  
    47    # Spread the tasks in this job between us-west-1 and us-east-1.
    48    datacenters = ["us-west-1", "us-east-1"]
    49  
    50    # Run this job as a "service" type. Each job type has different
    51    # properties. See the documentation below for more examples.
    52    type = "service"
    53  
    54    # Specify this job to have rolling updates, two-at-a-time, with
    55    # 30 second intervals.
    56    update {
    57      stagger      = "30s"
    58      max_parallel = 2
    59    }
    60  
    61  
    62    # A group defines a series of tasks that should be co-located
    63    # on the same client (host). All tasks within a group will be
    64    # placed on the same host.
    65    group "webs" {
    66      # Specify the number of these tasks we want.
    67      count = 5
    68  
    69      network {
    70        # This requests a dynamic port named "http". This will
    71        # be something like "46283", but we refer to it via the
    72        # label "http".
    73        port "http" {}
    74  
    75        # This requests a static port on 443 on the host. This
    76        # will restrict this task to running once per host, since
    77        # there is only one port 443 on each host.
    78        port "https" {
    79          static = 443
    80        }
    81      }
    82  
    83      # The service block tells Nomad how to register this service
    84      # with Consul for service discovery and monitoring.
    85      service {
    86        # This tells Consul to monitor the service on the port
    87        # labelled "http". Since Nomad allocates high dynamic port
    88        # numbers, we use labels to refer to them.
    89        port = "http"
    90  
    91        check {
    92          type     = "http"
    93          path     = "/health"
    94          interval = "10s"
    95          timeout  = "2s"
    96        }
    97      }
    98  
    99      # Create an individual task (unit of work). This particular
   100      # task utilizes a Docker container to front a web application.
   101      task "frontend" {
   102        # Specify the driver to be "docker". Nomad supports
   103        # multiple drivers.
   104        driver = "docker"
   105  
   106        # Configuration is specific to each driver.
   107        config {
   108          image = "hashicorp/web-frontend"
   109        }
   110  
   111        # It is possible to set environment variables which will be
   112        # available to the task when it runs.
   113        env {
   114          DB_HOST = "db01.example.com"
   115          DB_USER = "web"
   116          DB_PASS = "loremipsum"
   117        }
   118  
   119        # Specify the maximum resources required to run the task,
   120        # include CPU and memory.
   121        resources {
   122          cpu    = 500 # MHz
   123          memory = 128 # MB
   124        }
   125      }
   126    }
   127  }
   128  ```
   129  
   130  Note that starting with Nomad 0.10, the `service` stanza can also be specified at the group level. This
   131  allows job specification authors to create and register services with Consul Connect support. A service
   132  stanza specified at the group level must include a [connect][] stanza, like the following snippet.
   133  
   134  ```hcl
   135  service {
   136         name = "count-api"
   137         port = "9001"
   138  
   139         connect {
   140           sidecar_service {}
   141         }
   142       }
   143  ```
   144  
   145  [hcl]: https://github.com/hashicorp/hcl 'HashiCorp Configuration Language'
   146  [job]: /docs/job-specification/job 'Nomad job Job Specification'
   147  [connect]: /docs/job-specification/connect 'Connect Stanza Specification'