github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/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 } 52 } 53 } 54 } 55 ``` 56 57 This section of the documentation only covers the job file options for 58 configuring service discovery. For more information on the setup and 59 configuration to integrate Nomad with service discovery, please see the 60 [Nomad service discovery documentation][service-discovery]. There are steps you 61 must take to configure Nomad. Simply adding this configuration to your job file 62 does not automatically enable service discovery. 63 64 ## `service` Parameters 65 66 - `check` <code>([Check](#check-parameters): nil)</code> - Specifies a health 67 check associated with the service. This can be specified multiple times to 68 define multiple checks for the service. At this time, Nomad supports the 69 `script`<sup><small>1</small></sup>, `http` and `tcp` checks. 70 71 - `name` `(string: "<job>-<group>-<task>")` - Specifies the name of this 72 service. If not supplied, this will default to the name of the job, group, and 73 task concatenated together with a dash, like `"docs-example-server"`. Each 74 service must have a unique name within the cluster. Names must adhere to 75 [RFC-1123 ยง2.1](https://tools.ietf.org/html/rfc1123#section-2) and are limited 76 to alphanumeric and hyphen characters (i.e. `[a-z0-9\-]`), and be less than 64 77 characters in length. 78 79 In addition to the standard [Nomad interpolation][interpolation], the 80 following keys are also available: 81 82 - `${JOB}` - the name of the job 83 - `${GROUP}` - the name of the group 84 - `${TASK}` - the name of the task 85 - `${BASE}` - shorthand for `${JOB}-${GROUP}-${TASK}` 86 87 - `port` `(string: <required>)` - Specifies the label of the port on which this 88 service is running. Note this is the _label_ of the port and not the port 89 number. The port label must match one defined in the [`network`][network] 90 stanza. 91 92 - `tags` `(array<string>: [])` - Specifies the list of tags to associate with 93 this service. If this is not supplied, no tags will be assigned to the service 94 when it is registered. 95 96 - `address_mode` `(string: "auto")` - Specifies what address (host or 97 driver-specific) this service should advertise. `host` indicates the host IP 98 and port. `driver` advertises the IP used in the driver (e.g. Docker's internal 99 IP) and uses the ports specified in the port map. The default is `auto` which 100 behaves the same as `host` unless the driver determines its IP should be used. 101 This setting was added in Nomad 0.6 and only supported by the Docker driver. 102 It will advertise the container IP if a network plugin is used (e.g. weave). 103 104 ### `check` Parameters 105 106 Note that health checks run inside the task. If your task is a Docker container, 107 the script will run inside the Docker container. If your task is running in a 108 chroot, it will run in the chroot. Please keep this in mind when authoring check 109 scripts. 110 111 - `args` `(array<string>: [])` - Specifies additional arguments to the 112 `command`. This only applies to script-based health checks. 113 114 - `command` `(string: <varies>)` - Specifies the command to run for performing 115 the health check. The script must exit: 0 for passing, 1 for warning, or any 116 other value for a failing health check. This is required for script-based 117 health checks. 118 119 ~> **Caveat:** The command must be the path to the command on disk, and no 120 shell exists by default. That means operators like `||` or `&&` are not 121 available. Additionally, all arguments must be supplied via the `args` 122 parameter. To achieve the behavior of shell operators, specify the command 123 as a shell, like `/bin/bash` and then use `args` to run the check. 124 125 - `initial_status` `(string: <enum>)` - Specifies the originating status of the 126 service. Valid options are the empty string, `passing`, `warning`, and 127 `critical`. 128 129 - `interval` `(string: <required>)` - Specifies the frequency of the health checks 130 that Consul will perform. This is specified using a label suffix like "30s" 131 or "1h". This must be greater than or equal to "1s" 132 133 - `name` `(string: "service: <name> check")` - Specifies the name of the health 134 check. 135 136 - `path` `(string: <varies>)` - Specifies the path of the HTTP endpoint which 137 Consul will query to query the health of a service. Nomad will automatically 138 add the IP of the service and the port, so this is just the relative URL to 139 the health check endpoint. This is required for http-based health checks. 140 141 - `port` `(string: <required>)` - Specifies the label of the port on which the 142 check will be performed. Note this is the _label_ of the port and not the port 143 number. The port label must match one defined in the [`network`][network] 144 stanza. If a port value was declared on the `service`, this will inherit from 145 that value if not supplied. If supplied, this value takes precedence over the 146 `service.port` value. This is useful for services which operate on multiple 147 ports. Checks will *always use the host IP and ports*. 148 149 - `protocol` `(string: "http")` - Specifies the protocol for the http-based 150 health checks. Valid options are `http` and `https`. 151 152 - `timeout` `(string: <required>)` - Specifies how long Consul will wait for a 153 health check query to succeed. This is specified using a label suffix like 154 "30s" or "1h". This must be greater than or equal to "1s" 155 156 - `type` `(string: <required>)` - This indicates the check types supported by 157 Nomad. Valid options are `script`, `http`, and `tcp`. 158 159 - `tls_skip_verify` `(bool: false)` - Skip verifying TLS certificates for HTTPS 160 checks. Requires Consul >= 0.7.2. 161 162 163 ## `service` Examples 164 165 The following examples only show the `service` stanzas. Remember that the 166 `service` stanza is only valid in the placements listed above. 167 168 ### Basic Service 169 170 This example registers a service named "load-balancer" with no health checks. 171 172 ```hcl 173 service { 174 name = "load-balancer" 175 port = "lb" 176 } 177 ``` 178 179 This example must be accompanied by a [`network`][network] stanza which defines 180 a static or dynamic port labeled "lb". For example: 181 182 ```hcl 183 resources { 184 network { 185 mbits = 10 186 port "lb" {} 187 } 188 } 189 ``` 190 191 ### Check with Bash-isms 192 193 This example shows a common mistake and correct behavior for custom checks. 194 Suppose a health check like this: 195 196 ```shell 197 $ test -f /tmp/file.txt 198 ``` 199 200 In this example `test` is not actually a command (binary) on the system; it is a 201 built-in shell function to bash. Thus, the following **would not work**: 202 203 ```hcl 204 service { 205 check { 206 type = "script" 207 command = "test -f /tmp/file.txt" # THIS IS NOT CORRECT 208 } 209 } 210 ``` 211 212 Nomad will attempt to find an executable named `test` on your system, but it 213 does not exist. It is actually just a function of bash. Additionally, it is not 214 possible to specify the arguments in a single string. Here is the correct 215 solution: 216 217 ```hcl 218 service { 219 check { 220 type = "script" 221 command = "/bin/bash" 222 args = ["-c", "test -f /tmp/file.txt"] 223 } 224 } 225 ``` 226 227 The `command` is actually `/bin/bash`, since that is the actual process we are 228 running. The arguments to that command are the script itself, which each 229 argument provided as a value to the `args` array. 230 231 ### HTTP Health Check 232 233 This example shows a service with an HTTP health check. This will query the 234 service on the IP and port registered with Nomad at `/_healthz` every 5 seconds, 235 giving the service a maximum of 2 seconds to return a response. Any non-2xx code 236 is considered a failure. 237 238 ```hcl 239 service { 240 check { 241 type = "http" 242 port = "lb" 243 path = "/_healthz" 244 interval = "5s" 245 timeout = "2s" 246 } 247 } 248 ``` 249 250 ### Multiple Health Checks 251 252 This example shows a service with multiple health checks defined. All health 253 checks must be passing in order for the service to register as healthy. 254 255 ```hcl 256 service { 257 check { 258 type = "http" 259 port = "lb" 260 path = "/_healthz" 261 interval = "5s" 262 timeout = "2s" 263 } 264 265 check { 266 type = "http" 267 protocol = "https" 268 port = "lb" 269 path = "/_healthz" 270 interval = "5s" 271 timeout = "2s" 272 } 273 274 check { 275 type = "script" 276 command = "/usr/local/bin/pg-tools" 277 args = ["verify", "database" "prod", "up"] 278 interval = "5s" 279 timeout = "2s" 280 } 281 } 282 ``` 283 284 - - - 285 286 <sup><small>1</small></sup><small> Script checks are not supported for the 287 [qemu driver][qemu] since the Nomad client does not have access to the file 288 system of a task for that driver.</small> 289 290 [service-discovery]: /docs/service-discovery/index.html "Nomad Service Discovery" 291 [interpolation]: /docs/runtime/interpolation.html "Nomad Runtime Interpolation" 292 [network]: /docs/job-specification/network.html "Nomad network Job Specification" 293 [qemu]: /docs/drivers/qemu.html "Nomad qemu Driver"