open-nomad/website/source/intro/getting-started/jobs.html.md

190 lines
6.5 KiB
Markdown
Raw Normal View History

2015-09-22 22:57:12 +00:00
---
layout: "intro"
page_title: "Jobs"
sidebar_current: "getting-started-jobs"
description: |-
2015-09-22 23:18:26 +00:00
Learn how to submit, modify and stop jobs in Nomad.
2015-09-22 22:57:12 +00:00
---
# Jobs
2015-09-22 23:18:26 +00:00
Jobs are the primary configuration that users interact with when using
2015-09-23 01:19:00 +00:00
Nomad. A job is a declarative specification of tasks that Nomad should run.
2015-09-22 23:18:26 +00:00
Jobs have a globally unique name, one or many task groups, which are themselves
collections of one or many tasks.
2015-09-22 22:57:12 +00:00
2015-09-22 23:18:26 +00:00
The format of the jobs is [documented here](/docs/jobspec/index.html). They
can either be specified in [HCL](https://github.com/hashicorp/hcl) or JSON,
however we recommend only using JSON when the configuration is generated by a machine.
2015-09-22 22:57:12 +00:00
2015-09-23 01:10:46 +00:00
## Running a Job
2015-09-22 23:18:26 +00:00
To get started, we will use the [`init` command](/docs/commands/init.html) which
2015-10-10 19:27:09 +00:00
generates a skeleton job file:
2015-09-22 22:57:12 +00:00
```
2015-09-22 23:18:26 +00:00
$ nomad init
2015-09-23 01:10:46 +00:00
Example job file written to example.nomad
2015-09-22 22:57:12 +00:00
2015-09-22 23:18:26 +00:00
$ cat example.nomad
2015-09-22 22:57:12 +00:00
2015-09-23 01:10:46 +00:00
# There can only be a single job definition per file.
# Create a job with ID and Name 'example'
2015-09-22 23:18:26 +00:00
job "example" {
2015-09-23 01:10:46 +00:00
# Run the job in the global region, which is the default.
# region = "global"
...
```
In this example job file, we have declared a single task 'redis' which is using
the Docker driver to run the task. The primary way you interact with Nomad
is with the [`run` command](/docs/commands/run.html). The `run` command takes
a job file and registers it with Nomad. This is used both to register new
jobs and to update existing jobs.
We can register our example job now:
```
$ nomad run example.nomad
2016-02-06 00:28:20 +00:00
==> Monitoring evaluation "26cfc69e"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2016-02-06 00:28:20 +00:00
Allocation "8ba85cef" created: node "171a583b", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2016-02-06 00:28:20 +00:00
==> Evaluation "26cfc69e" finished with status "complete"
2015-09-23 01:10:46 +00:00
```
Anytime a job is updated, Nomad creates an evaluation to determine what
actions need to take place. In this case, because this is a new job, Nomad has
determined that an allocation should be created and has scheduled it on our
local agent.
To inspect the status of our job we use the [`status` command](/docs/commands/status.html):
```
$ nomad status example
ID = example
Name = example
Type = service
Priority = 50
Datacenters = dc1
2016-02-06 00:28:20 +00:00
Status = running
Periodic = false
2015-09-23 01:10:46 +00:00
==> Evaluations
2016-02-06 00:28:20 +00:00
ID Priority Triggered By Status
26cfc69e 50 job-register complete
2015-09-23 01:10:46 +00:00
==> Allocations
2016-02-06 00:28:20 +00:00
ID Eval ID Node ID Task Group Desired Status
8ba85cef 26cfc69e 171a583b cache run running
2015-09-23 01:10:46 +00:00
```
2015-09-23 01:19:00 +00:00
Here we can see that our evaluation that was created has completed, and that
2015-09-23 01:10:46 +00:00
it resulted in the creation of an allocation that is now running on the local node.
## Modifying a Job
The definition of a job is not static, and is meant to be updated over time.
You may update a job to change the docker container, to update the application version,
or to change the count of a task group to scale with load.
2015-09-23 01:10:46 +00:00
2015-09-23 01:19:00 +00:00
For now, edit the `example.nomad` file to uncomment the count and set it to 3:
2015-09-23 01:10:46 +00:00
```
2015-10-10 19:57:30 +00:00
# Control the number of instances of this group.
2015-09-23 01:10:46 +00:00
# Defaults to 1
count = 3
```
Once you have finished modifying the job specification, use `nomad run` to
push the updated version of the job:
```
$ nomad run example.nomad
2016-02-06 00:28:20 +00:00
==> Monitoring evaluation "127a49d0"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2016-02-06 00:28:20 +00:00
Allocation "8ab24eef" created: node "171a583b", group "cache"
Allocation "f6c29874" created: node "171a583b", group "cache"
Allocation "8ba85cef" modified: node "171a583b", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2016-02-06 00:28:20 +00:00
==> Evaluation "127a49d0" finished with status "complete"
2015-09-23 01:10:46 +00:00
```
Because we set the count of the task group to three, Nomad created two
additional allocations to get to the desired state. It is idempotent to
run the same job specification again and no new allocations will be created.
2015-10-10 20:06:00 +00:00
Now, let's try to do an application update. In this case, we will simply change
2015-09-23 01:10:46 +00:00
the version of redis we want to run. Edit the `example.nomad` file and change
the Docker image from "redis:latest" to "redis:2.8":
```
# Configure Docker driver with the image
config {
image = "redis:2.8"
2015-09-22 23:18:26 +00:00
}
2015-09-23 01:10:46 +00:00
```
2015-09-25 18:34:41 +00:00
This time we have not changed the number of task groups we want running,
2015-09-23 01:10:46 +00:00
but we've changed the task itself. This requires stopping the old tasks
2015-09-25 18:34:41 +00:00
and starting new tasks. Our example job is configured to do a rolling update via
the `stagger` attribute, doing a single update every 10 seconds. Use `run` to push the updated
2015-09-23 01:10:46 +00:00
specification now:
2015-09-22 22:57:12 +00:00
```
2015-09-23 01:10:46 +00:00
$ nomad run example.nomad
2016-02-06 00:28:20 +00:00
==> Monitoring evaluation "ebcc3e14"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2016-02-06 00:28:20 +00:00
Allocation "9a3743f4" created: node "171a583b", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2016-02-06 00:28:20 +00:00
==> Evaluation "ebcc3e14" finished with status "complete"
==> Monitoring next evaluation "b508d8f0-7f21-8d66-ec59-7f5b2573435a" in 0
==> Monitoring evaluation "b508d8f0"
Evaluation triggered by job "example"
Allocation "926e5876" created: node "171a583b", group "cache"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "b508d8f0" finished with status "complete"
==> Monitoring next evaluation "ea78c05a-a15f-92ae-8c3d-59f4a1edd091" in 10s
==> Monitoring evaluation "ea78c05a"
Evaluation triggered by job "example"
Allocation "3c8589d5" created: node "171a583b", group "cache"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "ea78c05a" finished with status "complete"
2015-09-23 01:10:46 +00:00
```
We can see that Nomad handled the update in three phases, only updating a single task
group in each phase. The update strategy can be configured, but rolling updates makes
it easy to upgrade an application at large scale.
2015-09-23 01:10:46 +00:00
## Stopping a Job
So far we've created, run and modified a job. The final step in a job lifecycle
is stopping the job. This is done with the [`stop` command](/docs/commands/stop.html):
```
$ nomad stop example
2016-02-06 00:28:20 +00:00
==> Monitoring evaluation "fd03c9f8"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
Evaluation status changed: "pending" -> "complete"
2016-02-06 00:28:20 +00:00
==> Evaluation "fd03c9f8" finished with status "complete"
2015-09-23 01:10:46 +00:00
```
When we stop a job, it creates an evaluation which is used to stop all
the existing allocations. This also deletes the job definition out of Nomad.
If we try to query the job status, we can see it is no longer registered:
```
$ nomad status example
2016-02-06 00:28:20 +00:00
No job(s) with prefix or id "example" found
2015-09-23 01:10:46 +00:00
```
If we wanted to start the job again, we could simply `run` it again.
2015-09-22 22:57:12 +00:00
## Next Steps
2015-09-23 01:10:46 +00:00
Users of Nomad primarily interact with jobs, and we've now seen
how to create and scale our job, perform an application update,
2015-09-23 01:34:30 +00:00
and do a job tear down. Next we will add another Nomad
client to [create our first cluster](cluster.html)
2015-09-22 23:18:26 +00:00