github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/docs/job-specification/service.mdx (about) 1 --- 2 layout: docs 3 page_title: service Stanza - Job Specification 4 sidebar_title: 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 <Placement 13 groups={[ 14 ['job', 'group', 'service'], 15 ['job', 'group', 'task', 'service'] 16 ]} 17 /> 18 19 The `service` stanza instructs Nomad to register a service with Consul. This 20 section of the documentation will discuss the configuration, but please also 21 read the [Nomad service discovery documentation][service-discovery] for more 22 detailed information about the integration. 23 24 ```hcl 25 job "docs" { 26 group "example" { 27 task "server" { 28 service { 29 tags = ["leader", "mysql"] 30 31 port = "db" 32 33 meta { 34 meta = "for your service" 35 } 36 37 check { 38 type = "tcp" 39 port = "db" 40 interval = "10s" 41 timeout = "2s" 42 } 43 44 check { 45 type = "script" 46 name = "check_table" 47 command = "/usr/local/bin/check_mysql_table_status" 48 args = ["--verbose"] 49 interval = "60s" 50 timeout = "5s" 51 52 check_restart { 53 limit = 3 54 grace = "90s" 55 ignore_warnings = false 56 } 57 } 58 } 59 } 60 } 61 } 62 ``` 63 64 This section of the documentation only cover the job file fields and stanzas 65 for service discovery. For more details on using Nomad with Consul please see 66 the [Consul integration documentation][service-discovery]. 67 68 Nomad 0.10 also allows specifying the `service` stanza at the task group level. 69 This enables services in the same task group to opt into [Consul 70 Connect][connect] integration. 71 72 ## `service` Parameters 73 74 - `check` <code>([Check](#check-parameters): nil)</code> - Specifies a health 75 check associated with the service. This can be specified multiple times to 76 define multiple checks for the service. At this time, Nomad supports the 77 `grpc`, `http`, `script`<sup><small>1</small></sup>, and `tcp` checks. 78 79 - `connect` - Configures the [Consul Connect][connect] integration. Only 80 available on group services. 81 82 - `name` `(string: "<job>-<group>-<task>")` - Specifies the name this service 83 will be advertised as in Consul. If not supplied, this will default to the 84 name of the job, group, and task concatenated together with a dash, like 85 `"docs-example-server"`. Each service must have a unique name within the 86 cluster. Names must adhere to [RFC-1123 87 §2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited to 88 alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64 89 characters in length. 90 91 In addition to the standard [Nomad interpolation][interpolation], the 92 following keys are also available: 93 94 - `${JOB}` - the name of the job 95 - `${GROUP}` - the name of the group 96 - `${TASK}` - the name of the task 97 - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}` 98 99 Validation of the name occurs in two parts. When the job is registered, an initial validation pass checks that 100 the service name adheres to RFC-1123 §2.1 and the length limit, excluding any variables requiring interpolation. 101 Once the client receives the service and all interpretable values are available, the service name will be 102 interpolated and revalidated. This can cause certain service names to pass validation at submit time but fail 103 at runtime. 104 105 - `port` `(string: <optional>)` - Specifies the port to advertise for this 106 service. The value of `port` depends on which [`address_mode`](#address_mode) 107 is being used: 108 109 - `driver` - Advertise the port determined by the driver (eg Docker or rkt). 110 The `port` may be a numeric port or a port label specified in the driver's 111 `port_map`. 112 113 - `host` - Advertise the host port for this service. `port` must match a port 114 _label_ specified in the [`network`][network] stanza. 115 116 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 117 this service. If this is not supplied, no tags will be assigned to the service 118 when it is registered. 119 120 - `canary_tags` `(array<string>: [])` - Specifies the list of tags to associate with 121 this service when the service is part of an allocation that is currently a 122 canary. Once the canary is promoted, the registered tags will be updated to 123 those specified in the `tags` parameter. If this is not supplied, the 124 registered tags will be equal to that of the `tags` parameter. 125 126 - `enable_tag_override` `(bool: false)` - Enables users of Consul's Catalog API 127 to make changes to the tags of a service without having those changes be 128 overwritten by Consul's anti-entropy mechanism. See Consul 129 [documentation](https://www.consul.io/docs/internals/anti-entropy.html#enable-tag-override) 130 for more information. 131 132 - `address_mode` `(string: "auto")` - Specifies what address (host or 133 driver-specific) this service should advertise. This setting is supported in 134 Docker since Nomad 0.6 and rkt since Nomad 0.7. See [below for 135 examples.](#using-driver-address-mode) Valid options are: 136 137 - `auto` - Allows the driver to determine whether the host or driver address 138 should be used. Defaults to `host` and only implemented by Docker. If you 139 use a Docker network plugin such as weave, Docker will automatically use 140 its address. 141 142 - `driver` - Use the IP specified by the driver, and the port specified in a 143 port map. A numeric port may be specified since port maps aren't required 144 by all network plugins. Useful for advertising SDN and overlay network 145 addresses. Task will fail if driver network cannot be determined. Only 146 implemented for Docker and rkt. 147 148 - `host` - Use the host IP and port. 149 150 - `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates 151 the Consul service with user-defined metadata. 152 153 - `canary_meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that 154 annotates the Consul service with user-defined metadata when the service is 155 part of an allocation that is currently a canary. Once the canary is 156 promoted, the registered meta will be updated to those specified in the 157 `meta` parameter. If this is not supplied, the registered meta will be set to 158 that of the `meta` parameter. 159 160 ### `check` Parameters 161 162 Note that health checks run inside the task. If your task is a Docker container, 163 the script will run inside the Docker container. If your task is running in a 164 chroot, it will run in the chroot. Please keep this in mind when authoring check 165 scripts. 166 167 - `address_mode` `(string: "host")` - Same as `address_mode` on `service`. 168 Unlike services, checks do not have an `auto` address mode as there's no way 169 for Nomad to know which is the best address to use for checks. Consul needs 170 access to the address for any HTTP or TCP checks. Added in Nomad 0.7.1. See 171 [below for details.](#using-driver-address-mode) Unlike `port`, this setting 172 is _not_ inherited from the `service`. 173 174 - `args` `(array<string>: [])` - Specifies additional arguments to the 175 `command`. This only applies to script-based health checks. 176 177 - `check_restart` - See [`check_restart` stanza][check_restart_stanza]. 178 179 - `command` `(string: <varies>)` - Specifies the command to run for performing 180 the health check. The script must exit: 0 for passing, 1 for warning, or any 181 other value for a failing health check. This is required for script-based 182 health checks. 183 184 ~> **Caveat:** The command must be the path to the command on disk, and no 185 shell exists by default. That means operators like `||` or `&&` are not 186 available. Additionally, all arguments must be supplied via the `args` 187 parameter. To achieve the behavior of shell operators, specify the command 188 as a shell, like `/bin/bash` and then use `args` to run the check. 189 190 - `grpc_service` `(string: <optional>)` - What service, if any, to specify in 191 the gRPC health check. gRPC health checks require Consul 1.0.5 or later. 192 193 - `grpc_use_tls` `(bool: false)` - Use TLS to perform a gRPC health check. May 194 be used with `tls_skip_verify` to use TLS but skip certificate verification. 195 196 - `initial_status` `(string: <enum>)` - Specifies the originating status of the 197 service. Valid options are the empty string, `passing`, `warning`, and 198 `critical`. 199 200 - `interval` `(string: <required>)` - Specifies the frequency of the health checks 201 that Consul will perform. This is specified using a label suffix like "30s" 202 or "1h". This must be greater than or equal to "1s" 203 204 - `method` `(string: "GET")` - Specifies the HTTP method to use for HTTP 205 checks. 206 207 - `name` `(string: "service: <name> check")` - Specifies the name of the health 208 check. If the name is not specified Nomad generates one based on the service name. 209 If you have more than one check you must specify the name. 210 211 - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which 212 Consul will query to query the health of a service. Nomad will automatically 213 add the IP of the service and the port, so this is just the relative URL to 214 the health check endpoint. This is required for http-based health checks. 215 216 - `expose` `(bool: false)` - Specifies whether an [Expose Path](/docs/job-specification/expose#path-parameters) 217 should be automatically generated for this check. Only compatible with 218 Connect-enabled task-group services using the default Connect proxy. Check 219 must be of [`type`][type] `http` or `grpc`. 220 221 - `port` `(string: <varies>)` - Specifies the label of the port on which the 222 check will be performed. Note this is the _label_ of the port and not the port 223 number unless `address_mode = driver`. The port label must match one defined 224 in the [`network`][network] stanza. If a port value was declared on the 225 `service`, this will inherit from that value if not supplied. If supplied, 226 this value takes precedence over the `service.port` value. This is useful for 227 services which operate on multiple ports. `grpc`, `http`, and `tcp` checks 228 require a port while `script` checks do not. Checks will use the host IP and 229 ports by default. In Nomad 0.7.1 or later numeric ports may be used if 230 `address_mode="driver"` is set on the check. 231 232 - `protocol` `(string: "http")` - Specifies the protocol for the http-based 233 health checks. Valid options are `http` and `https`. 234 235 - `task` `(string: <required>)` - Specifies the task associated with this 236 check. Scripts are executed within the task's environment, and 237 `check_restart` stanzas will apply to the specified task. For `checks` on group 238 level `services` only. 239 240 - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a 241 health check query to succeed. This is specified using a label suffix like 242 "30s" or "1h". This must be greater than or equal to "1s" 243 244 - `type` `(string: <required>)` - This indicates the check types supported by 245 Nomad. Valid options are `grpc`, `http`, `script`, and `tcp`. gRPC health 246 checks require Consul 1.0.5 or later. 247 248 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 249 checks. Requires Consul >= 0.7.2. 250 251 #### `header` Stanza 252 253 HTTP checks may include a `header` stanza to set HTTP headers. The `header` 254 stanza parameters have lists of strings as values. Multiple values will cause 255 the header to be set multiple times, once for each value. 256 257 ```hcl 258 service { 259 # ... 260 check { 261 type = "http" 262 port = "lb" 263 path = "/_healthz" 264 interval = "5s" 265 timeout = "2s" 266 header { 267 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 268 } 269 } 270 } 271 ``` 272 273 ## `service` Lifecycle 274 275 Nomad manages registering, updating, and deregistering services with Consul. It 276 is important to understand when each of these steps happens and how they can be 277 customized. 278 279 **Registration**: Nomad will register `group` services and checks *before* 280 starting any tasks. Services and checks for a specific `task` are registered 281 *after* the task has started. 282 283 **Updating**: If a service or check definition is updated, Nomad will update 284 the service in Consul as well. Consul is updated without restarting a task. 285 286 **Deregistering**: If a running task with a service stanza exits, the services 287 and checks are immediately deregistered from Consul without delay. If however 288 Nomad needs to kill a running task, the task is killed in the following order: 289 290 1. Immediately remove the services and checks from Consul. This stops new 291 traffic from being routed to the task that is being killed. 292 2. If [`shutdown_delay`][shutdowndelay] is set, wait the configured duration 293 before proceeding to step 3. Setting a [`shutdown_delay`][shutdowndelay] can 294 be useful if the application itself doesn't handle graceful shutdowns based 295 on the [`kill_signal`][killsignal]. The configured delay will provide a 296 period of time in which the service is no longer registered in Consul, and 297 thus is not receiving additional requests, but hasn't been signalled to 298 shutdown. This allows the application time to complete the requests and 299 become idle. 300 3. Send the [`kill_signal`][killsignal] to the task and wait for the task to 301 exit. The task should use this time to gracefully drain and finish any 302 existing requests. 303 4. If the task has not exited after the [`kill_timeout`][killtimeout], Nomad 304 will force kill the application. 305 306 ## `service` Examples 307 308 The following examples only show the `service` stanzas. Remember that the 309 `service` stanza is only valid in the placements listed above. 310 311 ### Basic Service 312 313 This example registers a service named "load-balancer" with no health checks. 314 315 ```hcl 316 service { 317 name = "load-balancer" 318 port = "lb" 319 } 320 ``` 321 322 This example must be accompanied by a [`network`][network] stanza which defines 323 a static or dynamic port labeled "lb". For example: 324 325 ```hcl 326 resources { 327 network { 328 mbits = 10 329 port "lb" {} 330 } 331 } 332 ``` 333 334 ### Script Checks with Shells 335 336 This example shows a service with a script check that is evaluated and interpolated in a shell; it 337 tests whether a file is present at `${HEALTH_CHECK_FILE}` environment variable: 338 339 ```hcl 340 service { 341 check { 342 type = "script" 343 command = "/bin/bash" 344 args = ["-c", "test -f ${HEALTH_CHECK_FILE}"] 345 } 346 } 347 ``` 348 349 Using `/bin/bash` (or another shell) is required here to interpolate the `${HEALTH_CHECK_FILE}` value. 350 351 The following examples of `command` fields **will not work**: 352 353 ```hcl 354 # invalid because command is not a path 355 check { 356 type = "script" 357 command = "test -f /tmp/file.txt" 358 } 359 360 # invalid because path will not be interpolated 361 check { 362 type = "script" 363 command = "/bin/test" 364 args = ["-f", "${HEALTH_CHECK_FILE}"] 365 } 366 ``` 367 368 ### HTTP Health Check 369 370 This example shows a service with an HTTP health check. This will query the 371 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 372 giving the service a maximum of 2 seconds to return a response, and include an 373 Authorization header. Any non-2xx code is considered a failure. 374 375 ```hcl 376 service { 377 check { 378 type = "http" 379 port = "lb" 380 path = "/_healthz" 381 interval = "5s" 382 timeout = "2s" 383 header { 384 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 385 } 386 } 387 } 388 ``` 389 390 ### Multiple Health Checks 391 392 This example shows a service with multiple health checks defined. All health 393 checks must be passing in order for the service to register as healthy. 394 395 ```hcl 396 service { 397 check { 398 name = "HTTP Check" 399 type = "http" 400 port = "lb" 401 path = "/_healthz" 402 interval = "5s" 403 timeout = "2s" 404 } 405 406 check { 407 name = "HTTPS Check" 408 type = "http" 409 protocol = "https" 410 port = "lb" 411 path = "/_healthz" 412 interval = "5s" 413 timeout = "2s" 414 method = "POST" 415 } 416 417 check { 418 name = "Postgres Check" 419 type = "script" 420 command = "/usr/local/bin/pg-tools" 421 args = ["verify", "database", "prod", "up"] 422 interval = "5s" 423 timeout = "2s" 424 } 425 } 426 ``` 427 428 ### gRPC Health Check 429 430 gRPC health checks use the same host and port behavior as `http` and `tcp` 431 checks, but gRPC checks also have an optional gRPC service to health check. Not 432 all gRPC applications require a service to health check. gRPC health checks 433 require Consul 1.0.5 or later. 434 435 ```hcl 436 service { 437 check { 438 type = "grpc" 439 port = "rpc" 440 interval = "5s" 441 timeout = "2s" 442 grpc_service = "example.Service" 443 grpc_use_tls = true 444 tls_skip_verify = true 445 } 446 } 447 ``` 448 449 In this example Consul would health check the `example.Service` service on the 450 `rpc` port defined in the task's [network resources][network] stanza. See 451 [Using Driver Address Mode](#using-driver-address-mode) for details on address 452 selection. 453 454 ### Using Driver Address Mode 455 456 The [Docker](/docs/drivers/docker#network_mode) and 457 [rkt](/docs/drivers/rkt#net) drivers support the `driver` setting for the 458 `address_mode` parameter in both `service` and `check` stanzas. The driver 459 address mode allows advertising and health checking the IP and port assigned to 460 a task by the driver. This way if you're using a network plugin like Weave with 461 Docker, you can advertise the Weave address in Consul instead of the host's 462 address. 463 464 For example if you were running the example Redis job in an environment with 465 Weave but Consul was running on the host you could use the following 466 configuration: 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 port_map { 480 db = 6379 481 } 482 } 483 484 resources { 485 cpu = 500 # 500 MHz 486 memory = 256 # 256MB 487 network { 488 mbits = 10 489 port "db" {} 490 } 491 } 492 493 service { 494 name = "weave-redis" 495 port = "db" 496 check { 497 name = "host-redis-check" 498 type = "tcp" 499 interval = "10s" 500 timeout = "2s" 501 } 502 } 503 } 504 } 505 } 506 ``` 507 508 No explicit `address_mode` required! 509 510 Services default to the `auto` address mode. When a Docker network mode other 511 than "host" or "bridge" is used, services will automatically advertise the 512 driver's address (in this case Weave's). The service will advertise the 513 container's port: 6379. 514 515 However since Consul is often run on the host without access to the Weave 516 network, `check` stanzas default to `host` address mode. The TCP check will run 517 against the host's IP and the dynamic host port assigned by Nomad. 518 519 Note that the `check` still inherits the `service` stanza's `db` port label, 520 but each will resolve the port label according to their address mode. 521 522 If Consul has access to the Weave network the job could be configured like 523 this: 524 525 ```hcl 526 job "example" { 527 datacenters = ["dc1"] 528 group "cache" { 529 530 task "redis" { 531 driver = "docker" 532 533 config { 534 image = "redis:3.2" 535 network_mode = "weave" 536 # No port map required! 537 } 538 539 resources { 540 cpu = 500 # 500 MHz 541 memory = 256 # 256MB 542 network { 543 mbits = 10 544 } 545 } 546 547 service { 548 name = "weave-redis" 549 port = 6379 550 address_mode = "driver" 551 check { 552 name = "host-redis-check" 553 type = "tcp" 554 interval = "10s" 555 timeout = "2s" 556 port = 6379 557 558 address_mode = "driver" 559 } 560 } 561 } 562 } 563 } 564 ``` 565 566 In this case Nomad doesn't need to assign Redis any host ports. The `service` 567 and `check` stanzas can both specify the port number to advertise and check 568 directly since Nomad isn't managing any port assignments. 569 570 ### IPv6 Docker containers 571 572 The [Docker](/docs/drivers/docker#advertise_ipv6_address) driver supports the 573 `advertise_ipv6_address` parameter in its configuration. 574 575 Services will automatically advertise the IPv6 address when `advertise_ipv6_address` 576 is used. 577 578 Unlike services, checks do not have an `auto` address mode as there's no way 579 for Nomad to know which is the best address to use for checks. Consul needs 580 access to the address for any HTTP or TCP checks. 581 582 So you have to set `address_mode` parameter in the `check` stanza to `driver`. 583 584 For example using `auto` address mode: 585 586 ```hcl 587 job "example" { 588 datacenters = ["dc1"] 589 group "cache" { 590 591 task "redis" { 592 driver = "docker" 593 594 config { 595 image = "redis:3.2" 596 advertise_ipv6_address = true 597 port_map { 598 db = 6379 599 } 600 } 601 602 resources { 603 cpu = 500 # 500 MHz 604 memory = 256 # 256MB 605 network { 606 mbits = 10 607 port "db" {} 608 } 609 } 610 611 service { 612 name = "ipv6-redis" 613 port = "db" 614 check { 615 name = "ipv6-redis-check" 616 type = "tcp" 617 interval = "10s" 618 timeout = "2s" 619 port = "db" 620 address_mode = "driver" 621 } 622 } 623 } 624 } 625 } 626 ``` 627 628 Or using `address_mode=driver` for `service` and `check` with numeric ports: 629 630 ```hcl 631 job "example" { 632 datacenters = ["dc1"] 633 group "cache" { 634 635 task "redis" { 636 driver = "docker" 637 638 config { 639 image = "redis:3.2" 640 advertise_ipv6_address = true 641 # No port map required! 642 } 643 644 resources { 645 cpu = 500 # 500 MHz 646 memory = 256 # 256MB 647 network { 648 mbits = 10 649 } 650 } 651 652 service { 653 name = "ipv6-redis" 654 port = 6379 655 address_mode = "driver" 656 check { 657 name = "ipv6-redis-check" 658 type = "tcp" 659 interval = "10s" 660 timeout = "2s" 661 port = 6379 662 address_mode = "driver" 663 } 664 } 665 } 666 } 667 } 668 ``` 669 670 The `service` and `check` stanzas can both specify the port number to 671 advertise and check directly since Nomad isn't managing any port assignments. 672 673 --- 674 675 <sup> 676 <small>1</small> 677 </sup> 678 <small> 679 {' '} 680 Script checks are not supported for the [qemu driver][qemu] since the Nomad 681 client does not have access to the file system of a task for that driver. 682 </small> 683 684 [check_restart_stanza]: /docs/job-specification/check_restart 'check_restart stanza' 685 [consul_grpc]: https://www.consul.io/api/agent/check.html#grpc 686 [service-discovery]: /docs/integrations/consul-integration#service-discovery 'Nomad Service Discovery' 687 [interpolation]: /docs/runtime/interpolation 'Nomad Runtime Interpolation' 688 [network]: /docs/job-specification/network 'Nomad network Job Specification' 689 [qemu]: /docs/drivers/qemu 'Nomad qemu Driver' 690 [restart_stanza]: /docs/job-specification/restart 'restart stanza' 691 [connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration' 692 [type]: /docs/job-specification/service#type 693 [shutdowndelay]: /docs/job-specification/task#shutdown_delay 694 [killsignal]: /docs/job-specification/task#kill_signal 695 [killtimeout]: /docs/job-specification/task#kill_timeout