github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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 of this 78 service. If not supplied, this will default to the name of the job, group, and 79 task concatenated together with a dash, like `"docs-example-server"`. Each 80 service must have a unique name within the cluster. Names must adhere to 81 [RFC-1123 §2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited 82 to alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64 83 characters in length. 84 85 In addition to the standard [Nomad interpolation][interpolation], the 86 following keys are also available: 87 88 - `${JOB}` - the name of the job 89 - `${GROUP}` - the name of the group 90 - `${TASK}` - the name of the task 91 - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}` 92 93 Validation of the name occurs in two parts. When the job is registered, an initial validation pass checks that 94 the service name adheres to RFC-1123 §2.1 and the length limit, excluding any variables requiring interpolation. 95 Once the client receives the service and all interpretable values are available, the service name will be 96 interpolated and revalidated. This can cause certain service names to pass validation at submit time but fail 97 at runtime. 98 99 - `port` `(string: <optional>)` - Specifies the label of the port on which this 100 service is running. Note this is the _label_ of the port and not the port 101 number unless `address_mode = driver`. The port label must match one defined 102 in the [`network`][network] stanza unless you're also using 103 `address_mode="driver"`. Numeric ports may be used when in driver addressing 104 mode. 105 106 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 107 this service. If this is not supplied, no tags will be assigned to the service 108 when it is registered. 109 110 - `address_mode` `(string: "auto")` - Specifies what address (host or 111 driver-specific) this service should advertise. This setting is supported in 112 Docker since Nomad 0.6 and rkt since Nomad 0.7. See [below for 113 examples.](#using-driver-address-mode) Valid options are: 114 115 - `auto` - Allows the driver to determine whether the host or driver address 116 should be used. Defaults to `host` and only implemented by Docker. If you 117 use a Docker network plugin such as weave, Docker will automatically use 118 its address. 119 120 - `driver` - Use the IP specified by the driver, and the port specified in a 121 port map. A numeric port may be specified since port maps aren't required 122 by all network plugins. Useful for advertising SDN and overlay network 123 addresses. Task will fail if driver network cannot be determined. Only 124 implemented for Docker and rkt. 125 126 - `host` - Use the host IP and port. 127 128 ### `check` Parameters 129 130 Note that health checks run inside the task. If your task is a Docker container, 131 the script will run inside the Docker container. If your task is running in a 132 chroot, it will run in the chroot. Please keep this in mind when authoring check 133 scripts. 134 135 - `address_mode` `(string: "host")` - Same as `address_mode` on `service`. 136 Unlike services, checks do not have an `auto` address mode as there's no way 137 for Nomad to know which is the best address to use for checks. Consul needs 138 access to the address for any HTTP or TCP checks. Added in Nomad 0.7.1. See 139 [below for details.](#using-driver-address-mode) Unlike `port`, this setting 140 is *not* inherited from the `service`. 141 142 - `args` `(array<string>: [])` - Specifies additional arguments to the 143 `command`. This only applies to script-based health checks. 144 145 - `check_restart` - See [`check_restart` stanza][check_restart_stanza]. 146 147 - `command` `(string: <varies>)` - Specifies the command to run for performing 148 the health check. The script must exit: 0 for passing, 1 for warning, or any 149 other value for a failing health check. This is required for script-based 150 health checks. 151 152 ~> **Caveat:** The command must be the path to the command on disk, and no 153 shell exists by default. That means operators like `||` or `&&` are not 154 available. Additionally, all arguments must be supplied via the `args` 155 parameter. To achieve the behavior of shell operators, specify the command 156 as a shell, like `/bin/bash` and then use `args` to run the check. 157 158 - `initial_status` `(string: <enum>)` - Specifies the originating status of the 159 service. Valid options are the empty string, `passing`, `warning`, and 160 `critical`. 161 162 - `interval` `(string: <required>)` - Specifies the frequency of the health checks 163 that Consul will perform. This is specified using a label suffix like "30s" 164 or "1h". This must be greater than or equal to "1s" 165 166 - `method` `(string: "GET")` - Specifies the HTTP method to use for HTTP 167 checks. 168 169 - `name` `(string: "service: <name> check")` - Specifies the name of the health 170 check. 171 172 - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which 173 Consul will query to query the health of a service. Nomad will automatically 174 add the IP of the service and the port, so this is just the relative URL to 175 the health check endpoint. This is required for http-based health checks. 176 177 - `port` `(string: <varies>)` - Specifies the label of the port on which the 178 check will be performed. Note this is the _label_ of the port and not the port 179 number unless `address_mode = driver`. The port label must match one defined 180 in the [`network`][network] stanza. If a port value was declared on the 181 `service`, this will inherit from that value if not supplied. If supplied, 182 this value takes precedence over the `service.port` value. This is useful for 183 services which operate on multiple ports. `http` and `tcp` checks require a 184 port while `script` checks do not. Checks will use the host IP and ports by 185 default. In Nomad 0.7.1 or later numeric ports may be used if 186 `address_mode="driver"` is set on the check. 187 188 - `protocol` `(string: "http")` - Specifies the protocol for the http-based 189 health checks. Valid options are `http` and `https`. 190 191 - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a 192 health check query to succeed. This is specified using a label suffix like 193 "30s" or "1h". This must be greater than or equal to "1s" 194 195 - `type` `(string: <required>)` - This indicates the check types supported by 196 Nomad. Valid options are `script`, `http`, and `tcp`. 197 198 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 199 checks. Requires Consul >= 0.7.2. 200 201 #### `header` Stanza 202 203 HTTP checks may include a `header` stanza to set HTTP headers. The `header` 204 stanza parameters have lists of strings as values. Multiple values will cause 205 the header to be set multiple times, once for each value. 206 207 ```hcl 208 service { 209 # ... 210 check { 211 type = "http" 212 port = "lb" 213 path = "/_healthz" 214 interval = "5s" 215 timeout = "2s" 216 header { 217 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 218 } 219 } 220 } 221 ``` 222 223 224 ## `service` Examples 225 226 The following examples only show the `service` stanzas. Remember that the 227 `service` stanza is only valid in the placements listed above. 228 229 ### Basic Service 230 231 This example registers a service named "load-balancer" with no health checks. 232 233 ```hcl 234 service { 235 name = "load-balancer" 236 port = "lb" 237 } 238 ``` 239 240 This example must be accompanied by a [`network`][network] stanza which defines 241 a static or dynamic port labeled "lb". For example: 242 243 ```hcl 244 resources { 245 network { 246 mbits = 10 247 port "lb" {} 248 } 249 } 250 ``` 251 252 ### Check with Bash-isms 253 254 This example shows a common mistake and correct behavior for custom checks. 255 Suppose a health check like this: 256 257 ```shell 258 $ test -f /tmp/file.txt 259 ``` 260 261 In this example `test` is not actually a command (binary) on the system; it is a 262 built-in shell function to bash. Thus, the following **would not work**: 263 264 ```hcl 265 service { 266 check { 267 type = "script" 268 command = "test -f /tmp/file.txt" # THIS IS NOT CORRECT 269 } 270 } 271 ``` 272 273 Nomad will attempt to find an executable named `test` on your system, but it 274 does not exist. It is actually just a function of bash. Additionally, it is not 275 possible to specify the arguments in a single string. Here is the correct 276 solution: 277 278 ```hcl 279 service { 280 check { 281 type = "script" 282 command = "/bin/bash" 283 args = ["-c", "test -f /tmp/file.txt"] 284 } 285 } 286 ``` 287 288 The `command` is actually `/bin/bash`, since that is the actual process we are 289 running. The arguments to that command are the script itself, which each 290 argument provided as a value to the `args` array. 291 292 ### HTTP Health Check 293 294 This example shows a service with an HTTP health check. This will query the 295 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 296 giving the service a maximum of 2 seconds to return a response, and include an 297 Authorization header. Any non-2xx code is considered a failure. 298 299 ```hcl 300 service { 301 check { 302 type = "http" 303 port = "lb" 304 path = "/_healthz" 305 interval = "5s" 306 timeout = "2s" 307 header { 308 Authorization = ["Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="] 309 } 310 } 311 } 312 ``` 313 314 ### Multiple Health Checks 315 316 This example shows a service with multiple health checks defined. All health 317 checks must be passing in order for the service to register as healthy. 318 319 ```hcl 320 service { 321 check { 322 type = "http" 323 port = "lb" 324 path = "/_healthz" 325 interval = "5s" 326 timeout = "2s" 327 } 328 329 check { 330 type = "http" 331 protocol = "https" 332 port = "lb" 333 path = "/_healthz" 334 interval = "5s" 335 timeout = "2s" 336 method = "POST" 337 } 338 339 check { 340 type = "script" 341 command = "/usr/local/bin/pg-tools" 342 args = ["verify", "database" "prod", "up"] 343 interval = "5s" 344 timeout = "2s" 345 } 346 } 347 ``` 348 349 ### Using Driver Address Mode 350 351 The [Docker](/docs/drivers/docker.html#network_mode) and 352 [rkt](/docs/drivers/rkt.html#net) drivers support the `driver` setting for the 353 `address_mode` parameter in both `service` and `check` stanzas. The driver 354 address mode allows advertising and health checking the IP and port assigned to 355 a task by the driver. This way if you're using a network plugin like Weave with 356 Docker, you can advertise the Weave address in Consul instead of the host's 357 address. 358 359 For example if you were running the example Redis job in an environment with 360 Weave but Consul was running on the host you could use the following 361 configuration: 362 363 ```hcl 364 job "example" { 365 datacenters = ["dc1"] 366 group "cache" { 367 368 task "redis" { 369 driver = "docker" 370 371 config { 372 image = "redis:3.2" 373 network_mode = "weave" 374 port_map { 375 db = 6379 376 } 377 } 378 379 resources { 380 cpu = 500 # 500 MHz 381 memory = 256 # 256MB 382 network { 383 mbits = 10 384 port "db" {} 385 } 386 } 387 388 service { 389 name = "weave-redis" 390 port = "db" 391 check { 392 name = "host-redis-check" 393 type = "tcp" 394 interval = "10s" 395 timeout = "2s" 396 } 397 } 398 } 399 } 400 } 401 ``` 402 403 No explicit `address_mode` required! 404 405 Services default to the `auto` address mode. When a Docker network mode other 406 than "host" or "bridge" is used, services will automatically advertise the 407 driver's address (in this case Weave's). The service will advertise the 408 container's port: 6379. 409 410 However since Consul is often run on the host without access to the Weave 411 network, `check` stanzas default to `host` address mode. The TCP check will run 412 against the host's IP and the dynamic host port assigned by Nomad. 413 414 Note that the `check` still inherits the `service` stanza's `db` port label, 415 but each will resolve the port label according to their address mode. 416 417 If Consul has access to the Weave network the job could be configured like 418 this: 419 420 ```hcl 421 job "example" { 422 datacenters = ["dc1"] 423 group "cache" { 424 425 task "redis" { 426 driver = "docker" 427 428 config { 429 image = "redis:3.2" 430 network_mode = "weave" 431 # No port map required! 432 } 433 434 resources { 435 cpu = 500 # 500 MHz 436 memory = 256 # 256MB 437 network { 438 mbits = 10 439 } 440 } 441 442 service { 443 name = "weave-redis" 444 port = 6379 445 address_mode = "driver" 446 check { 447 name = "host-redis-check" 448 type = "tcp" 449 interval = "10s" 450 timeout = "2s" 451 port = 6379 452 453 address_mode = "driver" 454 } 455 } 456 } 457 } 458 } 459 ``` 460 461 In this case Nomad doesn't need to assign Redis any host ports. The `service` 462 and `check` stanzas can both specify the port number to advertise and check 463 directly since Nomad isn't managing any port assignments. 464 465 466 - - - 467 468 <sup><small>1</small></sup><small> Script checks are not supported for the 469 [qemu driver][qemu] since the Nomad client does not have access to the file 470 system of a task for that driver.</small> 471 472 [check_restart_stanza]: /docs/job-specification/check_restart.html "check_restart stanza" 473 [service-discovery]: /docs/service-discovery/index.html "Nomad Service Discovery" 474 [interpolation]: /docs/runtime/interpolation.html "Nomad Runtime Interpolation" 475 [network]: /docs/job-specification/network.html "Nomad network Job Specification" 476 [qemu]: /docs/drivers/qemu.html "Nomad qemu Driver" 477 [restart_stanza]: /docs/job-specification/restart.html "restart stanza"