consul/website/pages/docs/agent/config-entries/service-router.mdx

289 lines
9.0 KiB
Plaintext
Raw Normal View History

2019-07-09 02:11:19 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
2020-04-06 20:27:35 +00:00
page_title: 'Configuration Entry Kind: Service Router'
sidebar_title: service-router
2020-04-07 18:55:19 +00:00
description: >-
The service-router config entry kind controls Connect traffic routing and
manipulation at networking layer 7 (e.g. HTTP).
2019-07-09 02:11:19 +00:00
---
# Service Router
2019-07-09 02:11:19 +00:00
-> **1.6.0+:** This config entry is available in Consul versions 1.6.0 and newer.
2019-07-09 02:11:19 +00:00
The `service-router` config entry kind controls Connect traffic routing and
manipulation at networking layer 7 (e.g. HTTP).
If a router is not explicitly configured or is configured with no routes then
the system behaves as if a router were configured sending all traffic to a
service of the same name.
## Interaction with other Config Entries
- Service router config entries are a component of [L7 Traffic
2020-04-09 23:46:54 +00:00
Management](/docs/connect/l7-traffic-management).
2019-07-09 02:11:19 +00:00
- Service router config entries are restricted to only services that define
their protocol as HTTP-based via a corresponding
2020-04-09 23:46:54 +00:00
[`service-defaults`](/docs/agent/config-entries/service-defaults) config
2019-07-09 02:11:19 +00:00
entry or globally via
2020-04-09 23:46:54 +00:00
[`proxy-defaults`](/docs/agent/config-entries/proxy-defaults) .
2019-07-09 02:11:19 +00:00
- Any route destination that omits the `ServiceSubset` field is eligible for
splitting via a
2020-04-09 23:46:54 +00:00
[`service-splitter`](/docs/agent/config-entries/service-splitter) should
2019-07-09 02:11:19 +00:00
one be configured for that service, otherwise resolution proceeds according
to any configured
2020-04-09 23:46:54 +00:00
[`service-resolver`](/docs/agent/config-entries/service-resolver).
2019-07-09 02:11:19 +00:00
## Sample Config Entries
Route HTTP requests with a path starting with `/admin` to a different service:
```hcl
Kind = "service-router"
Name = "web"
Routes = [
2019-07-09 02:11:19 +00:00
{
Match {
HTTP {
PathPrefix = "/admin"
2019-07-09 02:11:19 +00:00
}
}
Destination {
Service = "admin"
2019-07-09 02:11:19 +00:00
}
},
# NOTE: a default catch-all will send unmatched traffic to "web"
]
```
Route HTTP requests with a special url parameter or header to a canary subset:
```hcl
Kind = "service-router"
Name = "web"
Routes = [
2019-07-09 02:11:19 +00:00
{
Match {
HTTP {
Header = [
2019-07-09 02:11:19 +00:00
{
Name = "x-debug"
Exact = "1"
2019-07-09 02:11:19 +00:00
},
]
}
}
Destination {
Service = "web"
ServiceSubset = "canary"
2019-07-09 02:11:19 +00:00
}
},
{
Match {
HTTP {
QueryParam = [
2019-07-09 02:11:19 +00:00
{
Name = "x-debug"
Exact = "1"
2019-07-09 02:11:19 +00:00
},
]
}
}
Destination {
Service = "web"
ServiceSubset = "canary"
2019-07-09 02:11:19 +00:00
}
},
# NOTE: a default catch-all will send unmatched traffic to "web"
]
```
Re-route a gRPC method to another service. Since gRPC method calls [are
HTTP/2](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md), we can use an HTTP path match rule to re-route traffic:
```hcl
Kind = "service-router"
Name = "billing"
Routes = [
{
Match {
HTTP {
PathExact = "/mycompany.BillingService/GenerateInvoice"
}
}
Destination {
Service = "invoice-generator"
}
},
# NOTE: a default catch-all will send unmatched traffic to "billing"
]
```
2019-07-09 02:11:19 +00:00
## Available Fields
- `Kind` - Must be set to `service-router`
- `Name` `(string: <required>)` - Set to the name of the service being configured.
- `Namespace` `(string: "default")` <EnterpriseAlert inline /> - Specifies the namespace the config entry will apply to.
- `Meta` `(map<string|string>: nil)` - Specifies arbitrary KV metadata pairs. Added in Consul 1.8.4.
2019-07-09 02:11:19 +00:00
- `Routes` `(array<ServiceRoute>)` - The list of routes to consider when
processing L7 requests. The first route to match in the list is terminal and
stops further evaluation. Traffic that fails to match any of the provided
routes will be routed to the default service.
2020-04-06 20:27:35 +00:00
- `Match` `(ServiceRouteMatch: <optional>)` - A set of criteria that can
match incoming L7 requests. If empty or omitted it acts as a catch-all.
2020-04-06 20:27:35 +00:00
- `HTTP` `(ServiceRouteHTTPMatch: <optional>)` - A set of
[HTTP-specific match criteria](#serviceroutehttpmatch).
2020-04-06 20:27:35 +00:00
- `Destination` `(ServiceRouteDestination: <optional>)` - Controls [how to
proxy](#serviceroutedestination) the matching request(s) to a
service.
### `ServiceRouteHTTPMatch`
- `PathExact` `(string: "")` - Exact path to match on the HTTP request path.
2020-04-06 20:27:35 +00:00
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
2019-07-09 02:11:19 +00:00
- `PathPrefix` `(string: "")` - Path prefix to match on the HTTP request path.
2019-07-09 02:11:19 +00:00
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
2019-07-09 02:11:19 +00:00
- `PathRegex` `(string: "")` - Regular expression to match on the HTTP
request path.
2019-07-09 02:11:19 +00:00
The syntax is [described below](#regular-expression-syntax).
2019-07-09 02:11:19 +00:00
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
2019-07-09 02:11:19 +00:00
- `Methods` `(array<string>)` - A list of HTTP methods for which this match
applies. If unspecified all HTTP methods are matched.
If provided the names must be a valid [method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods).
- `Header` `(array<ServiceRouteHTTPMatchHeader>)` - A set of criteria that can
match on HTTP request headers. If more than one is configured all must match
for the overall match to apply.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Name` `(string: <required>)` - Name of the header to match.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Present` `(bool: false)` - Match if the header with the given name is
present with any value.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
may be configured.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Exact` `(string: "")` - Match if the header with the given name is this
value.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
may be configured.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Prefix` `(string: "")` - Match if the header with the given name has
this prefix.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
may be configured.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Suffix` `(string: "")` - Match if the header with the given name has
this suffix.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
may be configured.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Regex` `(string: "")` - Match if the header with the given name matches
this pattern.
2019-07-09 02:11:19 +00:00
The syntax is [described below](#regular-expression-syntax).
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
may be configured.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Invert` `(bool: false)` - Inverts the logic of the match.
2019-07-09 02:11:19 +00:00
- `QueryParam` `(array<ServiceRouteHTTPMatchQueryParam>)` - A set of criteria
that can match on HTTP query parameters. If more than one is configured all
must match for the overall match to apply.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Name` `(string: <required>)` - The name of the query parameter to match on.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
- `Present` `(bool: false)` - Match if the query parameter with the given
name is present with any value.
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Regex`, or `Present` may be configured.
2020-04-06 20:27:35 +00:00
- `Exact` `(string: "")` - Match if the query parameter with the given name
is this value.
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Regex`, or `Present` may be configured.
2020-04-06 20:27:35 +00:00
- `Regex` `(string: "")` - Match if the query parameter with the given name
matches this pattern.
The syntax is [described below](#regular-expression-syntax).
2019-07-09 02:11:19 +00:00
2020-04-06 20:27:35 +00:00
At most only one of `Exact`, `Regex`, or `Present` may be configured.
2019-07-09 02:11:19 +00:00
### `ServiceRouteDestination`
2019-07-09 02:11:19 +00:00
- `Service` `(string: "")` - The service to resolve instead of the default
service. If empty then the default service name is used.
2019-07-09 02:11:19 +00:00
- `ServiceSubset` `(string: "")` - A named subset of the given service to
resolve instead of the one defined as that service's `DefaultSubset`. If empty,
the default subset is used.
2019-07-09 02:11:19 +00:00
- `Namespace` `(string: "")` - The namespace to resolve the service from
instead of the current namespace. If empty the current namespace is assumed.
2019-07-09 02:11:19 +00:00
- `PrefixRewrite` `(string: "")` - Defines how to rewrite the HTTP request path
before proxying it to its final destination.
2019-07-09 02:11:19 +00:00
This requires that either `Match.HTTP.PathPrefix` or
`Match.HTTP.PathExact` be configured on this route.
2019-07-09 02:11:19 +00:00
- `RequestTimeout` `(duration: 0s)` - The total amount of time permitted for
the entire downstream request (and retries) to be processed.
2019-07-09 02:11:19 +00:00
- `NumRetries` `(int: 0)` - The number of times to retry the request when a
retryable result occurs.
2019-07-09 02:11:19 +00:00
- `RetryOnConnectFailure` `(bool: false)` - Allows for connection failure
errors to trigger a retry.
2019-07-09 02:11:19 +00:00
- `RetryOnStatusCodes` `(array<int>)` - A list of HTTP response status codes
that are eligible for retry.
2019-07-09 02:11:19 +00:00
## ACLs
Configuration entries may be protected by [ACLs](/docs/security/acl).
2019-07-09 02:11:19 +00:00
Reading a `service-router` config entry requires `service:read` on the resource.
2019-07-09 02:11:19 +00:00
Creating, updating, or deleting a `service-router` config entry requires
`service:write` on the resource and `service:read` on any other service referenced by
2019-07-09 02:11:19 +00:00
name in these fields:
- [`Routes[].Destination.Service`](#service)
## Regular Expression Syntax
The actual syntax of the regular expression fields described here is entirely
proxy-specific.
When using [Envoy](/docs/connect/proxies/envoy) as a proxy, the syntax for
these fields is version specific:
| Envoy Version | Syntax |
| --------------- | -------------------- |
| 1.11.2 or newer | [documentation](https://github.com/google/re2/wiki/Syntax) |
| 1.11.1 or older | [documentation](https://en.cppreference.com/w/cpp/regex/ecmascript) |