github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/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 transitions 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](/docs/jobspec/interpreted.html). 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 * `logs` - Logs allows configuring log rotation for the `stdout` and `stderr` 269 buffers of a Task. See the log rotation reference below for more details. 270 271 ### Resources 272 273 The `resources` object supports the following keys: 274 275 * `cpu` - The CPU required in MHz. 276 277 * `disk` - The disk required in MB. 278 279 * `iops` - The number of IOPS required given as a weight between 10-1000. 280 281 * `memory` - The memory required in MB. 282 283 * `network` - The network required. Details below. 284 285 The `network` object supports the following keys: 286 287 * `mbits` - The number of MBits in bandwidth required. 288 289 * `port` - `port` is a repeatable object that can be used to specify both 290 dynamic ports and reserved ports. It has the following format: 291 292 ``` 293 port "label" { 294 // If the `static` field is omitted, a dynamic port will be assigned. 295 static = 6539 296 } 297 ``` 298 299 ### Restart Policy 300 301 The `restart` object supports the following keys: 302 303 * `attempts` - `attempts` is the number of restarts allowed in an `interval`. 304 305 * `interval` - `interval` is a time duration that can be specified using the 306 `s`, `m`, and `h` suffixes, such as `30s`. The `interval` begins when the 307 first task starts and ensures that only `attempts` number of restarts happens 308 within it. If more than `attempts` number of failures happen, behavior is 309 controlled by `mode`. 310 311 * `delay` - A duration to wait before restarting a task. It is specified as a 312 time duration using the `s`, `m`, and `h` suffixes, such as `30s`. A random 313 jitter of up to 25% is added to the delay. 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 mode = "delay" 331 } 332 ``` 333 334 The default non-batch restart policy is: 335 336 ``` 337 restart { 338 interval = "1m" 339 attempts = 2 340 delay = "15s" 341 mode = "delay" 342 } 343 ``` 344 345 ### Constraint 346 347 The `constraint` object supports the following keys: 348 349 * `attribute` - Specifies the attribute to examine for the 350 constraint. See the table of attributes [here](/docs/jobspec/interpreted.html#interpreted_node_vars). 351 352 * `operator` - Specifies the comparison operator. Defaults to equality, 353 and can be `=`, `==`, `is`, `!=`, `not`, `>`, `>=`, `<`, `<=`. The 354 ordering is compared lexically. 355 356 * `value` - Specifies the value to compare the attribute against. 357 This can be a literal value or another attribute. 358 359 * `version` - Specifies a version constraint against the attribute. 360 This sets the operator to `version` and the `value` to what is 361 specified. This supports a comma seperated list of constraints, 362 including the pessimistic operator. See the 363 [go-version](https://github.com/hashicorp/go-version) repository 364 for examples. 365 366 * `regexp` - Specifies a regular expression constraint against 367 the attribute. This sets the operator to "regexp" and the `value` 368 to the regular expression. 369 370 * `distinct_hosts` - `distinct_hosts` accepts a boolean `true`. The default is 371 `false`. 372 373 When `distinct_hosts` is `true` at the Job level, each instance of all task 374 Groups specified in the job is placed on a separate host. 375 376 When `distinct_hosts` is `true` at the task group level with count > 1, each 377 instance of a task group is placed on a separate host. Different task groups in 378 the same job _may_ be co-scheduled. 379 380 Tasks within a task group are always co-scheduled. 381 382 ### Log Rotation 383 384 The `logs` object configures the log rotation policy for a task's `stdout` and 385 `stderr`. The `logs` object supports the following keys: 386 387 * `max_files` - The maximum number of rotated files Nomad will retain for 388 `stdout` and `stderr`, each tracked individually. 389 390 * `max_file_size` - The size of each rotated file. The size is specified in 391 `MB`. 392 393 If the amount of disk resource requested for the task is less than the total 394 amount of disk space needed to retain the rotated set of files, Nomad will return 395 a validation error when a job is submitted. 396 397 ``` 398 logs { 399 max_files = 3 400 max_file_size = 10 401 } 402 ``` 403 404 In the above example we have asked Nomad to retain 3 rotated files for both 405 `stderr` and `stdout` and size of each file is 10MB. The minimum disk space that 406 would be required for the task would be 60MB. 407 408 ## JSON Syntax 409 410 Job files can also be specified in JSON. The conversion is straightforward 411 and self-documented. The downsides of JSON are less human readability and 412 the lack of comments. Otherwise, the two are completely interoperable. 413 414 See the API documentation for more details on the JSON structure. 415 416