github.com/adityamillind98/nomad@v0.11.8/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 ~> **Caveat:** Script checks use the task driver to execute in the task's 245 environment. For task drivers with namespace isolation such as `docker` or 246 `exec`, setting up the context for the script check may take an unexpectedly 247 long amount of time (a full second or two), especially on busy hosts. The 248 timeout configuration must allow for both this setup and the execution of 249 the script. Operators should use long timeouts (5 or more seconds) for script 250 checks, and monitor telemetry for 251 `client.allocrunner.taskrunner.tasklet_timeout`. 252 253 - `type` `(string: <required>)` - This indicates the check types supported by 254 Nomad. Valid options are `grpc`, `http`, `script`, and `tcp`. gRPC health 255 checks require Consul 1.0.5 or later. 256 257 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 258 checks. Requires Consul >= 0.7.2. 259 260 #### `header` Stanza 261 262 HTTP checks may include a `header` stanza to set HTTP headers. The `header` 263 stanza parameters have lists of strings as values. Multiple values will cause 264 the header to be set multiple times, once for each value. 265 266 ```hcl 267 service { 268 # ... 269 check { 270 type = "http" 271 port = "lb" 272 path = "/_healthz" 273 interval = "5s" 274 timeout = "2s" 275 header { 276 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 277 } 278 } 279 } 280 ``` 281 282 ## `service` Lifecycle 283 284 Nomad manages registering, updating, and deregistering services with Consul. It 285 is important to understand when each of these steps happens and how they can be 286 customized. 287 288 **Registration**: Nomad will register `group` services and checks *before* 289 starting any tasks. Services and checks for a specific `task` are registered 290 *after* the task has started. 291 292 **Updating**: If a service or check definition is updated, Nomad will update 293 the service in Consul as well. Consul is updated without restarting a task. 294 295 **Deregistering**: If a running task with a service stanza exits, the services 296 and checks are immediately deregistered from Consul without delay. If however 297 Nomad needs to kill a running task, the task is killed in the following order: 298 299 1. Immediately remove the services and checks from Consul. This stops new 300 traffic from being routed to the task that is being killed. 301 2. If [`shutdown_delay`][shutdowndelay] is set, wait the configured duration 302 before proceeding to step 3. Setting a [`shutdown_delay`][shutdowndelay] can 303 be useful if the application itself doesn't handle graceful shutdowns based 304 on the [`kill_signal`][killsignal]. The configured delay will provide a 305 period of time in which the service is no longer registered in Consul, and 306 thus is not receiving additional requests, but hasn't been signalled to 307 shutdown. This allows the application time to complete the requests and 308 become idle. 309 3. Send the [`kill_signal`][killsignal] to the task and wait for the task to 310 exit. The task should use this time to gracefully drain and finish any 311 existing requests. 312 4. If the task has not exited after the [`kill_timeout`][killtimeout], Nomad 313 will force kill the application. 314 315 ## `service` Examples 316 317 The following examples only show the `service` stanzas. Remember that the 318 `service` stanza is only valid in the placements listed above. 319 320 ### Basic Service 321 322 This example registers a service named "load-balancer" with no health checks. 323 324 ```hcl 325 service { 326 name = "load-balancer" 327 port = "lb" 328 } 329 ``` 330 331 This example must be accompanied by a [`network`][network] stanza which defines 332 a static or dynamic port labeled "lb". For example: 333 334 ```hcl 335 resources { 336 network { 337 mbits = 10 338 port "lb" {} 339 } 340 } 341 ``` 342 343 ### Script Checks with Shells 344 345 This example shows a service with a script check that is evaluated and interpolated in a shell; it 346 tests whether a file is present at `${HEALTH_CHECK_FILE}` environment variable: 347 348 ```hcl 349 service { 350 check { 351 type = "script" 352 command = "/bin/bash" 353 args = ["-c", "test -f ${HEALTH_CHECK_FILE}"] 354 } 355 } 356 ``` 357 358 Using `/bin/bash` (or another shell) is required here to interpolate the `${HEALTH_CHECK_FILE}` value. 359 360 The following examples of `command` fields **will not work**: 361 362 ```hcl 363 # invalid because command is not a path 364 check { 365 type = "script" 366 command = "test -f /tmp/file.txt" 367 } 368 369 # invalid because path will not be interpolated 370 check { 371 type = "script" 372 command = "/bin/test" 373 args = ["-f", "${HEALTH_CHECK_FILE}"] 374 } 375 ``` 376 377 ### HTTP Health Check 378 379 This example shows a service with an HTTP health check. This will query the 380 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 381 giving the service a maximum of 2 seconds to return a response, and include an 382 Authorization header. Any non-2xx code is considered a failure. 383 384 ```hcl 385 service { 386 check { 387 type = "http" 388 port = "lb" 389 path = "/_healthz" 390 interval = "5s" 391 timeout = "2s" 392 header { 393 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 394 } 395 } 396 } 397 ``` 398 399 ### Multiple Health Checks 400 401 This example shows a service with multiple health checks defined. All health 402 checks must be passing in order for the service to register as healthy. 403 404 ```hcl 405 service { 406 check { 407 name = "HTTP Check" 408 type = "http" 409 port = "lb" 410 path = "/_healthz" 411 interval = "5s" 412 timeout = "2s" 413 } 414 415 check { 416 name = "HTTPS Check" 417 type = "http" 418 protocol = "https" 419 port = "lb" 420 path = "/_healthz" 421 interval = "5s" 422 timeout = "2s" 423 method = "POST" 424 } 425 426 check { 427 name = "Postgres Check" 428 type = "script" 429 command = "/usr/local/bin/pg-tools" 430 args = ["verify", "database", "prod", "up"] 431 interval = "5s" 432 timeout = "2s" 433 } 434 } 435 ``` 436 437 ### gRPC Health Check 438 439 gRPC health checks use the same host and port behavior as `http` and `tcp` 440 checks, but gRPC checks also have an optional gRPC service to health check. Not 441 all gRPC applications require a service to health check. gRPC health checks 442 require Consul 1.0.5 or later. 443 444 ```hcl 445 service { 446 check { 447 type = "grpc" 448 port = "rpc" 449 interval = "5s" 450 timeout = "2s" 451 grpc_service = "example.Service" 452 grpc_use_tls = true 453 tls_skip_verify = true 454 } 455 } 456 ``` 457 458 In this example Consul would health check the `example.Service` service on the 459 `rpc` port defined in the task's [network resources][network] stanza. See 460 [Using Driver Address Mode](#using-driver-address-mode) for details on address 461 selection. 462 463 ### Using Driver Address Mode 464 465 The [Docker](/docs/drivers/docker#network_mode) and 466 [rkt](/docs/drivers/rkt#net) drivers support the `driver` setting for the 467 `address_mode` parameter in both `service` and `check` stanzas. The driver 468 address mode allows advertising and health checking the IP and port assigned to 469 a task by the driver. This way if you're using a network plugin like Weave with 470 Docker, you can advertise the Weave address in Consul instead of the host's 471 address. 472 473 For example if you were running the example Redis job in an environment with 474 Weave but Consul was running on the host you could use the following 475 configuration: 476 477 ```hcl 478 job "example" { 479 datacenters = ["dc1"] 480 group "cache" { 481 482 task "redis" { 483 driver = "docker" 484 485 config { 486 image = "redis:3.2" 487 network_mode = "weave" 488 port_map { 489 db = 6379 490 } 491 } 492 493 resources { 494 cpu = 500 # 500 MHz 495 memory = 256 # 256MB 496 network { 497 mbits = 10 498 port "db" {} 499 } 500 } 501 502 service { 503 name = "weave-redis" 504 port = "db" 505 check { 506 name = "host-redis-check" 507 type = "tcp" 508 interval = "10s" 509 timeout = "2s" 510 } 511 } 512 } 513 } 514 } 515 ``` 516 517 No explicit `address_mode` required! 518 519 Services default to the `auto` address mode. When a Docker network mode other 520 than "host" or "bridge" is used, services will automatically advertise the 521 driver's address (in this case Weave's). The service will advertise the 522 container's port: 6379. 523 524 However since Consul is often run on the host without access to the Weave 525 network, `check` stanzas default to `host` address mode. The TCP check will run 526 against the host's IP and the dynamic host port assigned by Nomad. 527 528 Note that the `check` still inherits the `service` stanza's `db` port label, 529 but each will resolve the port label according to their address mode. 530 531 If Consul has access to the Weave network the job could be configured like 532 this: 533 534 ```hcl 535 job "example" { 536 datacenters = ["dc1"] 537 group "cache" { 538 539 task "redis" { 540 driver = "docker" 541 542 config { 543 image = "redis:3.2" 544 network_mode = "weave" 545 # No port map required! 546 } 547 548 resources { 549 cpu = 500 # 500 MHz 550 memory = 256 # 256MB 551 network { 552 mbits = 10 553 } 554 } 555 556 service { 557 name = "weave-redis" 558 port = 6379 559 address_mode = "driver" 560 check { 561 name = "host-redis-check" 562 type = "tcp" 563 interval = "10s" 564 timeout = "2s" 565 port = 6379 566 567 address_mode = "driver" 568 } 569 } 570 } 571 } 572 } 573 ``` 574 575 In this case Nomad doesn't need to assign Redis any host ports. The `service` 576 and `check` stanzas can both specify the port number to advertise and check 577 directly since Nomad isn't managing any port assignments. 578 579 ### IPv6 Docker containers 580 581 The [Docker](/docs/drivers/docker#advertise_ipv6_address) driver supports the 582 `advertise_ipv6_address` parameter in its configuration. 583 584 Services will automatically advertise the IPv6 address when `advertise_ipv6_address` 585 is used. 586 587 Unlike services, checks do not have an `auto` address mode as there's no way 588 for Nomad to know which is the best address to use for checks. Consul needs 589 access to the address for any HTTP or TCP checks. 590 591 So you have to set `address_mode` parameter in the `check` stanza to `driver`. 592 593 For example using `auto` address mode: 594 595 ```hcl 596 job "example" { 597 datacenters = ["dc1"] 598 group "cache" { 599 600 task "redis" { 601 driver = "docker" 602 603 config { 604 image = "redis:3.2" 605 advertise_ipv6_address = true 606 port_map { 607 db = 6379 608 } 609 } 610 611 resources { 612 cpu = 500 # 500 MHz 613 memory = 256 # 256MB 614 network { 615 mbits = 10 616 port "db" {} 617 } 618 } 619 620 service { 621 name = "ipv6-redis" 622 port = "db" 623 check { 624 name = "ipv6-redis-check" 625 type = "tcp" 626 interval = "10s" 627 timeout = "2s" 628 port = "db" 629 address_mode = "driver" 630 } 631 } 632 } 633 } 634 } 635 ``` 636 637 Or using `address_mode=driver` for `service` and `check` with numeric ports: 638 639 ```hcl 640 job "example" { 641 datacenters = ["dc1"] 642 group "cache" { 643 644 task "redis" { 645 driver = "docker" 646 647 config { 648 image = "redis:3.2" 649 advertise_ipv6_address = true 650 # No port map required! 651 } 652 653 resources { 654 cpu = 500 # 500 MHz 655 memory = 256 # 256MB 656 network { 657 mbits = 10 658 } 659 } 660 661 service { 662 name = "ipv6-redis" 663 port = 6379 664 address_mode = "driver" 665 check { 666 name = "ipv6-redis-check" 667 type = "tcp" 668 interval = "10s" 669 timeout = "2s" 670 port = 6379 671 address_mode = "driver" 672 } 673 } 674 } 675 } 676 } 677 ``` 678 679 The `service` and `check` stanzas can both specify the port number to 680 advertise and check directly since Nomad isn't managing any port assignments. 681 682 --- 683 684 <sup> 685 <small>1</small> 686 </sup> 687 <small> 688 {' '} 689 Script checks are not supported for the [qemu driver][qemu] since the Nomad 690 client does not have access to the file system of a task for that driver. 691 </small> 692 693 [check_restart_stanza]: /docs/job-specification/check_restart 'check_restart stanza' 694 [consul_grpc]: https://www.consul.io/api/agent/check.html#grpc 695 [service-discovery]: /docs/integrations/consul-integration#service-discovery 'Nomad Service Discovery' 696 [interpolation]: /docs/runtime/interpolation 'Nomad Runtime Interpolation' 697 [network]: /docs/job-specification/network 'Nomad network Job Specification' 698 [qemu]: /docs/drivers/qemu 'Nomad qemu Driver' 699 [restart_stanza]: /docs/job-specification/restart 'restart stanza' 700 [connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration' 701 [type]: /docs/job-specification/service#type 702 [shutdowndelay]: /docs/job-specification/task#shutdown_delay 703 [killsignal]: /docs/job-specification/task#kill_signal 704 [killtimeout]: /docs/job-specification/task#kill_timeout