github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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 job globally 32 type = "system" 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 and 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 * `update` - Specifies the task's update strategy. When omitted, rolling 154 updates are disabled. The `update` block supports the following keys: 155 156 * `max_parallel` - `max_parallel` is given as an integer value and specifies 157 the number of tasks that can be updated at the same time. 158 159 * `stagger` - `stagger` introduces a delay between sets of task updates and 160 is given as an as a time duration. If stagger is provided as an integer, 161 seconds are assumed. Otherwise the "s", "m", and "h" suffix can be used, 162 such as "30s". 163 164 An example `update` block: 165 166 ``` 167 update { 168 // Update 3 tasks at a time. 169 max_parallel = 3 170 171 // Wait 30 seconds between updates. 172 stagger = "30s" 173 } 174 ``` 175 176 * `periodic` - `periodic` allows the job to be scheduled at fixed times, dates 177 or intervals. The `periodic` block supports the following keys: 178 179 * `enabled` - `enabled` determines whether the periodic job will spawn child 180 jobs. `enabled` is defaulted to true if the block is included. 181 182 * `cron` - A cron expression configuring the interval the job is launched 183 at. Supports predefined expressions such as "@daily" and "@weekly" See 184 [here](https://github.com/gorhill/cronexpr#implementation) for full 185 documentation of supported cron specs and the predefined expressions. 186 187 * <a id="prohibit_overlap">`prohibit_overlap`</a> - `prohibit_overlap` can 188 be set to true to enforce that the periodic job doesn't spawn a new 189 instance of the job if any of the previous jobs are still running. It is 190 defaulted to false. 191 192 An example `periodic` block: 193 194 ``` 195 periodic { 196 // Launch every 15 minutes 197 cron = "*/15 * * * * *" 198 199 // Do not allow overlapping runs. 200 prohibit_overlap = true 201 } 202 ``` 203 204 ### Task Group 205 206 The `group` object supports the following keys: 207 208 * `count` - Specifies the number of the task groups that should 209 be running. Must be positive, defaults to one. 210 211 * `constraint` - This can be provided multiple times to define additional 212 constraints. See the constraint reference for more details. 213 214 * `restart` - Specifies the restart policy to be applied to tasks in this group. 215 If omitted, a default policy for batch and non-batch jobs is used based on the 216 job type. See the restart policy reference for more details. 217 218 * `task` - This can be specified multiple times, to add a task as 219 part of the group. 220 221 * `meta` - Annotates the task group with opaque metadata. 222 223 ### Task 224 225 The `task` object supports the following keys: 226 227 * `driver` - Specifies the task driver that should be used to run the 228 task. See the [driver documentation](/docs/drivers/index.html) for what 229 is available. Examples include `docker`, `qemu`, `java`, and `exec`. 230 231 * `constraint` - This can be provided multiple times to define additional 232 constraints. See the constraint reference for more details. 233 234 * `config` - A map of key/value configuration passed into the driver 235 to start the task. The details of configurations are specific to 236 each driver. 237 238 * `service` - Nomad integrates with Consul for service discovery. A service 239 block represents a routable and discoverable service on the network. Nomad 240 automatically registers when a task is started and de-registers it when the 241 task transitons to the dead state. [Click 242 here](/docs/jobspec/servicediscovery.html) to learn more about services. 243 244 * `env` - A map of key/value representing environment variables that 245 will be passed along to the running process. Nomad variables are 246 interpreted when set in the environment variable values. See the table of 247 interpreted variables [here](#interpreted_vars). 248 249 For example the below environment map will be reinterpreted: 250 251 ``` 252 env { 253 // The value will be interpreted by the client and set to the 254 // correct value. 255 NODE_CLASS = "$nomad.class" 256 } 257 ``` 258 259 * `resources` - Provides the resource requirements of the task. 260 See the resources reference for more details. 261 262 * `meta` - Annotates the task group with opaque metadata. 263 264 * `kill_timeout` - `kill_timeout` is a time duration that can be specified using 265 the `s`, `m`, and `h` suffixes, such as `30s`. It can be used to configure the 266 time between signaling a task it will be killed and actually killing it. 267 268 ### Resources 269 270 The `resources` object supports the following keys: 271 272 * `cpu` - The CPU required in MHz. 273 274 * `disk` - The disk required in MB. 275 276 * `iops` - The number of IOPS required given as a weight between 10-1000. 277 278 * `memory` - The memory required in MB. 279 280 * `network` - The network required. Details below. 281 282 The `network` object supports the following keys: 283 284 * `mbits` - The number of MBits in bandwidth required. 285 286 * `port` - `port` is a repeatable object that can be used to specify both 287 dynamic ports and reserved ports. It has the following format: 288 289 ``` 290 port "label" { 291 // If the `static` field is omitted, a dynamic port will be assigned. 292 static = 6539 293 } 294 ``` 295 296 ### Restart Policy 297 298 The `restart` object supports the following keys: 299 300 * `attempts` - `attempts` is the number of restarts allowed in an `interval`. 301 302 * `interval` - `interval` is a time duration that can be specified using the 303 `s`, `m`, and `h` suffixes, such as `30s`. The `interval` begins when the 304 first task starts and ensures that only `attempts` number of restarts happens 305 within it. If more than `attempts` number of failures happen, behavior is 306 controlled by `mode`. 307 308 * `delay` - A duration to wait before restarting a task. It is specified as a 309 time duration using the `s`, `m`, and `h` suffixes, such as `30s`. A random 310 jitter of up to 25% is added to the the delay. 311 312 * `on_success` - `on_success` controls whether a task is restarted when the 313 task exits successfully. 314 315 * `mode` - Controls the behavior when the task fails more than `attempts` 316 times in an interval. Possible values are listed below: 317 318 * `delay` - `delay` will delay the next restart until the next `interval` is 319 reached. 320 321 * `fail` - `fail` will not restart the task again. 322 323 The default `batch` restart policy is: 324 325 ``` 326 restart { 327 attempts = 15 328 delay = "15s" 329 interval = "168h" # 7 days 330 on_success = false 331 mode = "delay" 332 } 333 ``` 334 335 The default non-batch restart policy is: 336 337 ``` 338 restart { 339 interval = "1m" 340 attempts = 2 341 delay = "15s" 342 on_success = true 343 mode = "delay" 344 } 345 ``` 346 347 ### Constraint 348 349 The `constraint` object supports the following keys: 350 351 * `attribute` - Specifies the attribute to examine for the 352 constraint. See the table of attributes [below](#interpreted_vars). 353 354 * `operator` - Specifies the comparison operator. Defaults to equality, 355 and can be `=`, `==`, `is`, `!=`, `not`, `>`, `>=`, `<`, `<=`. The 356 ordering is compared lexically. 357 358 * `value` - Specifies the value to compare the attribute against. 359 This can be a literal value or another attribute. 360 361 * `version` - Specifies a version constraint against the attribute. 362 This sets the operator to `version` and the `value` to what is 363 specified. This supports a comma seperated list of constraints, 364 including the pessimistic operator. See the 365 [go-version](https://github.com/hashicorp/go-version) repository 366 for examples. 367 368 * `regexp` - Specifies a regular expression constraint against 369 the attribute. This sets the operator to "regexp" and the `value` 370 to the regular expression. 371 372 * `distinct_hosts` - `distinct_hosts` accepts a boolean `true`. The default is 373 `false`. 374 375 When `distinct_hosts` is `true` at the Job level, each instance of all task 376 Groups specified in the job is placed on a separate host. 377 378 When `distinct_hosts` is `true` at the task group level with count > 1, each 379 instance of a task group is placed on a separate host. Different task groups in 380 the same job _may_ be co-scheduled. 381 382 Tasks within a task group are always co-scheduled. 383 384 ### Interpreted Variables <a id="interpreted_vars"></a> 385 386 Certain Nomad variables are interpretable for use in constraints, task 387 environment variables and task arguments. Below is a table documenting the 388 variables that can be interpreted: 389 390 <table class="table table-bordered table-striped"> 391 <tr> 392 <th>Variable</th> 393 <th>Description</th> 394 </tr> 395 <tr> 396 <td>$node.id</td> 397 <td>The client node identifier</td> 398 </tr> 399 <tr> 400 <td>$node.datacenter</td> 401 <td>The client node datacenter</td> 402 </tr> 403 <tr> 404 <td>$node.name</td> 405 <td>The client node name</td> 406 </tr> 407 <tr> 408 <td>$node.class</td> 409 <td>The client node class</td> 410 </tr> 411 <tr> 412 <td>$attr.\<key\></td> 413 <td>The attribute given by `key` on the client node.</td> 414 </tr> 415 <tr> 416 <td>$meta.\<key\></td> 417 <td>The metadata value given by `key` on the client node.</td> 418 </tr> 419 </table> 420 421 Below is a table documenting common node attributes: 422 423 <table class="table table-bordered table-striped"> 424 <tr> 425 <th>Attribute</th> 426 <th>Description</th> 427 </tr> 428 <tr> 429 <td>arch</td> 430 <td>CPU architecture of the client. Examples: `amd64`, `386`</td> 431 </tr> 432 <tr> 433 <td>consul.datacenter</td> 434 <td>The Consul datacenter of the client node if Consul found</td> 435 </tr> 436 <tr> 437 <td>cpu.numcores</td> 438 <td>Number of CPU cores on the client</td> 439 </tr> 440 <tr> 441 <td>driver.\<key\></td> 442 <td>See the [task drivers](/docs/drivers/index.html) for attribute documentation</td> 443 </tr> 444 <tr> 445 <td>hostname</td> 446 <td>Hostname of the client</td> 447 </tr> 448 <tr> 449 <td>kernel.name</td> 450 <td>Kernel of the client. Examples: `linux`, `darwin`</td> 451 </tr> 452 <tr> 453 <td>kernel.version</td> 454 <td>Version of the client kernel. Examples: `3.19.0-25-generic`, `15.0.0`</td> 455 </tr> 456 <tr> 457 <td>platform.aws.ami-id</td> 458 <td>On EC2, the AMI ID of the client node</td> 459 </tr> 460 <tr> 461 <td>platform.aws.instance-type</td> 462 <td>On EC2, the instance type of the client node</td> 463 </tr> 464 <tr> 465 <td>os.name</td> 466 <td>Operating system of the client. Examples: `ubuntu`, `windows`, `darwin`</td> 467 </tr> 468 <tr> 469 <td>os.version</td> 470 <td>Version of the client OS</td> 471 </tr> 472 </table> 473 474 ## JSON Syntax 475 476 Job files can also be specified in JSON. The conversion is straightforward 477 and self-documented. The downsides of JSON are less human readability and 478 the lack of comments. Otherwise, the two are completely interoperable. 479 480 See the API documentation for more details on the JSON structure. 481 482