github.com/quite/nomad@v0.8.6/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 `grpc`, `http`, `script`<sup><small>1</small></sup>, 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 port to advertise for this 101 service. The value of `port` depends on which [`address_mode`](#address_mode) 102 is being used: 103 104 - `driver` - Advertise the port determined by the driver (eg Docker or rkt). 105 The `port` may be a numeric port or a port label specified in the driver's 106 `port_map`. 107 108 - `host` - Advertise the host port for this service. `port` must match a port 109 _label_ specified in the [`network`][network] stanza. 110 111 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 112 this service. If this is not supplied, no tags will be assigned to the service 113 when it is registered. 114 115 - `canary_tags` `(array<string>: [])` - Specifies the list of tags to associate with 116 this service when the service is part of an allocation that is currently a 117 canary. Once the canary is promoted, the registered tags will be updated to 118 those specified in the `tags` parameter. If this is not supplied, the 119 registered tags will be equal to that of the `tags parameter. 120 121 - `address_mode` `(string: "auto")` - Specifies what address (host or 122 driver-specific) this service should advertise. This setting is supported in 123 Docker since Nomad 0.6 and rkt since Nomad 0.7. See [below for 124 examples.](#using-driver-address-mode) Valid options are: 125 126 - `auto` - Allows the driver to determine whether the host or driver address 127 should be used. Defaults to `host` and only implemented by Docker. If you 128 use a Docker network plugin such as weave, Docker will automatically use 129 its address. 130 131 - `driver` - Use the IP specified by the driver, and the port specified in a 132 port map. A numeric port may be specified since port maps aren't required 133 by all network plugins. Useful for advertising SDN and overlay network 134 addresses. Task will fail if driver network cannot be determined. Only 135 implemented for Docker and rkt. 136 137 - `host` - Use the host IP and port. 138 139 ### `check` Parameters 140 141 Note that health checks run inside the task. If your task is a Docker container, 142 the script will run inside the Docker container. If your task is running in a 143 chroot, it will run in the chroot. Please keep this in mind when authoring check 144 scripts. 145 146 - `address_mode` `(string: "host")` - Same as `address_mode` on `service`. 147 Unlike services, checks do not have an `auto` address mode as there's no way 148 for Nomad to know which is the best address to use for checks. Consul needs 149 access to the address for any HTTP or TCP checks. Added in Nomad 0.7.1. See 150 [below for details.](#using-driver-address-mode) Unlike `port`, this setting 151 is *not* inherited from the `service`. 152 153 - `args` `(array<string>: [])` - Specifies additional arguments to the 154 `command`. This only applies to script-based health checks. 155 156 - `check_restart` - See [`check_restart` stanza][check_restart_stanza]. 157 158 - `command` `(string: <varies>)` - Specifies the command to run for performing 159 the health check. The script must exit: 0 for passing, 1 for warning, or any 160 other value for a failing health check. This is required for script-based 161 health checks. 162 163 ~> **Caveat:** The command must be the path to the command on disk, and no 164 shell exists by default. That means operators like `||` or `&&` are not 165 available. Additionally, all arguments must be supplied via the `args` 166 parameter. To achieve the behavior of shell operators, specify the command 167 as a shell, like `/bin/bash` and then use `args` to run the check. 168 169 - `grpc_service` `(string: <optional>)` - What service, if any, to specify in 170 the gRPC health check. gRPC health checks require Consul 1.0.5 or later. 171 172 - `grpc_use_tls` `(bool: false)` - Use TLS to perform a gRPC health check. May 173 be used with `tls_skip_verify` to use TLS but skip certificate verification. 174 175 - `initial_status` `(string: <enum>)` - Specifies the originating status of the 176 service. Valid options are the empty string, `passing`, `warning`, and 177 `critical`. 178 179 - `interval` `(string: <required>)` - Specifies the frequency of the health checks 180 that Consul will perform. This is specified using a label suffix like "30s" 181 or "1h". This must be greater than or equal to "1s" 182 183 - `method` `(string: "GET")` - Specifies the HTTP method to use for HTTP 184 checks. 185 186 - `name` `(string: "service: <name> check")` - Specifies the name of the health 187 check. If the name is not specified Nomad generates one based on the service name. 188 If you have more than one check you must specify the name. 189 190 - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which 191 Consul will query to query the health of a service. Nomad will automatically 192 add the IP of the service and the port, so this is just the relative URL to 193 the health check endpoint. This is required for http-based health checks. 194 195 - `port` `(string: <varies>)` - Specifies the label of the port on which the 196 check will be performed. Note this is the _label_ of the port and not the port 197 number unless `address_mode = driver`. The port label must match one defined 198 in the [`network`][network] stanza. If a port value was declared on the 199 `service`, this will inherit from that value if not supplied. If supplied, 200 this value takes precedence over the `service.port` value. This is useful for 201 services which operate on multiple ports. `grpc`, `http`, and `tcp` checks 202 require a port while `script` checks do not. Checks will use the host IP and 203 ports by default. In Nomad 0.7.1 or later numeric ports may be used if 204 `address_mode="driver"` is set on the check. 205 206 - `protocol` `(string: "http")` - Specifies the protocol for the http-based 207 health checks. Valid options are `http` and `https`. 208 209 - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a 210 health check query to succeed. This is specified using a label suffix like 211 "30s" or "1h". This must be greater than or equal to "1s" 212 213 - `type` `(string: <required>)` - This indicates the check types supported by 214 Nomad. Valid options are `grpc`, `http`, `script`, and `tcp`. gRPC health 215 checks require Consul 1.0.5 or later. 216 217 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 218 checks. Requires Consul >= 0.7.2. 219 220 #### `header` Stanza 221 222 HTTP checks may include a `header` stanza to set HTTP headers. The `header` 223 stanza parameters have lists of strings as values. Multiple values will cause 224 the header to be set multiple times, once for each value. 225 226 ```hcl 227 service { 228 # ... 229 check { 230 type = "http" 231 port = "lb" 232 path = "/_healthz" 233 interval = "5s" 234 timeout = "2s" 235 header { 236 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 237 } 238 } 239 } 240 ``` 241 242 243 ## `service` Examples 244 245 The following examples only show the `service` stanzas. Remember that the 246 `service` stanza is only valid in the placements listed above. 247 248 ### Basic Service 249 250 This example registers a service named "load-balancer" with no health checks. 251 252 ```hcl 253 service { 254 name = "load-balancer" 255 port = "lb" 256 } 257 ``` 258 259 This example must be accompanied by a [`network`][network] stanza which defines 260 a static or dynamic port labeled "lb". For example: 261 262 ```hcl 263 resources { 264 network { 265 mbits = 10 266 port "lb" {} 267 } 268 } 269 ``` 270 271 ### Check with Bash-isms 272 273 This example shows a common mistake and correct behavior for custom checks. 274 Suppose a health check like this: 275 276 ```shell 277 $ test -f /tmp/file.txt 278 ``` 279 280 In this example `test` is not actually a command (binary) on the system; it is a 281 built-in shell function to bash. Thus, the following **would not work**: 282 283 ```hcl 284 service { 285 check { 286 type = "script" 287 command = "test -f /tmp/file.txt" # THIS IS NOT CORRECT 288 } 289 } 290 ``` 291 292 Nomad will attempt to find an executable named `test` on your system, but it 293 does not exist. It is actually just a function of bash. Additionally, it is not 294 possible to specify the arguments in a single string. Here is the correct 295 solution: 296 297 ```hcl 298 service { 299 check { 300 type = "script" 301 command = "/bin/bash" 302 args = ["-c", "test -f /tmp/file.txt"] 303 } 304 } 305 ``` 306 307 The `command` is actually `/bin/bash`, since that is the actual process we are 308 running. The arguments to that command are the script itself, which each 309 argument provided as a value to the `args` array. 310 311 ### HTTP Health Check 312 313 This example shows a service with an HTTP health check. This will query the 314 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 315 giving the service a maximum of 2 seconds to return a response, and include an 316 Authorization header. Any non-2xx code is considered a failure. 317 318 ```hcl 319 service { 320 check { 321 type = "http" 322 port = "lb" 323 path = "/_healthz" 324 interval = "5s" 325 timeout = "2s" 326 header { 327 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 328 } 329 } 330 } 331 ``` 332 333 ### Multiple Health Checks 334 335 This example shows a service with multiple health checks defined. All health 336 checks must be passing in order for the service to register as healthy. 337 338 ```hcl 339 service { 340 check { 341 name = "HTTP Check" 342 type = "http" 343 port = "lb" 344 path = "/_healthz" 345 interval = "5s" 346 timeout = "2s" 347 } 348 349 check { 350 name = "HTTPS Check" 351 type = "http" 352 protocol = "https" 353 port = "lb" 354 path = "/_healthz" 355 interval = "5s" 356 timeout = "2s" 357 method = "POST" 358 } 359 360 check { 361 name = "Postgres Check" 362 type = "script" 363 command = "/usr/local/bin/pg-tools" 364 args = ["verify", "database" "prod", "up"] 365 interval = "5s" 366 timeout = "2s" 367 } 368 } 369 ``` 370 371 ### gRPC Health Check 372 373 gRPC health checks use the same host and port behavior as `http` and `tcp` 374 checks, but gRPC checks also have an optional gRPC service to health check. Not 375 all gRPC applications require a service to health check. gRPC health checks 376 require Consul 1.0.5 or later. 377 378 ```hcl 379 service { 380 check { 381 type = "grpc" 382 port = "rpc" 383 interval = "5s" 384 timeout = "2s" 385 grpc_service = "example.Service" 386 grpc_use_tls = true 387 tls_skip_verify = true 388 } 389 } 390 ``` 391 392 In this example Consul would health check the `example.Service` service on the 393 `rpc` port defined in the task's [network resources][network] stanza. See 394 [Using Driver Address Mode](#using-driver-address-mode) for details on address 395 selection. 396 397 ### Using Driver Address Mode 398 399 The [Docker](/docs/drivers/docker.html#network_mode) and 400 [rkt](/docs/drivers/rkt.html#net) drivers support the `driver` setting for the 401 `address_mode` parameter in both `service` and `check` stanzas. The driver 402 address mode allows advertising and health checking the IP and port assigned to 403 a task by the driver. This way if you're using a network plugin like Weave with 404 Docker, you can advertise the Weave address in Consul instead of the host's 405 address. 406 407 For example if you were running the example Redis job in an environment with 408 Weave but Consul was running on the host you could use the following 409 configuration: 410 411 ```hcl 412 job "example" { 413 datacenters = ["dc1"] 414 group "cache" { 415 416 task "redis" { 417 driver = "docker" 418 419 config { 420 image = "redis:3.2" 421 network_mode = "weave" 422 port_map { 423 db = 6379 424 } 425 } 426 427 resources { 428 cpu = 500 # 500 MHz 429 memory = 256 # 256MB 430 network { 431 mbits = 10 432 port "db" {} 433 } 434 } 435 436 service { 437 name = "weave-redis" 438 port = "db" 439 check { 440 name = "host-redis-check" 441 type = "tcp" 442 interval = "10s" 443 timeout = "2s" 444 } 445 } 446 } 447 } 448 } 449 ``` 450 451 No explicit `address_mode` required! 452 453 Services default to the `auto` address mode. When a Docker network mode other 454 than "host" or "bridge" is used, services will automatically advertise the 455 driver's address (in this case Weave's). The service will advertise the 456 container's port: 6379. 457 458 However since Consul is often run on the host without access to the Weave 459 network, `check` stanzas default to `host` address mode. The TCP check will run 460 against the host's IP and the dynamic host port assigned by Nomad. 461 462 Note that the `check` still inherits the `service` stanza's `db` port label, 463 but each will resolve the port label according to their address mode. 464 465 If Consul has access to the Weave network the job could be configured like 466 this: 467 468 ```hcl 469 job "example" { 470 datacenters = ["dc1"] 471 group "cache" { 472 473 task "redis" { 474 driver = "docker" 475 476 config { 477 image = "redis:3.2" 478 network_mode = "weave" 479 # No port map required! 480 } 481 482 resources { 483 cpu = 500 # 500 MHz 484 memory = 256 # 256MB 485 network { 486 mbits = 10 487 } 488 } 489 490 service { 491 name = "weave-redis" 492 port = 6379 493 address_mode = "driver" 494 check { 495 name = "host-redis-check" 496 type = "tcp" 497 interval = "10s" 498 timeout = "2s" 499 port = 6379 500 501 address_mode = "driver" 502 } 503 } 504 } 505 } 506 } 507 ``` 508 509 In this case Nomad doesn't need to assign Redis any host ports. The `service` 510 and `check` stanzas can both specify the port number to advertise and check 511 directly since Nomad isn't managing any port assignments. 512 513 ### IPv6 Docker containers 514 515 The [Docker](/docs/drivers/docker.html#advertise_ipv6_address) driver supports the 516 `advertise_ipv6_address` parameter in it's configuration. 517 518 Services will automatically advertise the IPv6 address when `advertise_ipv6_address` 519 is used. 520 521 Unlike services, checks do not have an `auto` address mode as there's no way 522 for Nomad to know which is the best address to use for checks. Consul needs 523 access to the address for any HTTP or TCP checks. 524 525 So you have to set `address_mode` parameter in the `check` stanza to `driver`. 526 527 For example using `auto` address mode: 528 529 ```hcl 530 job "example" { 531 datacenters = ["dc1"] 532 group "cache" { 533 534 task "redis" { 535 driver = "docker" 536 537 config { 538 image = "redis:3.2" 539 advertise_ipv6_address = true 540 port_map { 541 db = 6379 542 } 543 } 544 545 resources { 546 cpu = 500 # 500 MHz 547 memory = 256 # 256MB 548 network { 549 mbits = 10 550 port "db" {} 551 } 552 } 553 554 service { 555 name = "ipv6-redis" 556 port = db 557 check { 558 name = "ipv6-redis-check" 559 type = "tcp" 560 interval = "10s" 561 timeout = "2s" 562 port = db 563 address_mode = "driver" 564 } 565 } 566 } 567 } 568 } 569 ``` 570 571 Or using `address_mode=driver` for `service` and `check` with numeric ports: 572 573 ```hcl 574 job "example" { 575 datacenters = ["dc1"] 576 group "cache" { 577 578 task "redis" { 579 driver = "docker" 580 581 config { 582 image = "redis:3.2" 583 advertise_ipv6_address = true 584 # No port map required! 585 } 586 587 resources { 588 cpu = 500 # 500 MHz 589 memory = 256 # 256MB 590 network { 591 mbits = 10 592 } 593 } 594 595 service { 596 name = "ipv6-redis" 597 port = 6379 598 address_mode = "driver" 599 check { 600 name = "ipv6-redis-check" 601 type = "tcp" 602 interval = "10s" 603 timeout = "2s" 604 port = 6379 605 address_mode = "driver" 606 } 607 } 608 } 609 } 610 } 611 ``` 612 613 The `service` and `check` stanzas can both specify the port number to 614 advertise and check directly since Nomad isn't managing any port assignments. 615 616 617 - - - 618 619 <sup><small>1</small></sup><small> Script checks are not supported for the 620 [qemu driver][qemu] since the Nomad client does not have access to the file 621 system of a task for that driver.</small> 622 623 [check_restart_stanza]: /docs/job-specification/check_restart.html "check_restart stanza" 624 [consul_grpc]: https://www.consul.io/api/agent/check.html#grpc 625 [service-discovery]: /docs/service-discovery/index.html "Nomad Service Discovery" 626 [interpolation]: /docs/runtime/interpolation.html "Nomad Runtime Interpolation" 627 [network]: /docs/job-specification/network.html "Nomad network Job Specification" 628 [qemu]: /docs/drivers/qemu.html "Nomad qemu Driver" 629 [restart_stanza]: /docs/job-specification/restart.html "restart stanza"