github.com/clly/consul@v1.4.5/website/source/api/agent/service.html.md (about)

     1  ---
     2  layout: api
     3  page_title: Service - Agent - HTTP API
     4  sidebar_current: api-agent-service
     5  description: |-
     6    The /agent/service endpoints interact with services on the local agent in
     7    Consul.
     8  ---
     9  
    10  # Service - Agent HTTP API
    11  
    12  The `/agent/service` endpoints interact with services on the local agent in
    13  Consul. These should not be confused with services in the catalog.
    14  
    15  ## List Services
    16  
    17  This endpoint returns all the services that are registered with
    18  the local agent. These services were either provided through configuration files
    19  or added dynamically using the HTTP API.
    20  
    21  It is important to note that the services known by the agent may be different
    22  from those reported by the catalog. This is usually due to changes being made
    23  while there is no leader elected. The agent performs active
    24  [anti-entropy](/docs/internals/anti-entropy.html), so in most situations
    25  everything will be in sync within a few seconds.
    26  
    27  | Method | Path                         | Produces                   |
    28  | ------ | ---------------------------- | -------------------------- |
    29  | `GET`  | `/agent/services`            | `application/json`         |
    30  
    31  The table below shows this endpoint's support for
    32  [blocking queries](/api/index.html#blocking-queries),
    33  [consistency modes](/api/index.html#consistency-modes),
    34  [agent caching](/api/index.html#agent-caching), and
    35  [required ACLs](/api/index.html#acls).
    36  
    37  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required   |
    38  | ---------------- | ----------------- | ------------- | -------------- |
    39  | `NO`             | `none`            | `none`        | `service:read` |
    40  
    41  ### Sample Request
    42  
    43  ```text
    44  $ curl \
    45      http://127.0.0.1:8500/v1/agent/services
    46  ```
    47  
    48  ### Sample Response
    49  
    50  ```json
    51  {
    52    "redis": {
    53        "ID": "redis",
    54        "Service": "redis",
    55        "Tags": [],
    56        "Meta": {
    57            "redis_version": "4.0"
    58        },
    59        "Port": 8000,
    60        "Address": "",
    61        "EnableTagOverride": false,
    62        "Weights": {
    63            "Passing": 10,
    64            "Warning": 1
    65        }
    66    }
    67  }
    68  ```
    69  
    70  ## Get Service Configuration
    71  
    72  This endpoint was added in Consul 1.3.0 and returns the full service definition
    73  for a single service instance registered on the local agent. It is used by
    74  [Connect proxies](/docs/connect/proxies.html) to discover the embedded proxy
    75  configuration that was registered with the instance.
    76  
    77  It is important to note that the services known by the agent may be different
    78  from those reported by the catalog. This is usually due to changes being made
    79  while there is no leader elected. The agent performs active
    80  [anti-entropy](/docs/internals/anti-entropy.html), so in most situations
    81  everything will be in sync within a few seconds.
    82  
    83  | Method | Path                         | Produces                   |
    84  | ------ | ---------------------------- | -------------------------- |
    85  | `GET`  | `/agent/service/:service_id` | `application/json`         |
    86  
    87  The table below shows this endpoint's support for
    88  [blocking queries](/api/index.html#blocking-queries),
    89  [consistency modes](/api/index.html#consistency-modes),
    90  [agent caching](/api/index.html#agent-caching), and
    91  [required ACLs](/api/index.html#acls).
    92  
    93  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required   |
    94  | ---------------- | ----------------- | ------------- | -------------- |
    95  | `YES`<sup>1</sup>| `none`            | `none`        | `service:read` |
    96  
    97  <sup>1</sup> Supports [hash-based
    98  blocking](/api/index.html#hash-based-blocking-queries) only.
    99  
   100  ### Parameters
   101  
   102  - `service_id` `(string: <required>)` - Specifies the ID of the service to
   103    fetch. This is specified as part of the URL.
   104  
   105  ### Sample Request
   106  
   107  ```text
   108  $ curl \
   109      http://127.0.0.1:8500/v1/agent/service/web-sidecar-proxy
   110  ```
   111  
   112  ### Sample Response
   113  
   114  ```json
   115  {
   116      "Kind": "connect-proxy",
   117      "ID": "web-sidecar-proxy",
   118      "Service": "web-sidecar-proxy",
   119      "Tags": null,
   120      "Meta": null,
   121      "Port": 18080,
   122      "Address": "",
   123      "Weights": {
   124          "Passing": 1,
   125          "Warning": 1
   126      },
   127      "EnableTagOverride": false,
   128      "ContentHash": "4ecd29c7bc647ca8",
   129      "Proxy": {
   130          "DestinationServiceName": "web",
   131          "DestinationServiceID": "web",
   132          "LocalServiceAddress": "127.0.0.1",
   133          "LocalServicePort": 8080,
   134          "Config": {
   135              "foo": "bar"
   136          },
   137          "Upstreams": [
   138              {
   139                  "DestinationType": "service",
   140                  "DestinationName": "db",
   141                  "LocalBindPort": 9191
   142              }
   143          ]
   144      }
   145  }
   146  ```
   147  
   148  The response has the same structure as the [service
   149  definition](/docs/agent/services.html) with one extra field `ContentHash` which
   150  contains the [hash-based blocking
   151  query](/api/index.html#hash-based-blocking-queries) hash for the result. The
   152  same hash is also present in `X-Consul-ContentHash`.
   153  
   154  ## Get local service health
   155  
   156  Retrieve an aggregated state of service(s) on the local agent by name.
   157  
   158  This endpoints support JSON format and text/plain formats, JSON being the
   159  default. In order to get the text format, you can append `?format=text` to
   160  the URL or use Mime Content negotiation by specifying a HTTP Header
   161  `Accept` starting with `text/plain`.
   162  
   163  | Method | Path                                                      | Produces           |
   164  | ------ | --------------------------------------------------------- | ------------------ |
   165  | `GET`  | `/v1/agent/health/service/name/:service_name`             | `application/json` |
   166  | `GET`  | `/v1/agent/health/service/name/:service_name?format=text` | `text/plain`       |
   167  
   168  The table below shows this endpoint's support for
   169  [blocking queries](/api/index.html#blocking-queries),
   170  [consistency modes](/api/index.html#consistency-modes),
   171  [agent caching](/api/index.html#agent-caching), and
   172  [required ACLs](/api/index.html#acls).
   173  
   174  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required   |
   175  | ---------------- | ----------------- | ------------- | -------------- |
   176  | `NO`             | `none`            | `none`        | `service:read` |
   177  
   178  Those endpoints return the aggregated values of all healthchecks for the
   179  service instance(s) and will return the corresponding HTTP codes:
   180  
   181  | Result | Meaning                                                         |
   182  | ------ | ----------------------------------------------------------------|
   183  | `200`  | All healthchecks of every matching service instance are passing |
   184  | `400`  | Bad parameter (missing service name of id)                      |
   185  | `404`  | No such service id or name                                      |
   186  | `429`  | Some healthchecks are passing, at least one is warning          |
   187  | `503`  | At least one of the healthchecks is critical                    |
   188  
   189  Those endpoints might be usefull for the following use-cases:
   190  
   191  * a load-balancer wants to check IP connectivity with an agent and retrieve
   192    the aggregated status of given service
   193  * create aliases for a given service (thus, the healthcheck of alias uses
   194    http://localhost:8500/v1/agent/service/id/aliased_service_id healthcheck)
   195  
   196  
   197  ##### Note
   198  If you know the ID of service you want to target, it is recommended to use
   199  [`/v1/agent/health/service/id/:service_id`](/api/agent/service.html#get-local-service-health-by-id)
   200  so you have the result for the service only. When requesting
   201  `/v1/agent/health/service/name/:service_name`, the caller will receive the
   202  worst state of all services having the given name.
   203  
   204  ### Sample Requests
   205  
   206  Given 2 services with name `web`, with web2 critical and web1 passing:
   207  
   208  #### List worst statuses of all instances of web-demo services (HTTP 503)
   209  
   210  ##### By Name, Text
   211  
   212  ```shell
   213  curl http://localhost:8500/v1/agent/health/service/name/web?format=text
   214  critical
   215  ```
   216  
   217  ##### By Name, JSON
   218  
   219  In JSON, the detail of passing/warning/critical services is present in output,
   220  in a array.
   221  
   222  ```shell
   223  curl localhost:8500/v1/agent/health/service/name/web
   224  ```
   225  
   226  ```json
   227  {
   228      "critical": [
   229          {
   230              "ID": "web2",
   231              "Service": "web",
   232              "Tags": [
   233                  "rails"
   234              ],
   235              "Address": "",
   236              "Meta": null,
   237              "Port": 80,
   238              "EnableTagOverride": false,
   239              "ProxyDestination": "",
   240              "Connect": {
   241                  "Native": false,
   242                  "Proxy": null
   243              },
   244              "CreateIndex": 0,
   245              "ModifyIndex": 0
   246          }
   247      ],
   248      "passing": [
   249          {
   250              "ID": "web1",
   251              "Service": "web",
   252              "Tags": [
   253                  "rails"
   254              ],
   255              "Address": "",
   256              "Meta": null,
   257              "Port": 80,
   258              "EnableTagOverride": false,
   259              "ProxyDestination": "",
   260              "Connect": {
   261                  "Native": false,
   262                  "Proxy": null
   263              },
   264              "CreateIndex": 0,
   265              "ModifyIndex": 0
   266          }
   267      ]
   268  }
   269  ```
   270  
   271  #### List status of web2 (HTTP 503)
   272  
   273  ##### Failure By ID, Text
   274  
   275  ```shell
   276  curl http://localhost:8500/v1/agent/health/service/id/web2?format=text
   277  critical
   278  ```
   279  
   280  ##### Failure By ID, JSON
   281  
   282  In JSON, the output per ID is not an array, but only contains the value
   283  of service.
   284  
   285  ```shell
   286  curl localhost:8500/v1/agent/health/service/id/web2
   287  ```
   288  
   289  ```json
   290  {
   291      "critical": {
   292          "ID": "web2",
   293          "Service": "web",
   294          "Tags": [
   295              "rails"
   296          ],
   297          "Address": "",
   298          "Meta": null,
   299          "Port": 80,
   300          "EnableTagOverride": false,
   301          "ProxyDestination": "",
   302          "Connect": {
   303              "Native": false,
   304              "Proxy": null
   305          },
   306          "CreateIndex": 0,
   307          "ModifyIndex": 0
   308      }
   309  }
   310  ```
   311  
   312  #### List status of web2 (HTTP 200)
   313  
   314  ##### Success By ID, Text
   315  
   316  ```shell
   317  curl localhost:8500/v1/agent/health/service/id/web1?format=text
   318  passing
   319  ```
   320  
   321  #### Success By ID, JSON
   322  
   323  ```shell
   324  curl localhost:8500/v1/agent/health/service/id/web1
   325  ```
   326  
   327  ```json
   328  {
   329      "passing": {
   330          "ID": "web1",
   331          "Service": "web",
   332          "Tags": [
   333              "rails"
   334          ],
   335          "Address": "",
   336          "Meta": null,
   337          "Port": 80,
   338          "EnableTagOverride": false,
   339          "ProxyDestination": "",
   340          "Connect": {
   341              "Native": false,
   342              "Proxy": null
   343          },
   344          "CreateIndex": 0,
   345          "ModifyIndex": 0
   346      }
   347  }
   348  ```
   349  
   350  ## Get local service health by its ID
   351  
   352  Retrive an aggregated state of service(s) on the local agent by ID.
   353  
   354  See:
   355  
   356  | Method | Path                                                   | Produces           |
   357  | ------ | ------------------------------------------------------ | ------------------ |
   358  | `GET`  | `/v1/agent/health/service/id/:service_id`             | `application/json` |
   359  | `GET`  | `/v1/agent/health/service/id/:service_id?format=text` | `text/plain`       |
   360  
   361  Parameters and response format are the same as
   362  [`/v1/agent/health/service/name/:service_name`](/api/agent/service.html#get-local-service-health).
   363  
   364  ## Register Service
   365  
   366  This endpoint adds a new service, with an optional health check, to the local
   367  agent.
   368  
   369  The agent is responsible for managing the status of its local services, and for
   370  sending updates about its local services to the servers to keep the global
   371  catalog in sync.
   372  
   373  For "connect-proxy" kind services, the `service:write` ACL for the
   374  `Proxy.DestinationServiceName` value is also required to register the service.
   375  
   376  | Method | Path                         | Produces                   |
   377  | ------ | ---------------------------- | -------------------------- |
   378  | `PUT`  | `/agent/service/register`    | `application/json`         |
   379  
   380  The table below shows this endpoint's support for
   381  [blocking queries](/api/index.html#blocking-queries),
   382  [consistency modes](/api/index.html#consistency-modes),
   383  [agent caching](/api/index.html#agent-caching), and
   384  [required ACLs](/api/index.html#acls).
   385  
   386  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required    |
   387  | ---------------- | ----------------- | ------------- | --------------- |
   388  | `NO`             | `none`            | `none`        | `service:write` |
   389  
   390  ### Parameters
   391  
   392  Note that this endpoint, unlike most also [supports `snake_case`](/docs/agent/services.html#service-definition-parameter-case)
   393  service definition keys for compatibility with the config file format.
   394  
   395  - `Name` `(string: <required>)` - Specifies the logical name of the service.
   396    Many service instances may share the same logical service name.
   397  
   398  - `ID` `(string: "")` - Specifies a unique ID for this service. This must be
   399    unique per _agent_. This defaults to the `Name` parameter if not provided.
   400  
   401  - `Tags` `(array<string>: nil)` - Specifies a list of tags to assign to the
   402    service. These tags can be used for later filtering and are exposed via the
   403    APIs.
   404  
   405  - `Address` `(string: "")` - Specifies the address of the service. If not
   406    provided, the agent's address is used as the address for the service during
   407    DNS queries.
   408  
   409  - `Meta` `(map<string|string>: nil)` - Specifies arbitrary KV metadata
   410    linked to the service instance.
   411  
   412  - `Port` `(int: 0)` - Specifies the port of the service.
   413  
   414  - `Kind` `(string: "")` - The kind of service. Defaults to "" which is a
   415    typical Consul service. This value may also be "connect-proxy" for
   416    services that are [Connect-capable](/docs/connect/index.html)
   417    proxies representing another service.
   418  
   419  - `ProxyDestination` `(string: "")` - **Deprecated** From 1.2.0 to 1.2.3 this
   420    was used for "connect-proxy" `Kind` services however the equivalent field is
   421    now in `Proxy.DestinationServiceName`. Registrations using this field will
   422    continue to work until some later major version where this will be removed
   423    entirely. It's strongly recommended to switch to using the new field.
   424  
   425  - `Proxy` `(Proxy: nil)` - From 1.2.3 on, specifies the configuration for a
   426    Connect proxy instance. This is only valid if `Kind == "connect-proxy"`. See
   427    the [Proxy documentation](/docs/connect/proxies.html) for full details.
   428  
   429  - `Connect` `(Connect: nil)` - Specifies the
   430    [configuration for Connect](/docs/connect/configuration.html). See the
   431    [Connect Structure](#connect-structure) section below for supported fields.
   432  
   433  - `Check` `(Check: nil)` - Specifies a check. Please see the
   434    [check documentation](/api/agent/check.html) for more information about the
   435    accepted fields. If you don't provide a name or id for the check then they
   436    will be generated. To provide a custom id and/or name set the `CheckID`
   437    and/or `Name` field.
   438  
   439  - `Checks` `(array<Check>: nil)` - Specifies a list of checks. Please see the
   440    [check documentation](/api/agent/check.html) for more information about the
   441    accepted fields. If you don't provide a name or id for the check then they
   442    will be generated. To provide a custom id and/or name set the `CheckID`
   443    and/or `Name` field. The automatically generated `Name` and `CheckID` depend
   444    on the position of the check within the array, so even though the behavior is
   445    deterministic, it is recommended for all checks to either let consul set the
   446    `CheckID` by leaving the field empty/omitting it or to provide a unique value.
   447  
   448  - `EnableTagOverride` `(bool: false)` - Specifies to disable the anti-entropy
   449    feature for this service's tags. If `EnableTagOverride` is set to `true` then
   450    external agents can update this service in the [catalog](/api/catalog.html)
   451    and modify the tags. Subsequent local sync operations by this agent will
   452    ignore the updated tags. For instance, if an external agent modified both the
   453    tags and the port for this service and `EnableTagOverride` was set to `true`
   454    then after the next sync cycle the service's port would revert to the original
   455    value but the tags would maintain the updated value. As a counter example, if
   456    an external agent modified both the tags and port for this service and
   457    `EnableTagOverride` was set to `false` then after the next sync cycle the
   458    service's port _and_ the tags would revert to the original value and all
   459    modifications would be lost.
   460  
   461  - `Weights` `(Weights: nil)` - Specifies weights for the service. Please see the
   462    [service documentation](/docs/agent/services.html) for more information about
   463    weights. If this field is not provided weights will default to
   464    `{"Passing": 1, "Warning": 1}`.
   465  
   466      It is important to note that this applies only to the locally registered
   467      service. If you have multiple nodes all registering the same service their
   468      `EnableTagOverride` configuration and all other service configuration items
   469      are independent of one another. Updating the tags for the service registered
   470      on one node is independent of the same service (by name) registered on
   471      another node. If `EnableTagOverride` is not specified the default value is
   472      `false`. See [anti-entropy syncs](/docs/internals/anti-entropy.html) for
   473      more info.
   474  
   475  #### Connect Structure
   476  
   477  For the `Connect` field, the parameters are:
   478  
   479  - `Native` `(bool: false)` - Specifies whether this service supports
   480    the [Connect](/docs/connect/index.html) protocol [natively](/docs/connect/native.html).
   481    If this is true, then Connect proxies, DNS queries, etc. will be able to
   482    service discover this service.
   483  - `Proxy` `(Proxy: nil)` -
   484    [**Deprecated**](/docs/connect/proxies/managed-deprecated.html) Specifies that
   485    a managed Connect proxy should be started for this service instance, and
   486    optionally provides configuration for the proxy. The format is as documented
   487    in [Managed Proxy Deprecation](/docs/connect/proxies/managed-deprecated.html).
   488  - `SidecarService` `(ServiceDefinition: nil)` - Specifies an optional nested
   489    service definition to register. For more information see
   490    [Sidecar Service Registration](/docs/connect/proxies/sidecar-service.html).
   491  
   492  ### Sample Payload
   493  
   494  ```json
   495  {
   496    "ID": "redis1",
   497    "Name": "redis",
   498    "Tags": [
   499      "primary",
   500      "v1"
   501    ],
   502    "Address": "127.0.0.1",
   503    "Port": 8000,
   504    "Meta": {
   505      "redis_version": "4.0"
   506    },
   507    "EnableTagOverride": false,
   508    "Check": {
   509      "DeregisterCriticalServiceAfter": "90m",
   510      "Args": ["/usr/local/bin/check_redis.py"],
   511      "HTTP": "http://localhost:5000/health",
   512      "Interval": "10s",
   513      "TTL": "15s"
   514    },
   515    "Weights": {
   516      "Passing": 10,
   517      "Warning": 1
   518    }
   519  }
   520  ```
   521  
   522  ### Sample Request
   523  
   524  ```text
   525  $ curl \
   526      --request PUT \
   527      --data @payload.json \
   528      http://127.0.0.1:8500/v1/agent/service/register
   529  ```
   530  
   531  ## Deregister Service
   532  
   533  This endpoint removes a service from the local agent. If the service does not
   534  exist, no action is taken.
   535  
   536  The agent will take care of deregistering the service with the catalog. If there
   537  is an associated check, that is also deregistered.
   538  
   539  | Method | Path                         | Produces                   |
   540  | ------ | ---------------------------- | -------------------------- |
   541  | `PUT`  | `/agent/service/deregister/:service_id` | `application/json` |
   542  
   543  The table below shows this endpoint's support for
   544  [blocking queries](/api/index.html#blocking-queries),
   545  [consistency modes](/api/index.html#consistency-modes),
   546  [agent caching](/api/index.html#agent-caching), and
   547  [required ACLs](/api/index.html#acls).
   548  
   549  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required    |
   550  | ---------------- | ----------------- | ------------- | --------------- |
   551  | `NO`             | `none`            | `none`        | `service:write` |
   552  
   553  ### Parameters
   554  
   555  - `service_id` `(string: <required>)` - Specifies the ID of the service to
   556    deregister. This is specified as part of the URL.
   557  
   558  ### Sample Request
   559  
   560  ```text
   561  $ curl \
   562      --request PUT \
   563      http://127.0.0.1:8500/v1/agent/service/deregister/my-service-id
   564  ```
   565  
   566  ## Enable Maintenance Mode
   567  
   568  This endpoint places a given service into "maintenance mode". During maintenance
   569  mode, the service will be marked as unavailable and will not be present in DNS
   570  or API queries. This API call is idempotent. Maintenance mode is persistent and
   571  will be automatically restored on agent restart.
   572  
   573  | Method | Path                         | Produces                   |
   574  | ------ | ---------------------------- | -------------------------- |
   575  | `PUT`  | `/agent/service/maintenance/:service_id` | `application/json`         |
   576  
   577  The table below shows this endpoint's support for
   578  [blocking queries](/api/index.html#blocking-queries),
   579  [consistency modes](/api/index.html#consistency-modes),
   580  [agent caching](/api/index.html#agent-caching), and
   581  [required ACLs](/api/index.html#acls).
   582  
   583  | Blocking Queries | Consistency Modes | Agent Caching | ACL Required    |
   584  | ---------------- | ----------------- | ------------- | --------------- |
   585  | `NO`             | `none`            | `none`        | `service:write` |
   586  
   587  ### Parameters
   588  
   589  - `service_id` `(string: <required>)` - Specifies the ID of the service to put
   590    in maintenance mode. This is specified as part of the URL.
   591  
   592  - `enable` `(bool: <required>)` - Specifies whether to enable or disable
   593    maintenance mode. This is specified as part of the URL as a query string
   594    parameter.
   595  
   596  - `reason` `(string: "")` - Specifies a text string explaining the reason for
   597    placing the node into maintenance mode. This is simply to aid human operators.
   598    If no reason is provided, a default value will be used instead. This is
   599    specified as part of the URL as a query string parameter, and, as such, must
   600    be URI-encoded.
   601  
   602  ### Sample Request
   603  
   604  ```text
   605  $ curl \
   606      --request PUT \
   607      http://127.0.0.1:8500/v1/agent/service/maintenance/my-service-id?enable=true&reason=For+the+docs
   608  ```