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

303 lines
10 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 [HashiCorp Configuration Language](https://github.com/hashicorp/hcl) (HCL) or JSON,
2015-09-22 23:18:26 +00:00
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
Summary
Task Group Queued Starting Running Failed Complete Lost
cache 0 0 1 0 0 0
2016-06-25 02:15:55 +00:00
Allocations
2016-08-16 02:40:34 +00:00
ID Eval ID Node ID Task Group Desired Status Created At
dadcdb81 61b0b423 72687b1a cache run running 06/23/16 01:41:13 UTC
2015-09-23 01:10:46 +00:00
```
2016-06-25 02:15:55 +00:00
Here we can see that the result of our evaluation was the creation of an
allocation that is now running on the local node.
2015-09-23 01:10:46 +00:00
An allocation represents an instance of Task Group placed on a node. To inspect
an Allocation we use the [`alloc-status` command](/docs/commands/alloc-status.html):
```
2016-06-25 02:15:55 +00:00
$ nomad alloc-status dadcdb81
ID = dadcdb81
Eval ID = 61b0b423
Name = example.cache[0]
Node ID = 72687b1a
Job ID = example
Client Status = running
2016-08-19 02:25:32 +00:00
Created At = 06/23/16 01:41:13 UTC
2016-06-25 02:15:55 +00:00
Task "redis" is "running"
Task Resources
CPU Memory Disk IOPS Addresses
2/500 6.3 MiB/256 MiB 300 MiB 0 db: 127.0.0.1:30329
2016-06-25 02:15:55 +00:00
Recent Events:
Time Type Description
06/23/16 01:41:16 UTC Started Task started by client
06/23/16 01:41:13 UTC Received Task received by client
```
2016-06-25 02:15:55 +00:00
We can see that Nomad reports the state of the allocation as well as its
current resource usage. By supplying the `-stats` flag, more detailed resource
usage statistics will be reported.
2016-07-25 20:29:19 +00:00
To see the logs of a task, we can use the [logs command](/docs/commands/logs.html):
```
2016-07-25 20:29:19 +00:00
$ nomad logs 8ba85cef redis
2016-06-25 02:15:55 +00:00
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.1 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
...
```
2015-09-23 01:10:46 +00:00
## 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
```
2016-06-25 02:15:55 +00:00
Once you have finished modifying the job specification, use [`plan`
command](/docs/commands/plan.html) to invoke a dry-run of the scheduler to see
what would happen if you ran the updated job:
2015-09-23 01:10:46 +00:00
```
2016-06-25 02:15:55 +00:00
$ nomad plan example.nomad
+/- Job: "example"
+/- Task Group: "cache" (2 create, 1 in-place update)
+/- Count: "1" => "3" (forces create)
Task: "redis"
Scheduler dry-run:
- All tasks successfully allocated.
Job Modify Index: 6
To submit the job with version verification run:
nomad run -check-index 6 example.nomad
When running the job with the check-index flag, the job will only be run if the
server side version matches the job modify index returned. If the index has
2016-06-25 02:15:55 +00:00
changed, another user has modified the job and the plan's results are
potentially invalid.
```
We can see that the scheduler detected the change in count and informs us that
it will cause 2 new instances to be created. The in-place update that will
occur is to push the update job specification to the existing allocation and
2016-07-18 14:24:30 +00:00
will not cause any service interruption. We can then run the job with the
2016-06-25 02:15:55 +00:00
run command the `plan` emitted.
By running with the `-check-index` flag, Nomad checks that the job has not
been modified since the plan was run. This is useful if multiple people are
interacting with the job at the same time to ensure the job hasn't changed
before you apply your modifications.
```
$ nomad run -check-index 6 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
```
2016-06-25 02:15:55 +00:00
We can run `plan` again to see what will happen if we submit this change:
```
$ nomad plan example.nomad
+/- Job: "example"
+/- Task Group: "cache" (3 create/destroy update)
+/- Task: "redis" (forces create/destroy update)
+/- Config {
+/- image: "redis:latest" => "redis:2.8"
port_map[0][db]: "6379"
}
Scheduler dry-run:
- All tasks successfully allocated.
- Rolling update, next evaluation will be in 10s.
Job Modify Index: 42
To submit the job with version verification run:
nomad run -check-index 42 example.nomad
When running the job with the check-index flag, the job will only be run if the
server side version matches the job modify index returned. If the index has
2016-06-25 02:15:55 +00:00
changed, another user has modified the job and the plan's results are
potentially invalid.
```
Here we can see the `plan` reports it will do three create/destroy updates
which stops the old tasks and starts the new tasks because we have changed the
version of redis to run. We also see that the update will happen with a rolling
update. This is because our example job is configured to do a rolling update
via the `stagger` attribute, doing a single update every 10 seconds.
Once ready, use `run` to push the updated 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 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"
2016-02-06 23:40:39 +00:00
==> Monitoring next evaluation "ea78c05a" in 10s
2016-02-06 00:28:20 +00:00
==> 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