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

179 lines
6.8 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
2015-09-28 00:04:34 +00:00
==> Monitoring evaluation "3d823c52-929a-fa8b-c50d-1ac4d00cf6b7"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2015-09-28 00:04:34 +00:00
Allocation "85b839d7-f67a-72a4-5a13-104020ae4807" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2015-09-28 00:04:34 +00:00
==> Evaluation "3d823c52-929a-fa8b-c50d-1ac4d00cf6b7" 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
2015-09-28 00:04:34 +00:00
Status = <none>
2015-09-23 01:10:46 +00:00
==> Evaluations
ID Priority TriggeredBy Status
2015-09-28 00:04:34 +00:00
3d823c52-929a-fa8b-c50d-1ac4d00cf6b7 50 job-register complete
2015-09-23 01:10:46 +00:00
==> Allocations
ID EvalID NodeID TaskGroup Desired Status
2015-09-28 00:04:34 +00:00
85b839d7-f67a-72a4-5a13-104020ae4807 3d823c52-929a-fa8b-c50d-1ac4d00cf6b7 2512929f-5b7c-a959-dfd9-bf8a8eb022a6 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
2015-09-28 00:04:34 +00:00
==> Monitoring evaluation "ec199c63-2022-f5c7-328d-1cf85e61bf66"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2015-09-28 00:04:34 +00:00
Allocation "21551679-5224-cb6b-80a2-d0b091612d2e" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
Allocation "b1be1410-a01c-20ad-80ff-96750ec0f1da" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
Allocation "ed32a35d-8086-3f04-e299-4432e562cbf2" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2015-09-28 00:04:34 +00:00
==> Evaluation "ec199c63-2022-f5c7-328d-1cf85e61bf66" 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
2015-09-28 00:04:34 +00:00
==> Monitoring evaluation "d34d37f4-19b1-f4c0-b2da-c949e6ade82d"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
2015-09-28 00:04:34 +00:00
Allocation "5614feb0-212d-21e5-ccfb-56a394fc41d5" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
Allocation "bf7e3ad5-b217-14fe-f3f8-2b83af9dbb42" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
Allocation "e3978af2-f61e-c601-7aa1-90aea9b23cf6" created: node "2512929f-5b7c-a959-dfd9-bf8a8eb022a6", group "cache"
2015-09-23 01:10:46 +00:00
Evaluation status changed: "pending" -> "complete"
2015-09-28 00:04:34 +00:00
==> Evaluation "d34d37f4-19b1-f4c0-b2da-c949e6ade82d" 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
2015-09-28 00:04:34 +00:00
==> Monitoring evaluation "bb407de4-02cb-f009-d986-646d6c11366d"
2015-09-23 01:10:46 +00:00
Evaluation triggered by job "example"
Evaluation status changed: "pending" -> "complete"
2015-09-28 00:04:34 +00:00
==> Evaluation "bb407de4-02cb-f009-d986-646d6c11366d" 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
Error querying job: Unexpected response code: 404 (job not found)
```
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