github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/website/source/docs/jobspec/index.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Job Specification" 4 sidebar_current: "docs-jobspec-syntax" 5 description: |- 6 Learn about the Job specification used to submit jobs to Nomad. 7 --- 8 9 # Job Specification 10 11 Jobs can be specified either in [HCL](https://github.com/hashicorp/hcl) or JSON. 12 HCL is meant to strike a balance between human readable and editable, and machine-friendly. 13 14 For machine-friendliness, Nomad can also read JSON configurations. In general, we recommend 15 using the HCL syntax. 16 17 ## HCL Syntax 18 19 For a detailed description of HCL general syntax, [see this guide](https://github.com/hashicorp/hcl#syntax). 20 Here we cover the details of the Job specification for Nomad: 21 22 ``` 23 # Define a job called my-service 24 job "my-service" { 25 # Job should run in the US region 26 region = "us" 27 28 # Spread tasks between us-west-1 and us-east-1 29 datacenters = ["us-west-1", "us-east-1"] 30 31 # run this with service scheduler 32 type = "service" 33 34 # Rolling updates should be sequential 35 update { 36 stagger = "30s" 37 max_parallel = 1 38 } 39 40 group "webs" { 41 # We want 5 web servers 42 count = 5 43 44 # Create a web front end using a docker image 45 task "frontend" { 46 driver = "docker" 47 config { 48 image = "hashicorp/web-frontend" 49 } 50 service { 51 port = "http" 52 check { 53 type = "http" 54 path = "/health" 55 interval = "10s" 56 timeout = "2s" 57 } 58 } 59 env { 60 DB_HOST = "db01.example.com" 61 DB_USER = "web" 62 DB_PASSWORD = "loremipsum" 63 } 64 resources { 65 cpu = 500 66 memory = 128 67 network { 68 mbits = 100 69 # Request for a dynamic port 70 port "http" { 71 } 72 # Request for a static port 73 port "https" { 74 static = 443 75 } 76 } 77 } 78 } 79 } 80 } 81 ``` 82 83 This is a fairly simple example job, but demonstrates many of the features and syntax 84 of the job specification. The primary "objects" are the job, task group, and task. 85 Each job file has only a single job, however a job may have multiple task groups, 86 and each task group may have multiple tasks. Task groups are a set of tasks that 87 must be co-located on a machine. Groups with a single task and count of one 88 can be declared outside of a group which is created implicitly. 89 90 Constraints can be specified at the job, task group, or task level to restrict 91 where a task is eligible for running. An example constraint looks like: 92 93 ``` 94 # Restrict to only nodes running linux 95 constraint { 96 attribute = "${attr.kernel.name}" 97 value = "linux" 98 } 99 ``` 100 101 Jobs can also specify additional metadata at the job, task group, or task level. 102 This metadata is opaque to Nomad and can be used for any purpose, including 103 defining constraints on the metadata. Metadata can be specified by: 104 105 ``` 106 # Setup ELB via metadata and setup foo 107 meta { 108 foo = "bar" 109 elb_mode = "tcp" 110 elb_check_interval = "10s" 111 } 112 ``` 113 114 ## Syntax Reference 115 116 Following is a syntax reference for the possible keys that are supported 117 and their default values if any for each type of object. 118 119 ### Job 120 121 The `job` object supports the following keys: 122 123 * `all_at_once` - Controls if the entire set of tasks in the job must 124 be placed atomically or if they can be scheduled incrementally. 125 This should only be used for special circumstances. Defaults to `false`. 126 127 * `constraint` - This can be provided multiple times to define additional 128 constraints. See the constraint reference for more details. 129 130 * `datacenters` - A list of datacenters in the region which are eligible 131 for task placement. This must be provided, and does not have a default. 132 133 * `group` - This can be provided multiple times to define additional 134 task groups. See the task group reference for more details. 135 136 * `meta` - Annotates the job with opaque metadata. 137 138 * `priority` - Specifies the job priority which is used to prioritize 139 scheduling and access to resources. Must be between 1 and 100 inclusively, 140 with a larger value corresponding to a higher priority. Defaults to 50. 141 142 * `region` - The region to run the job in, defaults to "global". 143 144 * `task` - This can be specified multiple times to add a task as 145 part of the job. Tasks defined directly in a job are wrapped in 146 a task group of the same name. 147 148 * `type` - Specifies the job type and switches which scheduler 149 is used. Nomad provides the `service`, `system` and `batch` schedulers, 150 and defaults to `service`. To learn more about each scheduler type visit 151 [here](/docs/jobspec/schedulers.html) 152 153 <a id="update"></a> 154 155 * `update` - Specifies the task's update strategy. When omitted, rolling 156 updates are disabled. The `update` block supports the following keys: 157 158 * `max_parallel` - `max_parallel` is given as an integer value and specifies 159 the number of tasks that can be updated at the same time. 160 161 * `stagger` - `stagger` introduces a delay between sets of task updates and 162 is given as an as a time duration. If stagger is provided as an integer, 163 seconds are assumed. Otherwise the "s", "m", and "h" suffix can be used, 164 such as "30s". 165 166 An example `update` block: 167 168 ``` 169 update { 170 // Update 3 tasks at a time. 171 max_parallel = 3 172 173 // Wait 30 seconds between updates. 174 stagger = "30s" 175 } 176 ``` 177 178 * `periodic` - `periodic` allows the job to be scheduled at fixed times, dates 179 or intervals. The periodic expression is always evaluated in the UTC 180 timezone to ensure consistent evaluation when Nomad Servers span multiple 181 time zones. The `periodic` block is optional and supports the following keys: 182 183 * `enabled` - `enabled` determines whether the periodic job will spawn child 184 jobs. `enabled` is defaulted to true if the block is included. 185 186 * `cron` - A cron expression configuring the interval the job is launched 187 at. Supports predefined expressions such as "@daily" and "@weekly" See 188 [here](https://github.com/gorhill/cronexpr#implementation) for full 189 documentation of supported cron specs and the predefined expressions. 190 191 * <a id="prohibit_overlap">`prohibit_overlap`</a> - `prohibit_overlap` can 192 be set to true to enforce that the periodic job doesn't spawn a new 193 instance of the job if any of the previous jobs are still running. It is 194 defaulted to false. 195 196 An example `periodic` block: 197 198 ``` 199 periodic { 200 // Launch every 15 minutes 201 cron = "*/15 * * * * *" 202 203 // Do not allow overlapping runs. 204 prohibit_overlap = true 205 } 206 ``` 207 208 ### Task Group 209 210 The `group` object supports the following keys: 211 212 * `count` - Specifies the number of the task groups that should 213 be running. Must be non-negative, defaults to one. 214 215 * `constraint` - This can be provided multiple times to define additional 216 constraints. See the constraint reference for more details. 217 218 * `restart` - Specifies the restart policy to be applied to tasks in this group. 219 If omitted, a default policy for batch and non-batch jobs is used based on the 220 job type. See the [restart policy reference](#restart_policy) for more details. 221 222 * `task` - This can be specified multiple times, to add a task as 223 part of the group. 224 225 * `meta` - A key/value map that annotates the task group with opaque metadata. 226 227 ### Task 228 229 The `task` object supports the following keys: 230 231 * `driver` - Specifies the task driver that should be used to run the 232 task. See the [driver documentation](/docs/drivers/index.html) for what 233 is available. Examples include `docker`, `qemu`, `java`, and `exec`. 234 235 * `user` - Set the user that will run the task. It defaults to the same user 236 the Nomad client is being run as. This can only be set on Linux platforms. 237 238 * `constraint` - This can be provided multiple times to define additional 239 constraints. See the constraint reference for more details. 240 241 * `config` - A map of key/value configuration passed into the driver 242 to start the task. The details of configurations are specific to 243 each driver. 244 245 * `service` - Nomad integrates with Consul for service discovery. A service 246 block represents a routable and discoverable service on the network. Nomad 247 automatically registers when a task is started and de-registers it when the 248 task transitions to the dead state. [Click 249 here](/docs/jobspec/servicediscovery.html) to learn more about services. 250 251 * `env` - A map of key/value representing environment variables that 252 will be passed along to the running process. Nomad variables are 253 interpreted when set in the environment variable values. See the table of 254 interpreted variables [here](/docs/jobspec/interpreted.html). 255 256 For example the below environment map will be reinterpreted: 257 258 ``` 259 env { 260 // The value will be interpreted by the client and set to the 261 // correct value. 262 NODE_CLASS = "${nomad.class}" 263 } 264 ``` 265 266 * `resources` - Provides the resource requirements of the task. 267 See the [resources reference](#resources) for more details. 268 269 * `meta` - Annotates the task group with opaque metadata. 270 271 <a id="kill_timeout"></a> 272 273 * `kill_timeout` - `kill_timeout` is a time duration that can be specified using 274 the `s`, `m`, and `h` suffixes, such as `30s`. It can be used to configure the 275 time between signaling a task it will be killed and actually killing it. Nomad 276 sends an `os.Interrupt` which on Unix systems is defined as `SIGINT`. After 277 the timeout a kill signal is sent (on Unix `SIGKILL`). The default 278 `kill_timeout` is 5 seconds. 279 280 * `logs` - Logs allows configuring log rotation for the `stdout` and `stderr` 281 buffers of a Task. See the [log rotation section](#log_rotation) for more details. 282 283 * `artifact` - Defines an artifact to be downloaded before the task is run. This 284 can be provided multiple times to define additional artifacts to download. See 285 the artifacts reference for more details. 286 287 ### Resources 288 289 The `resources` object supports the following keys: 290 291 * `cpu` - The CPU required in MHz. Defaults to `100`. 292 293 * `disk` - The disk required in MB. Defaults to `200`. 294 295 * `iops` - The number of IOPS required given as a weight between 10-1000. Defaults to `0`. 296 297 * `memory` - The memory required in MB. Defaults to `300`. 298 299 * `network` - The network required. Details below. 300 301 The `network` object supports the following keys: 302 303 * `mbits` (required) - The number of MBits in bandwidth required. 304 305 * `port` - `port` is a repeatable object that can be used to specify both 306 dynamic ports and reserved ports. It has the following format: 307 308 ``` 309 port "label" { 310 // If the `static` field is omitted, a dynamic port will be assigned. 311 static = 6539 312 } 313 ``` 314 315 <a id="restart_policy"></a> 316 317 ### Restart Policy 318 319 The `restart` object supports the following keys: 320 321 * `attempts` - `attempts` is the number of restarts allowed in an `interval`. 322 323 * `interval` - `interval` is a time duration that can be specified using the 324 `s`, `m`, and `h` suffixes, such as `30s`. The `interval` begins when the 325 first task starts and ensures that only `attempts` number of restarts happens 326 within it. If more than `attempts` number of failures happen, behavior is 327 controlled by `mode`. 328 329 * `delay` - A duration to wait before restarting a task. It is specified as a 330 time duration using the `s`, `m`, and `h` suffixes, such as `30s`. A random 331 jitter of up to 25% is added to the delay. 332 333 * `mode` - Controls the behavior when the task fails more than `attempts` 334 times in an interval. Possible values are listed below: 335 336 * `delay` - `delay` will delay the next restart until the next `interval` is 337 reached. 338 339 * `fail` - `fail` will not restart the task again. 340 341 The default `batch` restart policy is: 342 343 ``` 344 restart { 345 attempts = 15 346 delay = "15s" 347 interval = "168h" # 7 days 348 mode = "delay" 349 } 350 ``` 351 352 The default non-batch restart policy is: 353 354 ``` 355 restart { 356 interval = "1m" 357 attempts = 2 358 delay = "15s" 359 mode = "delay" 360 } 361 ``` 362 363 ### Constraint 364 365 The `constraint` object supports the following keys: 366 367 * `attribute` - Specifies the attribute to examine for the 368 constraint. See the table of attributes [here](/docs/jobspec/interpreted.html#interpreted_node_vars). 369 370 * `operator` - Specifies the comparison operator. Defaults to equality, 371 and can be `=`, `==`, `is`, `!=`, `not`, `>`, `>=`, `<`, `<=`. The 372 ordering is compared lexically. The following are equivalent: 373 374 * `=`, `==` and `is` 375 * `!=` and `not` 376 377 * `value` - Specifies the value to compare the attribute against. 378 This can be a literal value or another attribute. 379 380 * `version` - Specifies a version constraint against the attribute. 381 This sets the operator to `version` and the `value` to what is 382 specified. This supports a comma separated list of constraints, 383 including the pessimistic operator. See the 384 [go-version](https://github.com/hashicorp/go-version) repository 385 for examples. 386 387 * `regexp` - Specifies a regular expression constraint against 388 the attribute. This sets the operator to "regexp" and the `value` 389 to the regular expression. 390 391 * `distinct_hosts` - `distinct_hosts` accepts a boolean value and defaults to 392 `false`. If set, the scheduler will not co-locate any task groups on the same 393 machine. This can be specified as a job constraint which applies the 394 constraint to all task groups in the job, or as a task group constraint which 395 scopes the effect to just that group. 396 397 Placing the constraint at both the job level and at the task group level is 398 redundant since when placed at the job level, the constraint will be applied 399 to all task groups. 400 401 <a id="log_rotation"></a> 402 403 ### Log Rotation 404 405 The `logs` object configures the log rotation policy for a task's `stdout` and 406 `stderr`. The `logs` object supports the following keys: 407 408 * `max_files` - The maximum number of rotated files Nomad will retain for 409 `stdout` and `stderr`, each tracked individually. 410 411 * `max_file_size` - The size of each rotated file. The size is specified in 412 `MB`. 413 414 If the amount of disk resource requested for the task is less than the total 415 amount of disk space needed to retain the rotated set of files, Nomad will return 416 a validation error when a job is submitted. 417 418 ``` 419 logs { 420 max_files = 3 421 max_file_size = 10 422 } 423 ``` 424 425 In the above example we have asked Nomad to retain 3 rotated files for both 426 `stderr` and `stdout` and size of each file is 10MB. The minimum disk space that 427 would be required for the task would be 60MB. 428 429 <a id="artifact_doc"></a> 430 431 ### Artifact 432 433 Nomad downloads artifacts using 434 [`go-getter`](https://github.com/hashicorp/go-getter). The `go-getter` library 435 allows downloading of artifacts from various sources using a URL as the input 436 source. The key/value pairs given in the `options` block map directly to 437 parameters appended to the supplied `source` URL. These are then used by 438 `go-getter` to appropriately download the artifact. `go-getter` also has a CLI 439 tool to validate its URL and can be used to check if the Nomad `artifact` is 440 valid. 441 442 Nomad allows downloading `http`, `https`, and `S3` artifacts. If these artifacts 443 are archives (zip, tar.gz, bz2, etc.), these will be unarchived before the task 444 is started. 445 446 The `artifact` object supports the following keys: 447 448 * `source` - The path to the artifact to download. 449 450 * `destination` - An optional path to download the artifact into relative to the 451 root of the task's directory. If the `destination` key is omitted, it will 452 default to `local/`. 453 454 * `options` - The `options` block allows setting parameters for `go-getter`. 455 Full documentation of supported options are available 456 [here](https://github.com/hashicorp/go-getter/tree/ef5edd3d8f6f482b775199be2f3734fd20e04d4a#protocol-specific-options-1). 457 An example is given below: 458 459 ``` 460 options { 461 # Validate the downloaded artifact 462 checksum = "md5:c4aa853ad2215426eb7d70a21922e794" 463 464 # S3 options for downloading artifacts from S3 465 aws_access_key_id = "<id>" 466 aws_access_key_secret = "<secret>" 467 aws_access_token = "<token>" 468 } 469 ``` 470 471 An example of downloading and unzipping an archive is as simple as: 472 473 ``` 474 artifact { 475 # The archive will be extracted before the task is run, making 476 # it easy to ship configurations with your binary. 477 source = "https://example.com/my.zip" 478 479 options { 480 checksum = "md5:7f4b3e3b4dd5150d4e5aaaa5efada4c3" 481 } 482 } 483 ``` 484 485 #### S3 examples 486 487 S3 has several different types of addressing and more detail can be found 488 [here](http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro) 489 490 S3 region specific endpoints can be found 491 [here](http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region) 492 493 Path based style: 494 ``` 495 artifact { 496 source = "https://s3-us-west-2.amazonaws.com/my-bucket-example/my_app.tar.gz" 497 } 498 ``` 499 500 or to override automatic detection in the URL, use the S3-specific syntax 501 ``` 502 artifact { 503 source = "s3::https://s3-eu-west-1.amazonaws.com/my-bucket-example/my_app.tar.gz" 504 } 505 ``` 506 507 Virtual hosted based style 508 ``` 509 artifact { 510 source = "my-bucket-example.s3-eu-west-1.amazonaws.com/my_app.tar.gz" 511 } 512 ``` 513 514 ## JSON Syntax 515 516 Job files can also be specified in JSON. The conversion is straightforward 517 and self-documented. The downsides of JSON are less human readability and 518 the lack of comments. Otherwise, the two are completely interoperable. 519 520 See the [JSON API documentation](/docs/jobspec/json.html) for more details on 521 the JSON structure.