github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/intro/getting-started/jobs.mdx (about)

     1  ---
     2  layout: intro
     3  page_title: Jobs
     4  sidebar_title: Jobs
     5  description: 'Learn how to submit, modify and stop jobs in Nomad.'
     6  ---
     7  
     8  # Jobs
     9  
    10  Jobs are the primary configuration that users interact with when using
    11  Nomad. A job is a declarative specification of tasks that Nomad should run.
    12  Jobs have a globally unique name, one or many task groups, which are themselves
    13  collections of one or many tasks.
    14  
    15  The format of the jobs is documented in the [job specification][jobspec]. They
    16  can either be specified in [HashiCorp Configuration Language][hcl] or JSON,
    17  however we recommend only using JSON when the configuration is generated by a machine.
    18  
    19  ## Running a Job
    20  
    21  To get started, we will use the [`job init` command](/docs/commands/job/init) which
    22  generates a skeleton job file:
    23  
    24  ```shell-sessionnomad job init
    25  Example job file written to example.nomad
    26  ```
    27  
    28  You can view the contents of this file by running `cat example.nomad`. In this
    29  example job file, we have declared a single task 'redis' which is using
    30  the Docker driver to run the task. The primary way you interact with Nomad
    31  is with the [`job run` command](/docs/commands/job/run). The `run` command takes
    32  a job file and registers it with Nomad. This is used both to register new
    33  jobs and to update existing jobs.
    34  
    35  We can register our example job now:
    36  
    37  ```shell-sessionnomad job run example.nomad
    38  ==> Monitoring evaluation "13ebb66d"
    39      Evaluation triggered by job "example"
    40      Allocation "883269bf" created: node "e42d6f19", group "cache"
    41      Evaluation within deployment: "b0a84e74"
    42      Evaluation status changed: "pending" -> "complete"
    43  ==> Evaluation "13ebb66d" finished with status "complete"
    44  ```
    45  
    46  Anytime a job is updated, Nomad creates an evaluation to determine what
    47  actions need to take place. In this case, because this is a new job, Nomad has
    48  determined that an allocation should be created and has scheduled it on our
    49  local agent.
    50  
    51  To inspect the status of our job we use the [`status` command](/docs/commands/status):
    52  
    53  ```shell-sessionnomad status example
    54  ID            = example
    55  Name          = example
    56  Submit Date   = 10/31/17 22:58:40 UTC
    57  Type          = service
    58  Priority      = 50
    59  Datacenters   = dc1
    60  Status        = running
    61  Periodic      = false
    62  Parameterized = false
    63  
    64  Summary
    65  Task Group  Queued  Starting  Running  Failed  Complete  Lost
    66  cache       0       0         1        0       0         0
    67  
    68  Latest Deployment
    69  ID          = b0a84e74
    70  Status      = successful
    71  Description = Deployment completed successfully
    72  
    73  Deployed
    74  Task Group  Desired  Placed  Healthy  Unhealthy
    75  cache       1        1       1        0
    76  
    77  Allocations
    78  ID        Node ID   Task Group  Version  Desired  Status   Created  Modified
    79  8ba85cef  171a583b  cache       0        run      running  5m ago   5m ago
    80  ```
    81  
    82  Here we can see that the result of our evaluation was the creation of an
    83  allocation that is now running on the local node.
    84  
    85  An allocation represents an instance of Task Group placed on a node. To inspect
    86  an allocation we use the [`alloc status` command](/docs/commands/alloc/status):
    87  
    88  ```shell-sessionnomad alloc status 8ba85cef
    89  ID                  = 8ba85cef
    90  Eval ID             = 13ebb66d
    91  Name                = example.cache[0]
    92  Node ID             = e42d6f19
    93  Job ID              = example
    94  Job Version         = 0
    95  Client Status       = running
    96  Client Description  = <none>
    97  Desired Status      = run
    98  Desired Description = <none>
    99  Created             = 5m ago
   100  Modified            = 5m ago
   101  Deployment ID       = fa882a5b
   102  Deployment Health   = healthy
   103  
   104  Task "redis" is "running"
   105  Task Resources
   106  CPU        Memory           Disk     Addresses
   107  8/500 MHz  6.3 MiB/256 MiB  300 MiB  db: 127.0.0.1:22672
   108  
   109  Task Events:
   110  Started At     = 10/31/17 22:58:49 UTC
   111  Finished At    = N/A
   112  Total Restarts = 0
   113  Last Restart   = N/A
   114  
   115  Recent Events:
   116  Time                   Type        Description
   117  10/31/17 22:58:49 UTC  Started     Task started by client
   118  10/31/17 22:58:40 UTC  Driver      Downloading image redis:3.2
   119  10/31/17 22:58:40 UTC  Task Setup  Building Task Directory
   120  10/31/17 22:58:40 UTC  Received    Task received by client
   121  ```
   122  
   123  We can see that Nomad reports the state of the allocation as well as its
   124  current resource usage. By supplying the `-stats` flag, more detailed resource
   125  usage statistics will be reported.
   126  
   127  To see the logs of a task, we can use the [`logs` command](/docs/commands/alloc/logs):
   128  
   129  ````shell-sessionnomad alloc logs 8ba85cef redis
   130                   _._
   131              _.-``__ ''-._
   132         _.-``    `.  `_.  ''-._           Redis 3.2.1 (00000000/0) 64 bit
   133     .-`` .-```.  ```\/    _.,_ ''-._
   134    (    '      ,       .-`  | `,    )     Running in standalone mode
   135    |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
   136    |    `-._   `._    /     _.-'    |     PID: 1
   137     `-._    `-._  `-./  _.-'    _.-'
   138    |`-._`-._    `-.__.-'    _.-'_.-'|
   139    |    `-._`-._        _.-'_.-'    |           http://redis.io
   140     `-._    `-._`-.__.-'_.-'    _.-'
   141    |`-._`-._    `-.__.-'    _.-'_.-'|
   142    |    `-._`-._        _.-'_.-'    |
   143     `-._    `-._`-.__.-'_.-'    _.-'
   144         `-._    `-.__.-'    _.-'
   145             `-._        _.-'
   146                 `-.__.-'
   147  ...
   148  ````
   149  
   150  ## Modifying a Job
   151  
   152  The definition of a job is not static, and is meant to be updated over time.
   153  You may update a job to change the docker container, to update the application version,
   154  or to change the count of a task group to scale with load.
   155  
   156  For now, edit the `example.nomad` file to update the count and set it to 3:
   157  
   158  ```
   159  # The "count" parameter specifies the number of the task groups that should
   160  # be running under this group. This value must be non-negative and defaults
   161  # to 1.
   162  count = 3
   163  ```
   164  
   165  Once you have finished modifying the job specification, use the [`job plan`
   166  command](/docs/commands/job/plan) to invoke a dry-run of the scheduler to see
   167  what would happen if you ran the updated job:
   168  
   169  ```shell-sessionnomad job plan example.nomad
   170  +/- Job: "example"
   171  +/- Task Group: "cache" (2 create, 1 in-place update)
   172    +/- Count: "1" => "3" (forces create)
   173        Task: "redis"
   174  
   175  Scheduler dry-run:
   176  - All tasks successfully allocated.
   177  
   178  Job Modify Index: 7
   179  To submit the job with version verification run:
   180  
   181  nomad job run -check-index 7 example.nomad
   182  
   183  When running the job with the check-index flag, the job will only be run if the
   184  job modify index given matches the server-side version. If the index has
   185  changed, another user has modified the job and the plan's results are
   186  potentially invalid.
   187  ```
   188  
   189  We can see that the scheduler detected the change in count and informs us that
   190  it will cause 2 new instances to be created. The in-place update that will
   191  occur is to push the updated job specification to the existing allocation and
   192  will not cause any service interruption. We can then run the job with the run
   193  command the `plan` emitted.
   194  
   195  By running with the `-check-index` flag, Nomad checks that the job has not
   196  been modified since the plan was run. This is useful if multiple people are
   197  interacting with the job at the same time to ensure the job hasn't changed
   198  before you apply your modifications.
   199  
   200  ```shell-sessionnomad job run -check-index 7 example.nomad
   201  ==> Monitoring evaluation "93d16471"
   202      Evaluation triggered by job "example"
   203      Evaluation within deployment: "0d06e1b6"
   204      Allocation "3249e320" created: node "e42d6f19", group "cache"
   205      Allocation "453b210f" created: node "e42d6f19", group "cache"
   206      Allocation "883269bf" modified: node "e42d6f19", group "cache"
   207      Evaluation status changed: "pending" -> "complete"
   208  ==> Evaluation "93d16471" finished with status "complete"
   209  ```
   210  
   211  Because we set the count of the task group to three, Nomad created two
   212  additional allocations to get to the desired state. It is idempotent to
   213  run the same job specification again and no new allocations will be created.
   214  
   215  Now, let's try to do an application update. In this case, we will simply change
   216  the version of redis we want to run. Edit the `example.nomad` file and change
   217  the Docker image from "redis:3.2" to "redis:4.0":
   218  
   219  ```
   220  # Configure Docker driver with the image
   221  config {
   222      image = "redis:4.0"
   223  }
   224  ```
   225  
   226  We can run `plan` again to see what will happen if we submit this change:
   227  
   228  ```text
   229  +/- Job: "example"
   230  +/- Task Group: "cache" (1 create/destroy update, 2 ignore)
   231    +/- Task: "redis" (forces create/destroy update)
   232      +/- Config {
   233        +/- image:           "redis:3.2" => "redis:4.0"
   234            port_map[0][db]: "6379"
   235          }
   236  
   237  Scheduler dry-run:
   238  - All tasks successfully allocated.
   239  
   240  Job Modify Index: 1127
   241  To submit the job with version verification run:
   242  
   243  nomad job run -check-index 1127 example.nomad
   244  
   245  When running the job with the check-index flag, the job will only be run if the
   246  job modify index given matches the server-side version. If the index has
   247  changed, another user has modified the job and the plan's results are
   248  potentially invalid.
   249  ```
   250  
   251  The plan output shows us that one allocation will be updated and that the other
   252  two will be ignored. This is due to the `max_parallel` setting in the `update`
   253  stanza, which is set to 1 to instruct Nomad to perform only a single change at
   254  a time.
   255  
   256  Once ready, use `run` to push the updated specification:
   257  
   258  ```shell-sessionnomad job run example.nomad
   259  ==> Monitoring evaluation "293b313a"
   260      Evaluation triggered by job "example"
   261      Evaluation within deployment: "f4047b3a"
   262      Allocation "27bd4a41" created: node "e42d6f19", group "cache"
   263      Evaluation status changed: "pending" -> "complete"
   264  ==> Evaluation "293b313a" finished with status "complete"
   265  ```
   266  
   267  After running, the rolling upgrade can be followed by running `nomad status` and
   268  watching the deployed count.
   269  
   270  We can see that Nomad handled the update in three phases, only updating a single
   271  allocation in each phase and waiting for it to be healthy for `min_healthy_time`
   272  of 10 seconds before moving on to the next. The update strategy can be
   273  configured, but rolling updates makes it easy to upgrade an application at large
   274  scale.
   275  
   276  ## Stopping a Job
   277  
   278  So far we've created, run and modified a job. The final step in a job lifecycle
   279  is stopping the job. This is done with the [`job stop` command](/docs/commands/job/stop):
   280  
   281  ```shell-sessionnomad job stop example
   282  ==> Monitoring evaluation "6d4cd6ca"
   283      Evaluation triggered by job "example"
   284      Evaluation within deployment: "f4047b3a"
   285      Evaluation status changed: "pending" -> "complete"
   286  ==> Evaluation "6d4cd6ca" finished with status "complete"
   287  ```
   288  
   289  When we stop a job, it creates an evaluation which is used to stop all
   290  the existing allocations. If we now query the job status, we can see it is
   291  now marked as `dead (stopped)`, indicating that the job has been stopped and
   292  Nomad is no longer running it:
   293  
   294  ```shell-sessionnomad status example
   295  ID            = example
   296  Name          = example
   297  Submit Date   = 11/01/17 17:30:40 UTC
   298  Type          = service
   299  Priority      = 50
   300  Datacenters   = dc1
   301  Status        = dead (stopped)
   302  Periodic      = false
   303  Parameterized = false
   304  
   305  Summary
   306  Task Group  Queued  Starting  Running  Failed  Complete  Lost
   307  cache       0       0         0        0       6         0
   308  
   309  Latest Deployment
   310  ID          = f4047b3a
   311  Status      = successful
   312  Description = Deployment completed successfully
   313  
   314  Deployed
   315  Task Group  Desired  Placed  Healthy  Unhealthy
   316  cache       3        3       3        0
   317  
   318  Allocations
   319  ID        Node ID   Task Group  Version  Desired  Status    Created    Modified
   320  8ace140d  2cfe061e  cache       2        stop     complete  5m ago     5m ago
   321  8af5330a  2cfe061e  cache       2        stop     complete  6m ago     6m ago
   322  df50c3ae  2cfe061e  cache       2        stop     complete  6m ago     6m ago
   323  ```
   324  
   325  If we wanted to start the job again, we could simply `run` it again.
   326  
   327  ## Next Steps
   328  
   329  Users of Nomad primarily interact with jobs, and we've now seen
   330  how to create and scale our job, perform an application update,
   331  and do a job tear down. Next we will add another Nomad
   332  client to [create our first cluster](/intro/getting-started/cluster)
   333  
   334  [jobspec]: /docs/job-specification 'Nomad Job Specification'
   335  [hcl]: https://github.com/hashicorp/hcl 'HashiCorp Configuration Language'