github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/website/source/docs/job-specification/service.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "service Stanza - Job Specification" 4 sidebar_current: "docs-job-specification-service" 5 description: |- 6 The "service" stanza instructs Nomad to register the task as a service using 7 the service discovery integration. 8 --- 9 10 # `service` Stanza 11 12 <table class="table table-bordered table-striped"> 13 <tr> 14 <th width="120">Placement</th> 15 <td> 16 <code>job -> group -> task -> **service**</code> 17 </td> 18 </tr> 19 </table> 20 21 The `service` stanza instructs Nomad to register the task as a service using the 22 service discovery integration. This section of the documentation will discuss the 23 configuration, but please also read the 24 [Nomad service discovery documentation][service-discovery] for more detailed 25 information about the integration. 26 27 ```hcl 28 job "docs" { 29 group "example" { 30 task "server" { 31 service { 32 tags = ["leader", "mysql"] 33 34 port = "db" 35 36 check { 37 type = "tcp" 38 port = "db" 39 interval = "10s" 40 timeout = "2s" 41 } 42 43 check { 44 type = "script" 45 name = "check_table" 46 command = "/usr/local/bin/check_mysql_table_status" 47 args = ["--verbose"] 48 interval = "60s" 49 timeout = "5s" 50 51 check_restart { 52 limit = 3 53 grace = "90s" 54 ignore_warnings = false 55 } 56 } 57 } 58 } 59 } 60 } 61 ``` 62 63 This section of the documentation only covers the job file options for 64 configuring service discovery. For more information on the setup and 65 configuration to integrate Nomad with service discovery, please see the 66 [Nomad service discovery documentation][service-discovery]. There are steps you 67 must take to configure Nomad. Simply adding this configuration to your job file 68 does not automatically enable service discovery. 69 70 ## `service` Parameters 71 72 - `check` <code>([Check](#check-parameters): nil)</code> - Specifies a health 73 check associated with the service. This can be specified multiple times to 74 define multiple checks for the service. At this time, Nomad supports the 75 `script`<sup><small>1</small></sup>, `http` and `tcp` checks. 76 77 - `name` `(string: "<job>-<group>-<task>")` - Specifies the name this service 78 will be advertised as in Consul. If not supplied, this will default to the 79 name of the job, group, and task concatenated together with a dash, like 80 `"docs-example-server"`. Each service must have a unique name within the 81 cluster. Names must adhere to [RFC-1123 82 §2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited to 83 alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64 84 characters in length. 85 86 In addition to the standard [Nomad interpolation][interpolation], the 87 following keys are also available: 88 89 - `${JOB}` - the name of the job 90 - `${GROUP}` - the name of the group 91 - `${TASK}` - the name of the task 92 - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}` 93 94 Validation of the name occurs in two parts. When the job is registered, an initial validation pass checks that 95 the service name adheres to RFC-1123 §2.1 and the length limit, excluding any variables requiring interpolation. 96 Once the client receives the service and all interpretable values are available, the service name will be 97 interpolated and revalidated. This can cause certain service names to pass validation at submit time but fail 98 at runtime. 99 100 - `port` `(string: <optional>)` - Specifies the label of the port on which this 101 service is running. Note this is the _label_ of the port and not the port 102 number unless `address_mode = driver`. The port label must match one defined 103 in the [`network`][network] stanza unless you're also using 104 `address_mode="driver"`. Numeric ports may be used when in driver addressing 105 mode. 106 107 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 108 this service. If this is not supplied, no tags will be assigned to the service 109 when it is registered. 110 111 - `address_mode` `(string: "auto")` - Specifies what address (host or 112 driver-specific) this service should advertise. This setting is supported in 113 Docker since Nomad 0.6 and rkt since Nomad 0.7. See [below for 114 examples.](#using-driver-address-mode) Valid options are: 115 116 - `auto` - Allows the driver to determine whether the host or driver address 117 should be used. Defaults to `host` and only implemented by Docker. If you 118 use a Docker network plugin such as weave, Docker will automatically use 119 its address. 120 121 - `driver` - Use the IP specified by the driver, and the port specified in a 122 port map. A numeric port may be specified since port maps aren't required 123 by all network plugins. Useful for advertising SDN and overlay network 124 addresses. Task will fail if driver network cannot be determined. Only 125 implemented for Docker and rkt. 126 127 - `host` - Use the host IP and port. 128 129 ### `check` Parameters 130 131 Note that health checks run inside the task. If your task is a Docker container, 132 the script will run inside the Docker container. If your task is running in a 133 chroot, it will run in the chroot. Please keep this in mind when authoring check 134 scripts. 135 136 - `address_mode` `(string: "host")` - Same as `address_mode` on `service`. 137 Unlike services, checks do not have an `auto` address mode as there's no way 138 for Nomad to know which is the best address to use for checks. Consul needs 139 access to the address for any HTTP or TCP checks. Added in Nomad 0.7.1. See 140 [below for details.](#using-driver-address-mode) Unlike `port`, this setting 141 is *not* inherited from the `service`. 142 143 - `args` `(array<string>: [])` - Specifies additional arguments to the 144 `command`. This only applies to script-based health checks. 145 146 - `check_restart` - See [`check_restart` stanza][check_restart_stanza]. 147 148 - `command` `(string: <varies>)` - Specifies the command to run for performing 149 the health check. The script must exit: 0 for passing, 1 for warning, or any 150 other value for a failing health check. This is required for script-based 151 health checks. 152 153 ~> **Caveat:** The command must be the path to the command on disk, and no 154 shell exists by default. That means operators like `||` or `&&` are not 155 available. Additionally, all arguments must be supplied via the `args` 156 parameter. To achieve the behavior of shell operators, specify the command 157 as a shell, like `/bin/bash` and then use `args` to run the check. 158 159 - `initial_status` `(string: <enum>)` - Specifies the originating status of the 160 service. Valid options are the empty string, `passing`, `warning`, and 161 `critical`. 162 163 - `interval` `(string: <required>)` - Specifies the frequency of the health checks 164 that Consul will perform. This is specified using a label suffix like "30s" 165 or "1h". This must be greater than or equal to "1s" 166 167 - `method` `(string: "GET")` - Specifies the HTTP method to use for HTTP 168 checks. 169 170 - `name` `(string: "service: <name> check")` - Specifies the name of the health 171 check. If the name is not specified Nomad generates one based on the service name. 172 If you have more than one check you must specify the name. 173 174 - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which 175 Consul will query to query the health of a service. Nomad will automatically 176 add the IP of the service and the port, so this is just the relative URL to 177 the health check endpoint. This is required for http-based health checks. 178 179 - `port` `(string: <varies>)` - Specifies the label of the port on which the 180 check will be performed. Note this is the _label_ of the port and not the port 181 number unless `address_mode = driver`. The port label must match one defined 182 in the [`network`][network] stanza. If a port value was declared on the 183 `service`, this will inherit from that value if not supplied. If supplied, 184 this value takes precedence over the `service.port` value. This is useful for 185 services which operate on multiple ports. `http` and `tcp` checks require a 186 port while `script` checks do not. Checks will use the host IP and ports by 187 default. In Nomad 0.7.1 or later numeric ports may be used if 188 `address_mode="driver"` is set on the check. 189 190 - `protocol` `(string: "http")` - Specifies the protocol for the http-based 191 health checks. Valid options are `http` and `https`. 192 193 - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a 194 health check query to succeed. This is specified using a label suffix like 195 "30s" or "1h". This must be greater than or equal to "1s" 196 197 - `type` `(string: <required>)` - This indicates the check types supported by 198 Nomad. Valid options are `script`, `http`, and `tcp`. 199 200 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 201 checks. Requires Consul >= 0.7.2. 202 203 #### `header` Stanza 204 205 HTTP checks may include a `header` stanza to set HTTP headers. The `header` 206 stanza parameters have lists of strings as values. Multiple values will cause 207 the header to be set multiple times, once for each value. 208 209 ```hcl 210 service { 211 # ... 212 check { 213 type = "http" 214 port = "lb" 215 path = "/_healthz" 216 interval = "5s" 217 timeout = "2s" 218 header { 219 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 220 } 221 } 222 } 223 ``` 224 225 226 ## `service` Examples 227 228 The following examples only show the `service` stanzas. Remember that the 229 `service` stanza is only valid in the placements listed above. 230 231 ### Basic Service 232 233 This example registers a service named "load-balancer" with no health checks. 234 235 ```hcl 236 service { 237 name = "load-balancer" 238 port = "lb" 239 } 240 ``` 241 242 This example must be accompanied by a [`network`][network] stanza which defines 243 a static or dynamic port labeled "lb". For example: 244 245 ```hcl 246 resources { 247 network { 248 mbits = 10 249 port "lb" {} 250 } 251 } 252 ``` 253 254 ### Check with Bash-isms 255 256 This example shows a common mistake and correct behavior for custom checks. 257 Suppose a health check like this: 258 259 ```shell 260 $ test -f /tmp/file.txt 261 ``` 262 263 In this example `test` is not actually a command (binary) on the system; it is a 264 built-in shell function to bash. Thus, the following **would not work**: 265 266 ```hcl 267 service { 268 check { 269 type = "script" 270 command = "test -f /tmp/file.txt" # THIS IS NOT CORRECT 271 } 272 } 273 ``` 274 275 Nomad will attempt to find an executable named `test` on your system, but it 276 does not exist. It is actually just a function of bash. Additionally, it is not 277 possible to specify the arguments in a single string. Here is the correct 278 solution: 279 280 ```hcl 281 service { 282 check { 283 type = "script" 284 command = "/bin/bash" 285 args = ["-c", "test -f /tmp/file.txt"] 286 } 287 } 288 ``` 289 290 The `command` is actually `/bin/bash`, since that is the actual process we are 291 running. The arguments to that command are the script itself, which each 292 argument provided as a value to the `args` array. 293 294 ### HTTP Health Check 295 296 This example shows a service with an HTTP health check. This will query the 297 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 298 giving the service a maximum of 2 seconds to return a response, and include an 299 Authorization header. Any non-2xx code is considered a failure. 300 301 ```hcl 302 service { 303 check { 304 type = "http" 305 port = "lb" 306 path = "/_healthz" 307 interval = "5s" 308 timeout = "2s" 309 header { 310 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 311 } 312 } 313 } 314 ``` 315 316 ### Multiple Health Checks 317 318 This example shows a service with multiple health checks defined. All health 319 checks must be passing in order for the service to register as healthy. 320 321 ```hcl 322 service { 323 check { 324 name = "HTTP Check" 325 type = "http" 326 port = "lb" 327 path = "/_healthz" 328 interval = "5s" 329 timeout = "2s" 330 } 331 332 check { 333 name = "HTTPS Check" 334 type = "http" 335 protocol = "https" 336 port = "lb" 337 path = "/_healthz" 338 interval = "5s" 339 timeout = "2s" 340 method = "POST" 341 } 342 343 check { 344 name = "Postgres Check" 345 type = "script" 346 command = "/usr/local/bin/pg-tools" 347 args = ["verify", "database" "prod", "up"] 348 interval = "5s" 349 timeout = "2s" 350 } 351 } 352 ``` 353 354 ### Using Driver Address Mode 355 356 The [Docker](/docs/drivers/docker.html#network_mode) and 357 [rkt](/docs/drivers/rkt.html#net) drivers support the `driver` setting for the 358 `address_mode` parameter in both `service` and `check` stanzas. The driver 359 address mode allows advertising and health checking the IP and port assigned to 360 a task by the driver. This way if you're using a network plugin like Weave with 361 Docker, you can advertise the Weave address in Consul instead of the host's 362 address. 363 364 For example if you were running the example Redis job in an environment with 365 Weave but Consul was running on the host you could use the following 366 configuration: 367 368 ```hcl 369 job "example" { 370 datacenters = ["dc1"] 371 group "cache" { 372 373 task "redis" { 374 driver = "docker" 375 376 config { 377 image = "redis:3.2" 378 network_mode = "weave" 379 port_map { 380 db = 6379 381 } 382 } 383 384 resources { 385 cpu = 500 # 500 MHz 386 memory = 256 # 256MB 387 network { 388 mbits = 10 389 port "db" {} 390 } 391 } 392 393 service { 394 name = "weave-redis" 395 port = "db" 396 check { 397 name = "host-redis-check" 398 type = "tcp" 399 interval = "10s" 400 timeout = "2s" 401 } 402 } 403 } 404 } 405 } 406 ``` 407 408 No explicit `address_mode` required! 409 410 Services default to the `auto` address mode. When a Docker network mode other 411 than "host" or "bridge" is used, services will automatically advertise the 412 driver's address (in this case Weave's). The service will advertise the 413 container's port: 6379. 414 415 However since Consul is often run on the host without access to the Weave 416 network, `check` stanzas default to `host` address mode. The TCP check will run 417 against the host's IP and the dynamic host port assigned by Nomad. 418 419 Note that the `check` still inherits the `service` stanza's `db` port label, 420 but each will resolve the port label according to their address mode. 421 422 If Consul has access to the Weave network the job could be configured like 423 this: 424 425 ```hcl 426 job "example" { 427 datacenters = ["dc1"] 428 group "cache" { 429 430 task "redis" { 431 driver = "docker" 432 433 config { 434 image = "redis:3.2" 435 network_mode = "weave" 436 # No port map required! 437 } 438 439 resources { 440 cpu = 500 # 500 MHz 441 memory = 256 # 256MB 442 network { 443 mbits = 10 444 } 445 } 446 447 service { 448 name = "weave-redis" 449 port = 6379 450 address_mode = "driver" 451 check { 452 name = "host-redis-check" 453 type = "tcp" 454 interval = "10s" 455 timeout = "2s" 456 port = 6379 457 458 address_mode = "driver" 459 } 460 } 461 } 462 } 463 } 464 ``` 465 466 In this case Nomad doesn't need to assign Redis any host ports. The `service` 467 and `check` stanzas can both specify the port number to advertise and check 468 directly since Nomad isn't managing any port assignments. 469 470 ### IPv6 Docker containers 471 472 The [Docker](/docs/drivers/docker.html#advertise_ipv6_address) driver supports the 473 `advertise_ipv6_address` parameter in it's configuration. 474 475 Services will automatically advertise the IPv6 address when `advertise_ipv6_address` 476 is used. 477 478 Unlike services, checks do not have an `auto` address mode as there's no way 479 for Nomad to know which is the best address to use for checks. Consul needs 480 access to the address for any HTTP or TCP checks. 481 482 So you have to set `address_mode` parameter in the `check` stanza to `driver`. 483 484 For example using `auto` address mode: 485 486 ```hcl 487 job "example" { 488 datacenters = ["dc1"] 489 group "cache" { 490 491 task "redis" { 492 driver = "docker" 493 494 config { 495 image = "redis:3.2" 496 advertise_ipv6_address = true 497 port_map { 498 db = 6379 499 } 500 } 501 502 resources { 503 cpu = 500 # 500 MHz 504 memory = 256 # 256MB 505 network { 506 mbits = 10 507 port "db" {} 508 } 509 } 510 511 service { 512 name = "ipv6-redis" 513 port = db 514 check { 515 name = "ipv6-redis-check" 516 type = "tcp" 517 interval = "10s" 518 timeout = "2s" 519 port = db 520 address_mode = "driver" 521 } 522 } 523 } 524 } 525 } 526 ``` 527 528 Or using `address_mode=driver` for `service` and `check` with numeric ports: 529 530 ```hcl 531 job "example" { 532 datacenters = ["dc1"] 533 group "cache" { 534 535 task "redis" { 536 driver = "docker" 537 538 config { 539 image = "redis:3.2" 540 advertise_ipv6_address = true 541 # No port map required! 542 } 543 544 resources { 545 cpu = 500 # 500 MHz 546 memory = 256 # 256MB 547 network { 548 mbits = 10 549 } 550 } 551 552 service { 553 name = "ipv6-redis" 554 port = 6379 555 address_mode = "driver" 556 check { 557 name = "ipv6-redis-check" 558 type = "tcp" 559 interval = "10s" 560 timeout = "2s" 561 port = 6379 562 address_mode = "driver" 563 } 564 } 565 } 566 } 567 } 568 ``` 569 570 The `service` and `check` stanzas can both specify the port number to 571 advertise and check directly since Nomad isn't managing any port assignments. 572 573 574 - - - 575 576 <sup><small>1</small></sup><small> Script checks are not supported for the 577 [qemu driver][qemu] since the Nomad client does not have access to the file 578 system of a task for that driver.</small> 579 580 [check_restart_stanza]: /docs/job-specification/check_restart.html "check_restart stanza" 581 [service-discovery]: /docs/service-discovery/index.html "Nomad Service Discovery" 582 [interpolation]: /docs/runtime/interpolation.html "Nomad Runtime Interpolation" 583 [network]: /docs/job-specification/network.html "Nomad network Job Specification" 584 [qemu]: /docs/drivers/qemu.html "Nomad qemu Driver" 585 [restart_stanza]: /docs/job-specification/restart.html "restart stanza"