Consul
Service Router Configuration Entry
The service-router
config entry kind (ServiceRouter
on Kubernetes) 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.
Requirements
- Consul service mesh connect enabled services
- Service to service communication over the protocol
http
- Consul 1.8.4+ on Kubernetes.
- Consul 1.5.0+ on other platforms.
v1.8.4+: On Kubernetes, the ServiceRouter
custom resource is supported in Consul versions 1.8.4+.
v1.6.0+: On other platforms, this config entry is supported in Consul versions 1.6.0+.
Interaction with other Config Entries
Service router config entries are a component of L7 Traffic Management.
Service router config entries are restricted to only services that define their protocol as HTTP-based via a corresponding
service-defaults
config entry or globally viaproxy-defaults
.Any route destination that omits the
ServiceSubset
field is eligible for splitting via aservice-splitter
should one be configured for that service, otherwise resolution proceeds according to any configuredservice-resolver
.
UI
Once a service-router
is successfully entered, you can view it in the UI. Service routers, service splitters, and service resolvers can all be viewed by clicking on your service then switching to the routing tab.
Sample Config Entries
Path prefix matching
Route HTTP requests with a path starting with /admin
to a different service:
Kind = "service-router"
Name = "web"
Routes = [
{
Match {
HTTP {
PathPrefix = "/admin"
}
}
Destination {
Service = "admin"
}
},
# NOTE: a default catch-all will send unmatched traffic to "web"
]
Header/query parameter matching
Route HTTP requests with a special URL parameter or header to a canary subset:
Kind = "service-router"
Name = "web"
Routes = [
{
Match {
HTTP {
Header = [
{
Name = "x-debug"
Exact = "1"
},
]
}
}
Destination {
Service = "web"
ServiceSubset = "canary"
}
},
{
Match {
HTTP {
QueryParam = [
{
Name = "x-debug"
Exact = "1"
},
]
}
}
Destination {
Service = "web"
ServiceSubset = "canary"
}
},
# NOTE: a default catch-all will send unmatched traffic to "web"
]
gRPC routing
Re-route a gRPC method to another service. Since gRPC method calls are HTTP/2, we can use an HTTP path match rule to re-route traffic:
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"
]
Retry logic
Enable retry logic by delegating this responsibility to Consul and the proxy. Review the ServiceRouteDestination
block for more details.
Kind = "service-router"
Name = "orders"
Routes = [
{
Match{
HTTP {
PathPrefix = "/coffees"
}
}
Destination {
Service = "products"
RequestTimeout = "10s"
NumRetries = 3
RetryOnConnectFailure = true
RetryOn = ["reset"]
}
},
{
Match{
HTTP {
PathPrefix = "/orders"
}
}
Destination {
Service = "procurement"
RequestTimeout = "10s"
NumRetries = 3
RetryOnConnectFailure = true
RetryOn = ["reset"]
}
}
]
Available Fields
Kind
- Must be set toservice-router
Name
(string: <required>)
- Set to the name of the service being configured.Namespace
(string: "default")
Enterprise - Specifies the namespace to which the configuration entry will apply.Partition
(string: "default")
Enterprise - Specifies the admin partition to which the configuration will apply.Meta
(map<string|string>: nil)
- Specifies arbitrary KV metadata pairs. Added in Consul 1.8.4.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.Match
(ServiceRouteMatch: <optional>)
- A set of criteria that can match incoming L7 requests. If empty or omitted it acts as a catch-all.HTTP
(ServiceRouteHTTPMatch: <optional>)
- A set of HTTP-specific match criteria.
Destination
(ServiceRouteDestination: <optional>)
- Controls how to proxy the matching request(s) to a service.
ServiceRouteHTTPMatch
PathExact
(string: "")
- Exact path to match on the HTTP request path.
At most only one ofPathExact
,PathPrefix
, orPathRegex
may be configured.PathPrefix
(string: "")
- Path prefix to match on the HTTP request path.
At most only one ofPathExact
,PathPrefix
, orPathRegex
may be configured.PathRegex
(string: "")
- Regular expression to match on the HTTP request path.
The syntax is described below.
At most only one ofPathExact
,PathPrefix
, orPathRegex
may be configured.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.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.Name
(string: <required>)
- Name of the header to match.Present
(bool: false)
- Match if the header with the given name is present with any value.
At most only one ofExact
,Prefix
,Suffix
,Regex
, orPresent
may be configured.Exact
(string: "")
- Match if the header with the given name is this value.
At most only one ofExact
,Prefix
,Suffix
,Regex
, orPresent
may be configured.Prefix
(string: "")
- Match if the header with the given name has this prefix.
At most only one ofExact
,Prefix
,Suffix
,Regex
, orPresent
may be configured.Suffix
(string: "")
- Match if the header with the given name has this suffix.
At most only one ofExact
,Prefix
,Suffix
,Regex
, orPresent
may be configured.Regex
(string: "")
- Match if the header with the given name matches this pattern.
The syntax is described below.
At most only one ofExact
,Prefix
,Suffix
,Regex
, orPresent
may be configured.Invert
(bool: false)
- Inverts the logic of the match
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.Name
(string: <required>)
- The name of the query parameter to match on.Present
(bool: false)
- Match if the query parameter with the given name is present with any value.
At most only one ofExact
,Regex
, orPresent
may be configured.Exact
(string: "")
- Match if the query parameter with the given name is this value.
At most only one ofExact
,Regex
, orPresent
may be configured.Regex
(string: "")
- Match if the query parameter with the given name matches this pattern.
The syntax is described below.
At most only one ofExact
,Regex
, orPresent
may be configured.
ServiceRouteDestination
Service
(string: "")
- The service to resolve instead of the default service. If empty then the default service name is used.ServiceSubset
(string: "")
- A named subset of the given service to resolve instead of the one defined as that service'sDefaultSubset
. If empty, the default subset is used.Namespace
(string: "")
Enterprise - The Consul namespace to resolve the service from instead of the current namespace. If empty, the current namespace is used.Partition
(string: "")
Enterprise - The Consul admin partition to resolve the service from instead of the current partition. If empty, the current partition is used.PrefixRewrite
(string: "")
- Defines how to rewrite the HTTP request path before proxying it to its final destination.
This requires that eitherMatch.HTTP.PathPrefix
orMatch.HTTP.PathExact
be configured on this route.RequestTimeout
(duration: 0)
- The total amount of time permitted for the entire downstream request (and retries) to be processed.IdleTimeout
(duration: 0)
- The total amount of time permitted for the request stream to be idleNumRetries
(int: 1)
- The number of times to retry the request when a retryable result occurs. You cannot set the value to0
. To disable retries, unset all other retry settings (RetryOnConnectFailure
,RetryOn
,RetryOnStatusCodes
)RetryOnConnectFailure
(bool: false)
- Allows for connection failure errors to trigger a retry.RetryOn
(array<string>)
- Allows Consul to retry requests when they meet one of the following sets of conditions:5xx
,gateway-error
,reset
,connect-failure
,envoy-ratelimited
,retriable-4xx
,refused-stream
,cancelled
,deadline-exceeded
,internal
,resource-exhausted
, orunavailable
RetryOnStatusCodes
(array<int>)
- A list of HTTP response status codes that are eligible for retry.RequestHeaders
(HTTPHeaderModifiers: <optional>)
- A set of HTTP-specific header modification rules that will be applied to requests routed to this service. This cannot be used with atcp
listener.ResponseHeaders
(HTTPHeaderModifiers: <optional>)
- A set of HTTP-specific header modification rules that will be applied to responses from this service. This cannot be used with atcp
listener.
HTTPHeaderModifiers
Add
(map<string|string>: optional)
- The set of key/value pairs that specify header values to add. Use header names as keys. Header names are _not_ case-sensitive. If header values with the same name already exist, the value set here will be appended and both will be present. If Envoy is used as the proxy, the value may contain variable placeholders for example%DOWNSTREAM_REMOTE_ADDRESS%
to interpolate dynamic request metadata into the value added.Set
(map<string|string>: optional)
- The set of key/value pairs that specify header values to add. Use header names as keys. Header names are _not_ case-sensitive. If header values with the same name already exist, the value set here will _replace_ them. If Envoy is used as the proxy, the value may contain variable placeholders for example%DOWNSTREAM_REMOTE_ADDRESS%
to interpolate dynamic request metadata into the value added.Remove
(array<string>: optional)
- The set of header names to remove. Only headers whose names are a case-insensitive exact match will be removed
ACLs
Configuration entries may be protected by ACLs.
Reading a service-router
config entry requires service:read
on the resource.
Creating, updating, or deleting a service-router
config entry requires
service:write
on the resource and service:read
on any other service referenced by
name in these fields:
Regular Expression Syntax
The actual syntax of the regular expression fields described here is entirely proxy-specific.
When using Envoy as a proxy (the only supported proxy in Kubernetes), the syntax for these fields is version specific:
Envoy Version | Syntax |
---|---|
1.11.2 or newer | documentation |
1.11.1 or older | documentation |