github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/service.mdx (about) 1 --- 2 layout: docs 3 page_title: service Stanza - Job Specification 4 description: |- 5 The "service" stanza instructs Nomad to register the task as a service using 6 the Nomad or Consul service discovery integration. 7 --- 8 9 # `service` Stanza 10 11 <Placement 12 groups={[ 13 ['job', 'group', 'service'], 14 ['job', 'group', 'task', 'service'], 15 ]} 16 /> 17 18 The `service` stanza instructs Nomad to register a service with the specified 19 provider; Nomad or Consul. This section of the documentation will discuss the 20 configuration, but please also read the 21 [Nomad service discovery documentation][service-discovery] for more detailed 22 information about the external integrations. 23 24 ```hcl 25 job "docs" { 26 group "example" { 27 task "server" { 28 service { 29 tags = ["leader", "mysql"] 30 31 port = "db" 32 33 provider = "consul" 34 35 meta { 36 meta = "for your service" 37 } 38 39 check { 40 type = "tcp" 41 port = "db" 42 interval = "10s" 43 timeout = "2s" 44 } 45 46 check { 47 type = "http" 48 name = "app_health" 49 path = "/health" 50 interval = "20s" 51 timeout = "5s" 52 53 check_restart { 54 limit = 3 55 grace = "90s" 56 ignore_warnings = false 57 } 58 } 59 } 60 } 61 } 62 } 63 ``` 64 65 This section of the documentation only cover the job file fields and stanzas 66 for service discovery. For more details on using Nomad with Consul please see 67 the [Consul integration documentation][service-discovery]. 68 69 Nomad 0.10 also allows specifying the `service` stanza at the task group level. 70 This enables services in the same task group to opt into [Consul 71 Connect][connect] integration. 72 73 ## `service` Parameters 74 75 - `provider` `(string: "consul")` - Specifies the service registration provider 76 to use for service registrations. Valid options are either `consul` or 77 `nomad`. All services within a single task group must utilise the same 78 provider value. 79 80 - `check` <code>([Check][check]: nil)</code> - Specifies a health 81 check associated with the service. This can be specified multiple times to 82 define multiple checks for the service. Only available where 83 `provider = "consul"`. At this time, the Consul integration supports the `grpc`, 84 `http`, `script`<sup><small>1</small></sup>, and `tcp` checks. 85 86 - `connect` - Configures the [Consul Connect][connect] integration. Only 87 available on group services and where `provider = "consul"`. 88 89 - `name` `(string: "<job>-<group>-<task>")` - Specifies the name this service 90 will be advertised as in Consul. If not supplied, this will default to the 91 name of the job, group, and task concatenated together with a dash, like 92 `"docs-example-server"`. Each service must have a unique name within the 93 cluster. Names must adhere to [RFC-1123 94 §2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited to 95 alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64 96 characters in length. 97 98 In addition to the standard [Nomad interpolation][interpolation], the 99 following keys are also available: 100 101 - `${JOB}` - the name of the job 102 - `${GROUP}` - the name of the group 103 - `${TASK}` - the name of the task 104 - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}` 105 106 Validation of the name occurs in two parts. When the job is registered, an initial validation pass checks that 107 the service name adheres to RFC-1123 §2.1 and the length limit, excluding any variables requiring interpolation. 108 Once the client receives the service and all interpretable values are available, the service name will be 109 interpolated and revalidated. This can cause certain service names to pass validation at submit time but fail 110 at runtime. 111 112 - `port` `(string: <optional>)` - Specifies the port to advertise for this 113 service. The value of `port` depends on which [`address_mode`](#address_mode) 114 is being used: 115 116 - `alloc` - Advertise the mapped `to` value of the labeled port and the allocation address. 117 If a `to` value is not set, the port falls back to using the allocated host port. The `port` 118 field may be a numeric port or a port label specified in the same group's network stanza. 119 120 - `driver` - Advertise the port determined by the driver (e.g. Docker). 121 The `port` may be a numeric port or a port label specified in the driver's 122 `ports` field. 123 124 - `host` - Advertise the host port for this service. `port` must match a port 125 _label_ specified in the [`network`][network] stanza. 126 127 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 128 this service. If this is not supplied, no tags will be assigned to the service 129 when it is registered. 130 131 - `canary_tags` `(array<string>: [])` - Specifies the list of tags to associate with 132 this service when the service is part of an allocation that is currently a 133 canary. Once the canary is promoted, the registered tags will be updated to 134 those specified in the `tags` parameter. If this is not supplied, the 135 registered tags will be equal to that of the `tags` parameter. 136 137 - `enable_tag_override` `(bool: false)` - Enables users of Consul's Catalog API 138 to make changes to the tags of a service without having those changes be 139 overwritten by Consul's anti-entropy mechanism. See Consul 140 [documentation](https://developer.hashicorp.com/consul/docs/concepts/anti-entropy#enable-tag-override) 141 for more information. Only available where `provider = "consul"`. 142 143 - `address` `(string: <optional>)` - Specifies a custom address to advertise in 144 Consul or Nomad service registration. If set, `address_mode` must be in `auto` 145 mode. Useful with interpolation - for example to advertise the public IP address 146 of an AWS EC2 instance set this to `${attr.unique.platform.aws.public-ipv4}`. 147 148 - `tagged_addresses` `(map<string|string>` - Specifies custom [tagged addresses][tagged_addresses] to 149 advertise in the Consul service registration. Only available where `provider = "consul"`. 150 151 - `address_mode` `(string: "auto")` - Specifies which address (host, alloc or 152 driver-specific) this service should advertise. See [below for 153 examples.](#using-driver-address-mode) Valid options are: 154 155 - `alloc` - For allocations which create a network namespace, this address mode 156 uses the IP address inside the namespace. Can only be used with "bridge" and "cni" 157 [networking modes][network_mode]. A numeric port may be specified for situations 158 where no port mapping is necessary. This mode can only be set for services which 159 are defined in a "group" block. 160 161 - `auto` - Allows the driver to determine whether the host or driver address 162 should be used. Defaults to `host` and only implemented by Docker. If you 163 use a Docker network plugin such as weave, Docker will automatically use 164 its address. 165 166 - `driver` - Use the IP specified by the driver, and the port specified in a 167 port map. A numeric port may be specified since port maps aren't required 168 by all network plugins. Useful for advertising SDN and overlay network 169 addresses. Task will fail if driver network cannot be determined. Only 170 implemented for Docker. This mode can only be set for services 171 which are defined in a "task" block. 172 173 - `host` - Use the host IP and port. 174 175 - `task` `(string: "")` - Specifies the name of the Nomad task associated with 176 this service definition. Only available on group services. Must be set if this 177 service definition represents a Consul Connect-native service and there is more 178 than one task in the task group. 179 180 - `meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that annotates 181 the Consul service with user-defined metadata. Only available where 182 `provider = "consul"`. 183 184 - `canary_meta` <code>([Meta][]: nil)</code> - Specifies a key-value map that 185 annotates the Consul service with user-defined metadata when the service is 186 part of an allocation that is currently a canary. Once the canary is 187 promoted, the registered meta will be updated to those specified in the 188 `meta` parameter. If this is not supplied, the registered meta will be set to 189 that of the `meta` parameter. Only available where `provider = "consul"`. 190 191 - `on_update` `(string: "require_healthy")` - Specifies how checks should be 192 evaluated when determining deployment health (including a job's initial 193 deployment). This allows job submitters to define certain checks as readiness 194 checks, progressing a deployment even if the Service's checks are not yet 195 healthy. Checks inherit the Service's value by default. The check status is 196 not altered in Consul and is only used to determine the check's health during 197 an update. 198 199 - `require_healthy` - In order for Nomad to consider the check healthy during 200 an update it must report as healthy. 201 202 - `ignore_warnings` - If a Service Check reports as warning, Nomad will treat 203 the check as healthy. The Check will still be in a warning state in Consul. 204 205 - `ignore` - Any status will be treated as healthy. 206 207 ~> **Caveat:** `on_update` is only compatible with certain 208 [`check_restart`][check_restart_stanza] configurations. `on_update = "ignore_warnings"` requires that `check_restart.ignore_warnings = true`. 209 `check_restart` can however specify `ignore_warnings = true` with `on_update = "require_healthy"`. If `on_update` is set to `ignore`, `check_restart` must 210 be omitted entirely. 211 212 213 ## `service` Lifecycle 214 215 Nomad manages registering, updating, and deregistering services with the 216 service provider. It is important to understand when each of these steps 217 happens and how they can be customized. 218 219 **Registration**: Nomad will register `group` services and checks _before_ 220 starting any tasks. Services and checks for a specific `task` are registered 221 _after_ the task has started. 222 223 **Updating**: If a service or check definition is updated, Nomad will update 224 the service in the provider as well. This update happens without restarting a 225 task. 226 227 **Deregistering**: If a running task with a service stanza exits, the services 228 and checks are immediately deregistered from the provider without delay. If, 229 however, Nomad needs to kill a running task, the task is killed in the 230 following order: 231 232 1. Immediately remove the services and checks from the provider. This stops new 233 traffic from being routed to the task that is being killed. 234 2. If [`shutdown_delay`][shutdowndelay] is set, wait the configured duration 235 before proceeding to step 3. Setting a [`shutdown_delay`][shutdowndelay] can 236 be useful if the application itself doesn't handle graceful shutdowns based 237 on the [`kill_signal`][killsignal]. The configured delay will provide a 238 period of time in which the service is no longer registered in the provider, 239 and thus is not receiving additional requests, but hasn't been signalled to 240 shutdown. This allows the application time to complete the requests and 241 become idle. 242 3. Send the [`kill_signal`][killsignal] to the task and wait for the task to 243 exit. The task should use this time to gracefully drain and finish any 244 existing requests. 245 4. If the task has not exited after the [`kill_timeout`][killtimeout], Nomad 246 will force kill the application. 247 248 ## `service` Examples 249 250 The following examples only show the `service` stanzas. Remember that the 251 `service` stanza is only valid in the placements listed above. 252 253 ### Basic Service 254 255 This example registers a service named "load-balancer" with no health checks 256 using the Nomad provider: 257 258 ```hcl 259 service { 260 name = "load-balancer" 261 port = "lb" 262 provider = "nomad" 263 } 264 ``` 265 266 This example registers a service named "load-balancer" with no health checks 267 using the Consul provider: 268 269 ```hcl 270 service { 271 name = "load-balancer" 272 port = "lb" 273 } 274 ``` 275 276 These examples must be accompanied by a [`network`][network] stanza which 277 defines a static or dynamic port labeled "lb". For example: 278 279 ```hcl 280 network { 281 port "lb" {} 282 } 283 ``` 284 285 286 ### Using Driver Address Mode 287 288 The [Docker](/docs/drivers/docker#network_mode) driver supports the `driver` 289 setting for the `address_mode` parameter in both `service` and `check` stanzas. 290 The driver address mode allows advertising and health checking the IP and port 291 assigned to a task by the driver. This way, if you're using a network plugin like 292 Weave with Docker, you can advertise the Weave address in Consul instead of the 293 host's address. 294 295 For example if you were running the example Redis job in an environment with 296 Weave but Consul was running on the host you could use the following 297 configuration: 298 299 ```hcl 300 job "example" { 301 datacenters = ["dc1"] 302 303 group "cache" { 304 network { 305 port "db" { 306 to = 6379 307 } 308 } 309 310 task "redis" { 311 driver = "docker" 312 313 config { 314 image = "redis:7" 315 network_mode = "weave" 316 ports = ["db"] 317 } 318 319 resources { 320 cpu = 500 # 500 MHz 321 memory = 256 # 256MB 322 } 323 324 service { 325 name = "weave-redis" 326 port = "db" 327 check { 328 name = "host-redis-check" 329 type = "tcp" 330 interval = "10s" 331 timeout = "2s" 332 } 333 } 334 } 335 } 336 } 337 ``` 338 339 No explicit `address_mode` required. 340 341 Services default to the `auto` address mode. When a Docker network mode other 342 than `"host"` or `"bridge"` is used, services will automatically advertise the 343 driver's address (in this case Weave's). The service will advertise the 344 container's port: 6379. 345 346 However since Consul is often run on the host without access to the Weave 347 network, `check` stanzas default to `host` address mode. The TCP check will run 348 against the host's IP and the dynamic host port assigned by Nomad. 349 350 Note that the `check` still inherits the `service` stanza's `db` port label, 351 but each will resolve the port label according to their address mode. 352 353 If Consul has access to the Weave network the job could be configured like 354 this: 355 356 ```hcl 357 job "example" { 358 datacenters = ["dc1"] 359 group "cache" { 360 361 task "redis" { 362 driver = "docker" 363 364 config { 365 image = "redis:7" 366 network_mode = "weave" 367 # No port map required. 368 } 369 370 resources { 371 cpu = 500 # 500 MHz 372 memory = 256 # 256MB 373 } 374 375 service { 376 name = "weave-redis" 377 port = 6379 378 address_mode = "driver" 379 check { 380 name = "host-redis-check" 381 type = "tcp" 382 interval = "10s" 383 timeout = "2s" 384 port = 6379 385 386 address_mode = "driver" 387 } 388 } 389 } 390 } 391 } 392 ``` 393 394 In this case Nomad doesn't need to assign Redis any host ports. The `service` 395 and `check` stanzas can both specify the port number to advertise and check 396 directly since Nomad isn't managing any port assignments. 397 398 ### IPv6 Docker containers 399 400 The [Docker](/docs/drivers/docker#advertise_ipv6_address) driver supports the 401 `advertise_ipv6_address` parameter in its configuration. 402 403 Services will automatically advertise the IPv6 address when `advertise_ipv6_address` 404 is used. 405 406 Unlike services, checks do not have an `auto` address mode as there's no way 407 for Nomad to know which is the best address to use for checks. Consul needs 408 access to the address for any HTTP or TCP checks. 409 410 So you have to set `address_mode` parameter in the `check` stanza to `driver`. 411 412 For example using `auto` address mode: 413 414 ```hcl 415 job "example" { 416 datacenters = ["dc1"] 417 group "cache" { 418 419 network { 420 port "db" { 421 to = 6379 422 } 423 } 424 425 426 task "redis" { 427 driver = "docker" 428 429 config { 430 image = "redis:7" 431 advertise_ipv6_address = true 432 ports = ["db"] 433 } 434 435 resources { 436 cpu = 500 # 500 MHz 437 memory = 256 # 256MB 438 } 439 440 service { 441 name = "ipv6-redis" 442 port = "db" 443 check { 444 name = "ipv6-redis-check" 445 type = "tcp" 446 interval = "10s" 447 timeout = "2s" 448 port = "db" 449 address_mode = "driver" 450 } 451 } 452 } 453 } 454 } 455 ``` 456 457 Or using `address_mode=driver` for `service` and `check` with numeric ports: 458 459 ```hcl 460 job "example" { 461 datacenters = ["dc1"] 462 463 group "cache" { 464 465 task "redis" { 466 driver = "docker" 467 468 config { 469 image = "redis:7" 470 advertise_ipv6_address = true 471 # No port map required. 472 } 473 474 resources { 475 cpu = 500 # 500 MHz 476 memory = 256 # 256MB 477 } 478 479 service { 480 name = "ipv6-redis" 481 port = 6379 482 address_mode = "driver" 483 check { 484 name = "ipv6-redis-check" 485 type = "tcp" 486 interval = "10s" 487 timeout = "2s" 488 port = 6379 489 address_mode = "driver" 490 } 491 } 492 } 493 } 494 } 495 ``` 496 497 The `service` and `check` stanzas can both specify the port number to 498 advertise and check directly since Nomad isn't managing any port assignments. 499 500 --- 501 502 [check]: /docs/job-specification/check 503 [check_restart_stanza]: /docs/job-specification/check_restart 504 [consul_grpc]: https://developer.hashicorp.com/consul/api-docs/agent/check#grpc 505 [consul_passfail]: https://developer.hashicorp.com/consul/docs/discovery/checks#success-failures-before-passing-critical 506 [service-discovery]: /docs/integrations/consul-integration#service-discovery 'Nomad Service Discovery' 507 [interpolation]: /docs/runtime/interpolation 'Nomad Runtime Interpolation' 508 [network]: /docs/job-specification/network 'Nomad network Job Specification' 509 [qemu]: /docs/drivers/qemu 'Nomad QEMU Driver' 510 [restart_stanza]: /docs/job-specification/restart 'restart stanza' 511 [connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration' 512 [type]: /docs/job-specification/service#type 513 [shutdowndelay]: /docs/job-specification/task#shutdown_delay 514 [killsignal]: /docs/job-specification/task#kill_signal 515 [killtimeout]: /docs/job-specification/task#kill_timeout 516 [service_task]: /docs/job-specification/service#task-1 517 [network_mode]: /docs/job-specification/network#mode 518 [on_update]: /docs/job-specification/service#on_update 519 [tagged_addresses]: https://developer.hashicorp.com/consul/docs/discovery/services#tagged-addresses