github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/website/source/docs/jobspec/index.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Job Specification" 4 sidebar_current: "docs-jobspec" 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 update strategy. This requires providing 154 `max_parallel` as an integer and `stagger` as a time duration. If stagger 155 is provided as an integer, seconds are assumed. Otherwise the "s", "m", 156 and "h" suffix can be used, such as "30s". Both values default to zero, 157 which disables rolling updates. 158 159 ### Task Group 160 161 The `group` object supports the following keys: 162 163 * `count` - Specifies the number of the task groups that should 164 be running. Must be positive, defaults to one. 165 166 * `constraint` - This can be provided multiple times to define additional 167 constraints. See the constraint reference for more details. 168 169 * `restart` - Specifies the restart policy to be applied to tasks in this group. 170 If omitted, a default policy for batch and non-batch jobs is used based on the 171 job type. See the restart policy reference for more details. 172 173 * `task` - This can be specified multiple times, to add a task as 174 part of the group. 175 176 * `meta` - Annotates the task group with opaque metadata. 177 178 ### Task 179 180 The `task` object supports the following keys: 181 182 * `driver` - Specifies the task driver that should be used to run the 183 task. See the [driver documentation](/docs/drivers/index.html) for what 184 is available. Examples include `docker`, `qemu`, `java`, and `exec`. 185 186 * `constraint` - This can be provided multiple times to define additional 187 constraints. See the constraint reference for more details. 188 189 * `config` - A map of key/value configuration passed into the driver 190 to start the task. The details of configurations are specific to 191 each driver. 192 193 * `service` - Nomad integrates with Consul for Service Discovery. A service 194 block represents a routable and discoverable service on the network. Nomad 195 automatically registers when a Task is started and de-registers it when the 196 Task transitons to the DEAD state. To learn more about Services please visit [here](/docs/jobspec/servicediscovery.html) 197 198 * `env` - A map of key/value representing environment variables that 199 will be passed along to the running process. 200 201 * `resources` - Provides the resource requirements of the task. 202 See the resources reference for more details. 203 204 * `meta` - Annotates the task group with opaque metadata. 205 206 ### Resources 207 208 The `resources` object supports the following keys: 209 210 * `cpu` - The CPU required in MHz. 211 212 * `disk` - The disk required in MB. 213 214 * `iops` - The number of IOPS required given as a weight between 10-1000. 215 216 * `memory` - The memory required in MB. 217 218 * `network` - The network required. Details below. 219 220 The `network` object supports the following keys: 221 222 * `dynamic_ports` - List of port labels which may contain letters, 223 numbers and underscores (`^[a-zA-Z0-9_]+$`). Each label will be assigned a 224 dynamic port when the task starts. Ports are passed to the task environment as 225 `NOMAD_PORT_{LABEL}`. Drivers may infer additional semantics from the label. 226 See the relevant driver docs for details. 227 228 * `mbits` - The number of MBits in bandwidth required. 229 230 * `reserved_ports` - This is a list of specific ports required. 231 For applications that cannot use a dynamic port, they can 232 request a specific port. 233 234 ### Restart Policy 235 236 The `restart` object supports the following keys: 237 238 * `attempts` - For `batch` jobs, `attempts` is the maximum number of restarts 239 allowed before the task is failed. For non-batch jobs, the `attempts` is the 240 number of restarts allowed in an `interval` before a restart delay is added. 241 242 * `interval` - `interval` is only valid on non-batch jobs and is a time duration 243 that can be specified using the `s`, `m`, and `h` suffixes, such as `30s`. 244 The `interval` begins when the first task starts and ensures that only 245 `attempts` number of restarts happens within it. If more than `attempts` 246 number of failures happen, the restart is delayed till after the `interval`, 247 which is then reset. 248 249 * `delay` - A duration to wait before restarting a task. It is specified as a 250 time duration using the `s`, `m`, and `h` suffixes, such as `30s`. 251 252 The default `batch` restart policy is: 253 254 ``` 255 restart { 256 attempts = 15 257 delay = "15s" 258 } 259 ``` 260 261 The default non-batch restart policy is: 262 263 ``` 264 restart { 265 interval = "1m" 266 attempts = 2 267 delay = "15s" 268 } 269 ``` 270 271 ### Constraint 272 273 The `constraint` object supports the following keys: 274 275 * `attribute` - Specifies the attribute to examine for the 276 constraint. See the table of attributes below. 277 278 * `operator` - Specifies the comparison operator. Defaults to equality, 279 and can be `=`, `==`, `is`, `!=`, `not`, `>`, `>=`, `<`, `<=`. The 280 ordering is compared lexically. 281 282 * `value` - Specifies the value to compare the attribute against. 283 This can be a literal value or another attribute. 284 285 * `version` - Specifies a version constraint against the attribute. 286 This sets the operator to `version` and the `value` to what is 287 specified. This supports a comma seperated list of constraints, 288 including the pessimistic operator. See the 289 [go-version](https://github.com/hashicorp/go-version) repository 290 for examples. 291 292 * `regexp` - Specifies a regular expression constraint against 293 the attribute. This sets the operator to "regexp" and the `value` 294 to the regular expression. 295 296 * `distinct_hosts` - `distinct_hosts` accepts a boolean `true`. The default is 297 `false`. 298 299 When `distinct_hosts` is `true` at the Job level, each instance of all Task 300 Groups specified in the job is placed on a separate host. 301 302 When `distinct_hosts` is `true` at the Task Group level with count > 1, each 303 instance of a Task Group is placed on a separate host. Different task groups in 304 the same job _may_ be co-scheduled. 305 306 Tasks within a task group are always co-scheduled. 307 308 Below is a table documenting the variables that can be interpreted: 309 310 <table class="table table-bordered table-striped"> 311 <tr> 312 <th>Variable</th> 313 <th>Description</th> 314 </tr> 315 <tr> 316 <td>$node.id</td> 317 <td>The client node identifier</td> 318 </tr> 319 <tr> 320 <td>$node.datacenter</td> 321 <td>The client node datacenter</td> 322 </tr> 323 <tr> 324 <td>$node.name</td> 325 <td>The client node name</td> 326 </tr> 327 <tr> 328 <td>$attr.\<key\></td> 329 <td>The attribute given by `key` on the client node.</td> 330 </tr> 331 <tr> 332 <td>$meta.\<key\></td> 333 <td>The metadata value given by `key` on the client node.</td> 334 </tr> 335 </table> 336 337 Below is a table documenting common node attributes: 338 339 <table class="table table-bordered table-striped"> 340 <tr> 341 <th>Attribute</th> 342 <th>Description</th> 343 </tr> 344 <tr> 345 <td>arch</td> 346 <td>CPU architecture of the client. Examples: `amd64`, `386`</td> 347 </tr> 348 <tr> 349 <td>consul.datacenter</td> 350 <td>The Consul datacenter of the client node if Consul found</td> 351 </tr> 352 <tr> 353 <td>cpu.numcores</td> 354 <td>Number of CPU cores on the client</td> 355 </tr> 356 <tr> 357 <td>driver.\<key\></td> 358 <td>See the [task drivers](/docs/drivers/index.html) for attribute documentation</td> 359 </tr> 360 <tr> 361 <td>hostname</td> 362 <td>Hostname of the client</td> 363 </tr> 364 <tr> 365 <td>kernel.name</td> 366 <td>Kernel of the client. Examples: `linux`, `darwin`</td> 367 </tr> 368 <tr> 369 <td>kernel.version</td> 370 <td>Version of the client kernel. Examples: `3.19.0-25-generic`, `15.0.0`</td> 371 </tr> 372 <tr> 373 <td>platform.aws.ami-id</td> 374 <td>On EC2, the AMI ID of the client node</td> 375 </tr> 376 <tr> 377 <td>platform.aws.instance-type</td> 378 <td>On EC2, the instance type of the client node</td> 379 </tr> 380 <tr> 381 <td>os.name</td> 382 <td>Operating system of the client. Examples: `ubuntu`, `windows`, `darwin`</td> 383 </tr> 384 <tr> 385 <td>os.version</td> 386 <td>Version of the client OS</td> 387 </tr> 388 </table> 389 390 ## JSON Syntax 391 392 Job files can also be specified in JSON. The conversion is straightforward 393 and self-documented. The downsides of JSON are less human readability and 394 the lack of comments. Otherwise, the two are completely interoperable. 395 396 See the API documentation for more details on the JSON structure. 397 398