github.com/diptanu/nomad@v0.5.7-0.20170516172507-d72e86cbe3d9/website/source/intro/getting-started/jobs.html.md (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Jobs"
     4  sidebar_current: "getting-started-jobs"
     5  description: |-
     6    Learn how to submit, modify and stop jobs in Nomad.
     7  ---
     8  
     9  # Jobs
    10  
    11  Jobs are the primary configuration that users interact with when using
    12  Nomad. A job is a declarative specification of tasks that Nomad should run.
    13  Jobs have a globally unique name, one or many task groups, which are themselves
    14  collections of one or many tasks.
    15  
    16  The format of the jobs is documented in the [job specification][jobspec]. They
    17  can either be specified in [HashiCorp Configuration Language][hcl] or JSON,
    18  however we recommend only using JSON when the configuration is generated by a machine.
    19  
    20  ## Running a Job
    21  
    22  To get started, we will use the [`init` command](/docs/commands/init.html) which
    23  generates a skeleton job file:
    24  
    25  ```
    26  $ nomad init
    27  Example job file written to example.nomad
    28  
    29  $ cat example.nomad
    30  
    31  # There can only be a single job definition per file.
    32  # Create a job with ID and Name 'example'
    33  job "example" {
    34  	# Run the job in the global region, which is the default.
    35  	# region = "global"
    36  ...
    37  ```
    38  
    39  In this example job file, we have declared a single task 'redis' which is using
    40  the Docker driver to run the task. The primary way you interact with Nomad
    41  is with the [`run` command](/docs/commands/run.html). The `run` command takes
    42  a job file and registers it with Nomad. This is used both to register new
    43  jobs and to update existing jobs.
    44  
    45  We can register our example job now:
    46  
    47  ```
    48  $ nomad run example.nomad
    49  ==> Monitoring evaluation "26cfc69e"
    50      Evaluation triggered by job "example"
    51      Allocation "8ba85cef" created: node "171a583b", group "cache"
    52      Evaluation status changed: "pending" -> "complete"
    53  ==> Evaluation "26cfc69e" finished with status "complete"
    54  ```
    55  
    56  Anytime a job is updated, Nomad creates an evaluation to determine what
    57  actions need to take place. In this case, because this is a new job, Nomad has
    58  determined that an allocation should be created and has scheduled it on our
    59  local agent.
    60  
    61  To inspect the status of our job we use the [`status` command](/docs/commands/status.html):
    62  
    63  ```
    64  $ nomad status example
    65  ID          = example
    66  Name        = example
    67  Type        = service
    68  Priority    = 50
    69  Datacenters = dc1
    70  Status      = running
    71  Periodic    = false
    72  
    73  Summary
    74  Task Group  Queued  Starting  Running  Failed  Complete  Lost
    75  cache       0       0         1        0       0         0
    76  
    77  Allocations
    78  ID        Eval ID   Node ID   Task Group  Desired  Status   Created At
    79  8ba85cef  26cfc69e  171a583b  cache       run      running  06/23/16 01:41:13 UTC
    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.html):
    87  
    88  ```
    89  $ nomad alloc-status 8ba85cef
    90  ID            = dadcdb81
    91  Eval ID       = 61b0b423
    92  Name          = example.cache[0]
    93  Node ID       = 72687b1a
    94  Job ID        = example
    95  Client Status = running
    96  Created At    = 06/23/16 01:41:13 UTC
    97  
    98  Task "redis" is "running"
    99  Task Resources
   100  CPU    Memory           Disk     IOPS  Addresses
   101  2/500  6.3 MiB/256 MiB  300 MiB  0     db: 127.0.0.1:30329
   102  
   103  Recent Events:
   104  Time                   Type      Description
   105  06/23/16 01:41:16 UTC  Started   Task started by client
   106  06/23/16 01:41:13 UTC  Received  Task received by client
   107  ```
   108  
   109  We can see that Nomad reports the state of the allocation as well as its
   110  current resource usage. By supplying the `-stats` flag, more detailed resource
   111  usage statistics will be reported.
   112  
   113  To see the logs of a task, we can use the [logs command](/docs/commands/logs.html):
   114  
   115  ```
   116  $ nomad logs 8ba85cef redis
   117                   _._
   118              _.-``__ ''-._
   119         _.-``    `.  `_.  ''-._           Redis 3.2.1 (00000000/0) 64 bit
   120     .-`` .-```.  ```\/    _.,_ ''-._
   121    (    '      ,       .-`  | `,    )     Running in standalone mode
   122    |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
   123    |    `-._   `._    /     _.-'    |     PID: 1
   124     `-._    `-._  `-./  _.-'    _.-'
   125    |`-._`-._    `-.__.-'    _.-'_.-'|
   126    |    `-._`-._        _.-'_.-'    |           http://redis.io
   127     `-._    `-._`-.__.-'_.-'    _.-'
   128    |`-._`-._    `-.__.-'    _.-'_.-'|
   129    |    `-._`-._        _.-'_.-'    |
   130     `-._    `-._`-.__.-'_.-'    _.-'
   131         `-._    `-.__.-'    _.-'
   132             `-._        _.-'
   133                 `-.__.-'
   134  ...
   135  ```
   136  
   137  ## Modifying a Job
   138  
   139  The definition of a job is not static, and is meant to be updated over time.
   140  You may update a job to change the docker container, to update the application version,
   141  or to change the count of a task group to scale with load.
   142  
   143  For now, edit the `example.nomad` file to uncomment the count and set it to 3:
   144  
   145  ```
   146  # Control the number of instances of this group.
   147  # Defaults to 1
   148  count = 3
   149  ```
   150  
   151  Once you have finished modifying the job specification, use [`plan`
   152  command](/docs/commands/plan.html) to invoke a dry-run of the scheduler to see
   153  what would happen if you ran the updated job:
   154  
   155  ```
   156  $ nomad plan example.nomad
   157  +/- Job: "example"
   158  +/- Task Group: "cache" (2 create, 1 in-place update)
   159    +/- Count: "1" => "3" (forces create)
   160        Task: "redis"
   161  
   162  Scheduler dry-run:
   163  - All tasks successfully allocated.
   164  
   165  Job Modify Index: 6
   166  To submit the job with version verification run:
   167  
   168  nomad run -check-index 6 example.nomad
   169  
   170  When running the job with the check-index flag, the job will only be run if the
   171  server side version matches the job modify index returned. If the index has
   172  changed, another user has modified the job and the plan's results are
   173  potentially invalid.
   174  ```
   175  
   176  We can see that the scheduler detected the change in count and informs us that
   177  it will cause 2 new instances to be created. The in-place update that will
   178  occur is to push the update job specification to the existing allocation and
   179  will not cause any service interruption. We can then run the job with the
   180  run command the `plan` emitted.
   181  
   182  By running with the `-check-index` flag, Nomad checks that the job has not
   183  been modified since the plan was run. This is useful if multiple people are
   184  interacting with the job at the same time to ensure the job hasn't changed
   185  before you apply your modifications.
   186  
   187  ```
   188  $ nomad run -check-index 6 example.nomad
   189  ==> Monitoring evaluation "127a49d0"
   190      Evaluation triggered by job "example"
   191      Allocation "8ab24eef" created: node "171a583b", group "cache"
   192      Allocation "f6c29874" created: node "171a583b", group "cache"
   193      Allocation "8ba85cef" modified: node "171a583b", group "cache"
   194      Evaluation status changed: "pending" -> "complete"
   195  ==> Evaluation "127a49d0" finished with status "complete"
   196  ```
   197  
   198  Because we set the count of the task group to three, Nomad created two
   199  additional allocations to get to the desired state. It is idempotent to
   200  run the same job specification again and no new allocations will be created.
   201  
   202  Now, let's try to do an application update. In this case, we will simply change
   203  the version of redis we want to run. Edit the `example.nomad` file and change
   204  the Docker image from "redis:latest" to "redis:2.8":
   205  
   206  ```
   207  # Configure Docker driver with the image
   208  config {
   209      image = "redis:2.8"
   210  }
   211  ```
   212  
   213  We can run `plan` again to see what will happen if we submit this change:
   214  
   215  ```
   216  $ nomad plan example.nomad
   217  +/- Job: "example"
   218  +/- Task Group: "cache" (3 create/destroy update)
   219    +/- Task: "redis" (forces create/destroy update)
   220      +/- Config {
   221        +/- image:           "redis:latest" => "redis:2.8"
   222            port_map[0][db]: "6379"
   223      }
   224  
   225  Scheduler dry-run:
   226  - All tasks successfully allocated.
   227  - Rolling update, next evaluation will be in 10s.
   228  
   229  Job Modify Index: 42
   230  To submit the job with version verification run:
   231  
   232  nomad run -check-index 42 example.nomad
   233  
   234  When running the job with the check-index flag, the job will only be run if the
   235  server side version matches the job modify index returned. If the index has
   236  changed, another user has modified the job and the plan's results are
   237  potentially invalid.
   238  ```
   239  
   240  Here we can see the `plan` reports it will do three create/destroy updates
   241  which stops the old tasks and starts the new tasks because we have changed the
   242  version of redis to run. We also see that the update will happen with a rolling
   243  update. This is because our example job is configured to do a rolling update
   244  via the `stagger` attribute, doing a single update every 10 seconds.
   245  
   246  Once ready, use `run` to push the updated specification now:
   247  
   248  ```
   249  $ nomad run example.nomad
   250  ==> Monitoring evaluation "ebcc3e14"
   251      Evaluation triggered by job "example"
   252      Allocation "9a3743f4" created: node "171a583b", group "cache"
   253      Evaluation status changed: "pending" -> "complete"
   254  ==> Evaluation "ebcc3e14" finished with status "complete"
   255  ==> Monitoring evaluation "b508d8f0"
   256      Evaluation triggered by job "example"
   257      Allocation "926e5876" created: node "171a583b", group "cache"
   258      Evaluation status changed: "pending" -> "complete"
   259  ==> Evaluation "b508d8f0" finished with status "complete"
   260  ==> Monitoring next evaluation "ea78c05a" in 10s
   261  ==> Monitoring evaluation "ea78c05a"
   262      Evaluation triggered by job "example"
   263      Allocation "3c8589d5" created: node "171a583b", group "cache"
   264      Evaluation status changed: "pending" -> "complete"
   265  ==> Evaluation "ea78c05a" finished with status "complete"
   266  ```
   267  
   268  We can see that Nomad handled the update in three phases, only updating a single task
   269  group in each phase. The update strategy can be configured, but rolling updates makes
   270  it easy to upgrade an application at large scale.
   271  
   272  ## Stopping a Job
   273  
   274  So far we've created, run and modified a job. The final step in a job lifecycle
   275  is stopping the job. This is done with the [`stop` command](/docs/commands/stop.html):
   276  
   277  ```
   278  $ nomad stop example
   279  ==> Monitoring evaluation "fd03c9f8"
   280      Evaluation triggered by job "example"
   281      Evaluation status changed: "pending" -> "complete"
   282  ==> Evaluation "fd03c9f8" finished with status "complete"
   283  ```
   284  
   285  When we stop a job, it creates an evaluation which is used to stop all
   286  the existing allocations. This also deletes the job definition out of Nomad.
   287  If we try to query the job status, we can see it is no longer registered:
   288  
   289  ```
   290  $ nomad status example
   291  No job(s) with prefix or id "example" found
   292  ```
   293  
   294  If we wanted to start the job again, we could simply `run` it again.
   295  
   296  ## Next Steps
   297  
   298  Users of Nomad primarily interact with jobs, and we've now seen
   299  how to create and scale our job, perform an application update,
   300  and do a job tear down. Next we will add another Nomad
   301  client to [create our first cluster](cluster.html)
   302  
   303  [jobspec]: /docs/job-specification/index.html "Nomad Job Specification"
   304  [hcl]: https://github.com/hashicorp/hcl "HashiCorp Configuration Language"