Service router configuration entry reference

This page provides reference information for service router configuration entries. Service routers use L7 network information to redirect a traffic request for a service to one or more specific service instances.

Refer to L7 traffic management overview for additional information.

Configuration model

The following list outlines field hierarchy, language-specific data types, and requirements in this configuration entry. Click on a property name to view additional details, including default values.

Complete configuration

When every field is defined, a service router configuration entry has the following form:

  1. Kind = "service-router" ## required
  2. Name = "<service-name>" ## required
  3. Namespace = "<namespace-configuration-applies-to>"
  4. Partition = "<partition-configuration-applies-to>"
  5. Meta = {
  6. <key> = "<value>"
  7. }
  8. Routes = [
  9. {
  10. Match {
  11. HTTP {
  12. PathExact = "<exact/http/path>" ## cannot specify with PathPrefix or PathRegex
  13. },
  14. HTTP {
  15. PathPrefix = "<path/prefix>" ## cannot specify with PathExact or PathRegex
  16. },
  17. HTTP {
  18. PathRegex = "<regex/path>" ## cannot specify with PathPrefix or PathExact
  19. },
  20. HTTP {
  21. Methods = ["GET", "POST", "PUT"]
  22. },
  23. HTTP {
  24. Header = [ ## do not specify Present, Exact, Prefix, Suffix, and Regex in a single Header
  25. {
  26. Name = "<header-to-match>" ## required when specifying Routes.Match.HTTP.Header
  27. Present = false
  28. Exact = "<exact-match-name>"
  29. Prefix = "<prefix-to-match>"
  30. Suffix = "<suffix-to-match>"
  31. Regex = "<regex-to-match>"
  32. Invert = false
  33. }
  34. ]
  35. }
  36. HTTP {
  37. QueryParam = [ ## do not specify Present, Exact, and Regex in a single QueryParam
  38. Name = "<text-to-match-in-query-parameter>" ## required when specifying Routes.Match.HTTP.Header
  39. Present = false
  40. Exact = "<exact-text-to-match-in-query-parameter>"
  41. Regex = "<regex-to-match-in-query-paramater>"
  42. ]
  43. }
  44. },
  45. Destination {
  46. Service = "<service-name-at-destination>"
  47. ServiceSubset = "<service-subset-at-destination>"
  48. Namespace = "<namespace-at-destination>"
  49. Partition = "<partition-at-destination>"
  50. PrefixRewrite = "<new-prefix-after-routing>" ## required specifying either Routes.Match.HTTP.PathPrefix or Routes.Match.HTTP.PathExact
  51. RequestTimeout = 0
  52. IdleTimeout = 0
  53. NumRetries = 1
  54. RetryOnConnectFailure = false
  55. RetryOn = ["reset", "unavailable"]
  56. RetryOnStatusCodes = [500, 502, 503]
  57. RequestHeaders = {
  58. Set = {
  59. "X-Web-Version" : "<text-string>"
  60. }
  61. }
  62. ResponseHeaders = {
  63. Set = {
  64. "X-Web-Version" : "<text-string>"
  65. }
  66. }
  67. }
  68. }
  69. ]
  1. {
  2. "Kind": "service-router", // required
  3. "Name": "<service-name>", // required
  4. "Namespace": "<namespace-configuration-applies-to>",
  5. "Partition": "<partition-configuration-applies-to>",
  6. "Meta": {
  7. "<key>": "<value>"
  8. },
  9. "Routes": [
  10. {
  11. "Match": {
  12. "HTTP": {
  13. "PathExact": "<exact/http/path>" // cannot specify with PathPrefix or PathRegex
  14. },
  15. "HTTP": {
  16. "PathPrefix": "<path/prefix>" // cannot specify with PathExact or PathRegex
  17. },
  18. "HTTP": {
  19. "PathRegex": "<regex/path>" // cannot specify with PathPrefix or PathExact
  20. },
  21. "HTTP": {
  22. "Methods": ["GET", "POST", "PUT"]
  23. },
  24. "HTTP": {
  25. "Header": [ // do not specify Present, Exact, Prefix, Suffix, and Regex in a single Header
  26. {
  27. "Name": "<header-to-match>", // required when specifying Routes.Match.HTTP.Header
  28. "Present": false,
  29. "Exact": "<exact-match-name>",
  30. "Prefix": "<prefix-to-match>",
  31. "Suffix": "<suffix-to-match>",
  32. "Regex": "<regex-to-match>",
  33. "Invert": false,
  34. }
  35. ]
  36. },
  37. "HTTP": {
  38. "QueryParam": [ // do not specify Present, Exact, and Regex in a single QueryParam
  39. "Name": "<text-to-match-in-query-parameter>", // required when specifying Routes.Match.HTTP.Header
  40. "Present": false,
  41. "Exact": "<exact-text-to-match-in-query-parameter>",
  42. "Regex": "<regex-to-match-in-query-paramater>"
  43. ]
  44. }
  45. },
  46. "Destination": {
  47. "Service": "<service-name-at-destination>",
  48. "ServiceSubset": "<service-subset-at-destination>",
  49. "Namespace": "<namespace-at-destination>",
  50. "Partition": "<partition-at-destination>",
  51. "PrefixRewrite": "<new-prefix-after-routing>", // required specifying either Routes.Match.HTTP.PathPrefix or Routes.Match.HTTP.PathExact
  52. "RequestTimeout": 0,
  53. "IdleTimeout": 0,
  54. "NumRetries": 1,
  55. "RetryOnConnectFailure": false,
  56. "RetryOn": ["reset", "unavailable"],
  57. "RetryOnStatusCodes": [500, 502, 503],
  58. "RequestHeaders": {
  59. "Set": {
  60. "X-Web-Version" : "<text-string>"
  61. }
  62. },
  63. "ResponseHeaders": {
  64. "Set": {
  65. "X-Web-Version" : "<text-string>"
  66. }
  67. }
  68. }
  69. }
  70. ]
  71. }
  1. apiVersion: consul.hashicorp.com/v1alpha1 # required
  2. kind: ServiceRouter # required
  3. metadata:
  4. name: <serviceName>
  5. namespace: <namespace>
  6. spec:
  7. routes:
  8. - match:
  9. http:
  10. pathExact: <exact/http/path> ## cannot specify with pathPrefix or pathRegex
  11. http:
  12. pathPrefix: <path/prefix> ## cannot specify with pathExact or pathRegex
  13. http:
  14. pathRegex: <regex/path> ## cannot specify with pathPrefix or pathExact
  15. http:
  16. methods: [GET, POST, PUT]
  17. http:
  18. header: ## do not specify present, exact, prefix, suffix, and regex in a single header
  19. - name: <header-to-match> ## required when specifying spec.routes.match.http.header
  20. present: false
  21. exact: <exact-match-name>
  22. prefix: <prefix-to-match>
  23. suffix: <suffix-to-match>
  24. regex: <regex-to-match>
  25. invert: false
  26. http:
  27. queryParam: ## do not specify present, exact, and regex in a single queryParam
  28. - name: <text-to-match-in-query-parameter> ## required when specifying spec.routes.match.http.header
  29. present: false
  30. exact: <exact-text-to-match-in-query-parameter>
  31. regex: <regex-to-match-in-query-paramater>
  32. destination:
  33. service: <service-name-at-destination>
  34. serviceSubset: <service-subset-at-destination>
  35. namespace: <namespace-at-destination>
  36. partition: <partition-at-destination>
  37. prefixRewrite: <new-prefix-after-routing> ## required specifying either spec.routes.match.http.pathPrefix or spec.routes.match.http.pathExact
  38. requestTimeout: 0
  39. idleTimeout: 0
  40. numRetries: 1
  41. retryOnConnectFailure: false
  42. retryOn: ['reset']
  43. retryOnStatusCodes: [500, 502, 503]
  44. requestHeaders:
  45. set:
  46. x-Web-Version: <text-string>
  47. responseHeaders:
  48. set:
  49. x-Web-Version: <text-string>

Specification

This section provides details about the fields you can configure in the service router configuration entry.

Kind

Specifies the type of configuration entry to implement.

Values

  • Default: none
  • This field is required.
  • Data type: String value that must be set to service-router.

Name

Specifies a name for the configuration entry. The name is metadata that you can use to reference the configuration entry when performing Consul operations with the consul config command.

Values

  • Default: None
  • Data type: String

Namespace EnterpriseService router - 图8Enterprise

Specifies the namespace to apply the configuration entry to. Refer to Namespaces for additional information about Consul namespaces.

Values

  • Default: None
  • Data type: String

Partition EnterpriseService router - 图9Enterprise

Specifies the admin partition to apply the configuration entry to. Refer to Admin partitions for additional information.

Values

  • Default: default
  • Data type: String

Meta

Specifies key-value pairs to add to the KV store.

Values

  • Default: None
  • Data type: Map of one or more key-value pairs
    • : String
    • : String or integer

Routes

Defines the possible routes for L7 requests. Consul evaluates traffic against the list of routes in their order of appearance in the configuration entry. When multiple routes satisfy the request, Consul uses the first route that matches. Traffic that fails to match any of the provided routes is routed to the default service.

Values

  • Default: None

  • Data type: List that can contain the following parameters:

  • Match

  • Destination

Routes[].Match

Describes a set of criteria that Consul compares incoming L7 traffic with. If empty or omitted, it acts as a catch-all.

Values

Routes[].Match{}.HTTP

Specifies a set of HTTP criteria used to evaluate incoming L7 traffic for matches.

When matching on the HTTP request path, you can only match on one path at a time. Do not configure PathExact, PathPrefix, and PathRegex in a single HTTP map.

Values

Routes[].Match{}.HTTP{}.PathExact

Specifies the exact path to match on the HTTP request path. When using this field, do not configure PathPrefix or PathRegex in the same HTTP map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.PathPrefix

Specifies the path prefix to match on the HTTP request path. When using this field, do not configure PathExact or PathRegex in the same HTTP map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.PathRegex

Specifies a regular expression to match on the HTTP request path. When using this field, do not configure PathExact or PathPrefix in the same HTTP map. The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Methods

Specifies HTTP methods that the match applies to. If not specified, the request matches on all HTTP methods. If provided, the name must be a valid method formatted as a string.

String values must be a valid HTTP request method.

Values

Routes[].Match{}.HTTP{}.Header

Specifies information in the HTTP request header to match with. When more than one field is configured, all criteria must match for the service routing to apply.

When using this field, do not configure Present, Exact, Prefix, Suffix, and Regex in the same HTTP map. You can use only one of these fields at a time when configuring match criteria for HTTP headers, as they are mutually exclusive with one another.

Values

Routes[].Match{}.HTTP{}.Header[].Name

Specifies the name of the HTTP header to match. This field is required when using Routes[].Match{}.HTTP{}.Header.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Header[].Present

Specifies that a request matches when the value in the Name field is present anywhere in the HTTP header. When using this field, do not configure Exact, Prefix, Suffix, or Regex in the same HTTP map.

Values

  • Default: false
  • Data type: Boolean

Routes[].Match{}.HTTP{}.Header[].Exact

Specifies that a request matches when the header with the given name is this exact value. When using this field, do not configure Present, Prefix, Suffix, or Regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Header[].Prefix

Specifies that a request matches when the header with the given name has this prefix. When using this field, do not configure Present, Exact, Suffix, or Regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Header[].Suffix

Specifies that a request matches when the header with the given name has this suffix. When using this field, do not configure Present, Exact, Prefix, or Regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Header[].Regex

Specifies that a request matches when the header with the given name matches this regular expression. When using this field, do not configure Present, Exact, Prefix, or Suffix in the same HTTP map . The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.Header[].Invert

Specifies that the logic for the HTTP header match should be inverted. Requests with matching criteria are not routed.

Values

  • Default: false
  • Data type: Boolean

Routes[].Match{}.HTTP{}.QueryParam

Specifies information to match to on HTTP query parameters. When more than one field is configured, all criteria must match for the service routing to apply.

When using this field, do not configure Present, Exact, and Regex in a single map. You can use only one of these fields at a time when configuring match criteria for HTTP query parameters, as they are mutually exclusive with one another.

Values

  • Default: None

  • Data type: List of maps. Each map can contain one or more of the following parameters:

Routes[].Match{}.HTTP{}.QueryParam[].Name

Specifies the name of the HTTP query parameter to match. This value is required when using Routes[].Match{}.HTTP{}.QueryParam.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.QueryParam[].Present

Specifies that a request matches when the value in the Name field is present anywhere in the HTTP query parameter. When using this field, do not configure Exact or Regex in the same map.

Values

  • Default: false
  • Data type: Boolean

Routes[].Match{}.HTTP{}.QueryParam[].Exact

Specifies that a request matches when the query parameter with the given name is this exact value. When using this field, do not configure Present or Regex in the same map.

Values

  • Default: None
  • Data type: String

Routes[].Match{}.HTTP{}.QueryParam[].Regex

Specifies that a request matches when the query parameter with the given name matches this regular expression. When using this field, do not configure Present or Exact in the same map. The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

Routes[].Destination

Specifies the target service to route matching requests to, as well as behavior for the request to follow when routed.

Values

Routes[].Destination{}.Service

Specifies the name of the service to resolve. If this parameter is not specified, the default service name is inherited from the configuration entry’s Name field.

Values

  • Default: None
  • Data type: String

Routes[].Destination{}.ServiceSubset

Specifies a named subset of the given service to resolve instead of the one defined as that service’s DefaultSubset in the service resolver configuration entry. If this parameter is not specified, the default subset is used.

Values

  • Default: None
  • Data type: String

Routes[].Destination{}.Namespace

Specifies the Consul namespace to resolve the service from instead of the current namespace. If this parameter is not specified, the current namespace is used.

Values

  • Default: None
  • Data type: String

Routes[].Destination{}.Partition

Specifies the Consul admin partition to resolve the service from instead of the current partition. If this parameter is not specified, the current partition is used.

Values

  • Default: None
  • Data type: String

Routes[].Destination{}.PrefixRewrite

Specifies rewrites to the HTTP request path before proxying it to its final destination. This field requires that either Routes[].Match{}.HTTP{}.PathPrefix or Routes[].Match{}.HTTP{}.PathExact be configured on this route.

Values

  • Default: None
  • Data type: String

Routes[].Destination{}.RequestTimeout

Specifies the total amount of time permitted for the entire downstream request to be processed, including retry attempts.

Values

  • Default: 0
  • Data type: Integer

Routes[].Destination{}.IdleTimeout

Specifies the total amount of time permitted for the request stream to be idle.

Values

  • Default: 0
  • Data type: Integer

Routes[].Destination{}.NumRetries

Specifies the number of times to retry the request when a retry condition occurs. Configure this field and other retry fields in Destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations. You cannot set the value to 0. To disable retries, unset all other retry settings: RetryOnConnectFailure, RetryOn, RetryOnStatusCodes.

Values

  • Default: 1
  • Data type: Integer

Routes[].Destination{}.RetryOnConnectFailure

Specifies that connection failure errors that trigger a retry request. Configure this field and other retry fields in Destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations.

Values

  • Default: false
  • Data type: Boolean

Routes[].Destination{}.RetryOn[]

Specifies a list of conditions for Consul to retry requests based on the response from an upstream service. Configure this field and other retry fields in the Destination object to configure the logic for retry attempts. For examples, refer to the retry logic example configurations.

The following retry conditions are supported:

ConditionsDescription
5xxConsul retries the request when an upstream responds with any 5xx error code or does not respond at all.
gateway-errorConsul retries the request when the upstream responds with a 502, 503, or 504 error.
resetConsul retries the request when the upstream does not respond at all.
connect-failureConsul retries the request when the connection to the upstream fails.
envoy-ratelimitedConsul retries the request when the header x-envoy-ratelimited is present.
retriable-4xxConsul retries the request when the upstream responds with a retriable 4xx code.
refused-streamConsul retries the request when the upstream resets the stream with a REFUSED_STREAM error code.
cancelledConsul retries the request when the gRPC status code in the response headers is cancelled.
deadline-exceededConsul retries the request when the gRPC status code in the response headers is deadline-exceeded.
internalConsul retries the request when the gRPC status code in the response headers is internal.
resource-exhaustedConsul retries the request when the gRPC status code in the response headers is resource-exhausted.
unavailableConsul retries the request when the gRPC status code in the response headers is unavailable.

Values

Routes[].Destination{}.RetryOnStatusCodes

Specifies a list of integers for HTTP response status codes that trigger a retry request. Configure this field and other retry fields in Destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations

Values

  • Default: None
  • Data type: List of integers

Routes[].Destination{}.RequestHeaders

Specifies a set of HTTP-specific header modification rules applied to requests routed with the service router. You cannot configure request headers if the listener protocol is set to tcp.

Values

  • Default: None
  • Values: Object containing one or more fields that define header modification rules:
    • Add: Map of one or more key-value pairs.
    • Set: Map of one or more key-value pairs.
    • Remove: Map of one or more key-value pairs.

The following table describes how to configure values for request headers:

RuleDescriptionType
AddDefines a set of key-value pairs to add to the header. Use header names as the keys. Header names are not case-sensitive. If header values with the same name already exist, the value is appended and Consul applies both headers. You can use variable placeholders.map of strings
SetDefines a set of key-value pairs to add to the request header or to replace existing header values with. Use header names as the keys. Header names are not case-sensitive. If header values with the same names already exist, Consul replaces the header values. You can use variable placeholders.map of strings
RemoveDefines a list of headers to remove. Consul removes only headers containing exact matches. Header names are not case-sensitive.list of strings

Use variable placeholders

For Add and Set, if the service is configured to use Envoy as the proxy, the value may contain variables to interpolate dynamic metadata into the value. For example, using the variable %DOWNSTREAM_REMOTE_ADDRESS% in your configuration entry allows you to pass a value that is generated when the routing occurs.

Routes[].Destination{}.ResponseHeaders

Specifies a set of HTTP-specific header modification rules applied to responses routed with the service router. You cannot configure request headers if the listener protocol is set to tcp.

Values

  • Default: None
  • Values: Object containing one or more fields that define header modification rules:
    • Add: Map of one or more string key-value pairs.
    • Set: Map of one or more string key-value pairs.
    • Remove: Map of one or more string key-value pairs.

The following table describes how to configure values for response headers:

RuleDescriptionType
AddDefines a set of key-value pairs to add to the header. Use header names as the keys. Header names are not case-sensitive. If header values with the same name already exist, the value is appended and Consul applies both headers. You can use variable placeholders.map of strings
SetDefines a set of key-value pairs to add to the request header or to replace existing header values with. Use header names as the keys. Header names are not case-sensitive. If header values with the same names already exist, Consul replaces the header values. You can use variable placeholders.map of strings
RemoveDefines a list of headers to remove. Consul removes only headers containing exact matches. Header names are not case-sensitive.list of strings

Use variable placeholders

For Add and Set, if the service is configured to use Envoy as the proxy, the value may contain variables to interpolate dynamic metadata into the value. For example, using the variable %DOWNSTREAM_REMOTE_ADDRESS% in your configuration entry allows you to pass a value that is generated when the routing occurs.

apiVersion

Kubernetes-only parameter that specifies the version of the Consul API that the configuration entry maps to Kubernetes configurations. The value must be consul.hashicorp.com/v1alpha1.

kind

Specifies the type of configuration entry to implement.

Values

  • Default: None
  • This field is required.
  • Data type: String value that must be set to ServiceRouter.

metadata.name

Specifies a name for the configuration entry. The name is metadata that you can use to reference the configuration entry when performing Consul operations, such as applying a configuration entry to a specific cluster.

Values

  • Default: None
  • Data type: String

metadata.namespace EnterpriseService router - 图10Enterprise

Specifies the Consul namespace to use for resolving the service. You can map Consul namespaces to Kubernetes Namespaces in different ways. Refer to Custom Resource Definitions (CRDs) for Consul on Kubernetes for additional information.

Values

  • Default: None
  • Data type: String

spec

Map that contains the details about the ServiceRouter configuration entry. The apiVersion, kind, and metadata fields are siblings of the spec field. All other configurations are children.

Values

  • Default: None
  • This field is required.
  • Data type: Object containing spec.routes configuration

spec.meta

Specifies key-value pairs to add to the KV store.

Values

  • Default: none
  • Data type: Map of one or more key-value pairs
    • keys: String
    • values: String, integer, or float

spec.routes

Defines the possible routes for L7 requests. Consul evaluates traffic against the list of routes in their order of appearance in the configuration entry. When multiple routes satisfy the request, Consul uses the first route that matches. Traffic that fails to match any of the provided routes is routed to the default service.

Values

  • Default: None

  • Data type: List that can contain the following parameters:

  • match

  • destination

spec.routes[].match

Describes a set of criteria that Consul compares incoming L7 traffic with. If empty or omitted, it acts as a catch-all.

Values

spec.routes[].match.http

Specifies a set of HTTP criteria used to evaluate incoming L7 traffic for matches.

When matching on the HTTP request path, you can only match on one path at a time. Do not configure pathExact, pathPrefix, and pathRegex in a single HTTP map.

Values

spec.routes[].match.http.pathExact

Specifies the exact path to match on the HTTP request path. When using this field, do not configure pathPrefix or pathRegex in the same HTTP map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.pathPrefix

Specifies the path prefix to match on the HTTP request path. When using this field, do not configure pathExact or pathRegex in the same HTTP map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.pathRegex

Specifies a regular expression to match on the HTTP request path. When using this field, do not configure pathExact or pathPrefix in the same HTTP map. The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.methods

Specifies HTTP methods that the match applies to. If not specified, the request matches on all HTTP methods. If provided, the name must be a valid method formatted as a string.

String values must be a valid HTTP request method.

Values

spec.routes[].match.http.header

Specifies information to match to in the HTTP request header. When more than one field is configured, all criteria must match for the service routing to apply.

When using this field, do not configure present, exact, prefix, suffix, and regex in the same HTTP map. You can use only one of these fields at a time when configuring match criteria for HTTP headers, as they are mutually exclusive with one another.

Values

spec.routes[].match.http.header.name

Specifies the name of the HTTP header to match. This field is required when using spec.routes[].match.http.header.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.header.present

Specifies that a request matches when the value in the Name field is present anywhere in the HTTP header. When using this field, do not configure exact, prefix, suffix, or regex in the same HTTP map.

Values

  • Default: false
  • Data type: Boolean

spec.routes[].match.http.header.exact

Specifies that a request matches when the header with the given name is this exact value. When using this field, do not configure present, prefix, suffix, or regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.header.prefix

Specifies that a request matches when the header with the given name has this prefix. When using this field, do not configure present, exact, suffix, or regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.header.suffix

Specifies that a request matches when the header with the given name has this suffix. When using this field, do not configure present, exact, prefix, or regex in the same HTTP map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.header.regex

Specifies that a request matches when the header with the given name matches this regular expression. When using this field, do not configure present, exact, prefix, or suffix in the same HTTP map . The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.header.invert

Specifies that the logic for the HTTP header match should be inverted. Requests with matching criteria are not routed.

Values

  • Default: false
  • Data type: Boolean

spec.routes[].match.http.queryParam

Specifies information to match to on HTTP query parameters. When more than one field is configured, all criteria must match for the service routing to apply.

When using this field, do not configure present, exact, and regex in a single map. You can use only one of these fields at a time when configuring match criteria for HTTP query parameters, as they are mutually exclusive with one another.

Values

  • Default: None

  • Data type: List of maps. Each map can contain one or more of the following parameters:

spec.routes[].match.http.queryParam[].name

Specifies the name of the HTTP query parameter to match. This value is required when using spec.routes[].match.http.queryParam.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.queryParam[].present

Specifies that a request matches when the value in the name field is present anywhere in the HTTP query parameter. When using this field, do not configure exact or regex in the same map.

Values

  • Default: false
  • Data type: Boolean

spec.routes[].match.http.queryParam[].exact

Specifies that a request matches when the query parameter with the given name is this exact value. When using this field, do not configure present or regex in the same map.

Values

  • Default: None
  • Data type: String

spec.routes[].match.http.queryParam[].regex

Specifies that a request matches when the query parameter with the given name matches this regular expression. When using this field, do not configure present or exact in the same map. The syntax for the regular expression field is proxy-specific. When using Envoy, refer to the documentation for Envoy v1.11.2 or newer or the documentation for Envoy v1.11.1 or older, depending on the version of Envoy you use.

Values

  • Default: None
  • Data type: String

spec.routes[].destination

Specifies the target service to route matching requests to, as well as behavior for the request to follow when routed.

Values

spec.routes[].destination.service

Specifies the name of the service to resolve. If this parameter is not specified, the default service name is inherited from the configuration entry’s metadata.name field.

Values

  • Default: None
  • Data type: String

spec.routes[].destination.serviceSubset

Specifies a named subset of the given service to resolve instead of the one defined as that service’s defaultSubset in the service resolver configuration entry. If this parameter is not specified, the default subset is used.

Values

  • Default: None
  • Data type: String

spec.routes[].destination.namespace

Specifies the Consul namespace to resolve the service from instead of the current namespace. If this parameter is not specified, the current namespace is used.

Values

  • Default: None
  • Data type: String

spec.routes[].destination.partition

Specifies the Consul admin partition to resolve the service from instead of the current partition. If this parameter is not specified, the current partition is used.

Values

  • Default: None
  • Data type: String

spec.routes[].destination.prefixRewrite

Specifies rewrites to the HTTP request path before proxying it to its final destination. This field requires that either spec.routes[].match.http.pathPrefix or spec.routes[].match.http.pathExact be configured on this route.

Values

  • Default: None
  • Data type: String

spec.routes[].destination.requestTimeout

Specifies the total amount of time permitted for the entire downstream request to be processed, including retry attempts.

Values

  • Default: 0
  • Data type: Integer

spec.routes[].destination.idleTimeout

Specifies the total amount of time permitted for the request stream to be idle.

Values

  • Default: 0
  • Data type: Integer

spec.routes[].destination.numRetries

Specifies the number of times to retry the request when a retry condition occurs. Configure this field and other retry fields in spec.routes.destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations. You cannot set the value to 0. To disable retries, unset all other retry settings: retryOnConnectFailure, retryOn, retryOnStatusCodes.

Values

  • Default: 1
  • Data type: Integer

spec.routes[].destination.retryOnConnectFailure

Specifies that connection failure errors that trigger a retry request. Configure this field and other retry fields in spec.routes[].destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations.

Values

  • Default: false
  • Data type: Boolean

spec.routes[].destination.retryOn

Specifies a list of conditions for Consul to retry requests based on the response from an upstream service. Configure this field and other retry fields in the spec.routes[].destination object to configure the logic for retry attempts. For examples, refer to the retry logic example configurations.

The following retry conditions are supported:

ConditionsDescription
5xxConsul retries the request when an upstream responds with any 5xx error code or does not respond at all.
gateway-errorConsul retries the request when the upstream responds with a 502, 503, or 504 error.
resetConsul retries the request when the upstream does not respond at all.
connect-failureConsul retries the request when the connection to the upstream fails.
envoy-ratelimitedConsul retries the request when the header x-envoy-ratelimited is present.
retriable-4xxConsul retries the request when the upstream responds with a retriable 4xx code.
refused-streamConsul retries the request when the upstream resets the stream with a REFUSED_STREAM error code.
cancelledConsul retries the request when the gRPC status code in the response headers is cancelled.
deadline-exceededConsul retries the request when the gRPC status code in the response headers is deadline-exceeded.
internalConsul retries the request when the gRPC status code in the response headers is internal.
resource-exhaustedConsul retries the request when the gRPC status code in the response headers is resource-exhausted.
unavailableConsul retries the request when the gRPC status code in the response headers is unavailable.

Values

spec.routes[].destination.retryOnStatusCodes

Specifies a list of integers for HTTP response status codes that trigger a retry request. Configure this field and other retry fields in spec.routes[].destination to configure the logic for retry attempts. For examples, refer to the retry logic example configurations

Values

  • Default: None
  • Data type: List of integers

spec.routes[].destination.requestHeaders

Specifies a set of HTTP-specific header modification rules applied to requests routed with the service router. You cannot configure request headers if the listener protocol is set to tcp.

Values

  • Default: None
  • Values: Object containing one or more fields that define header modification rules:
    • add: Map of one or more key-value pairs.
    • set: Map of one or more key-value pairs.
    • remove: Map of one or more key-value pairs.

The following table describes how to configure values for request headers:

RuleDescriptionType
addDefines a set of key-value pairs to add to the header. Use header names as the keys. Header names are not case-sensitive. If header values with the same name already exist, the value is appended and Consul applies both headers. You can use variable placeholders.map of strings
setDefines a set of key-value pairs to add to the request header or to replace existing header values with. Use header names as the keys. Header names are not case-sensitive. If header values with the same names already exist, Consul replaces the header values. You can use variable placeholders.map of strings
removeDefines a list of headers to remove. Consul removes only headers containing exact matches. Header names are not case-sensitive.list of strings

Use variable placeholders

For add and set, if the service is configured to use Envoy as the proxy, the value may contain variables to interpolate dynamic metadata into the value. For example, using the variable %DOWNSTREAM_REMOTE_ADDRESS% in your configuration entry allows you to pass a value that is generated when the routing occurs.

spec.routes[].destination.responseHeaders

Specifies a set of HTTP-specific header modification rules applied to responses routed with the service router. You cannot configure request headers if the listener protocol is set to tcp.

Values

  • Default: None
  • Values: Object containing one or more fields that define header modification rules:
    • add: Map of one or more string key-value pairs.
    • set: Map of one or more string key-value pairs.
    • remove: Map of one or more string key-value pairs.

The following table describes how to configure values for response headers:

RuleDescriptionType
addDefines a set of key-value pairs to add to the header. Use header names as the keys. Header names are not case-sensitive. If header values with the same name already exist, the value is appended and Consul applies both headers. You can use variable placeholders.map of strings
setDefines a set of key-value pairs to add to the request header or to replace existing header values with. Use header names as the keys. Header names are not case-sensitive. If header values with the same names already exist, Consul replaces the header values. You can use variable placeholders.map of strings
removeDefines a list of headers to remove. Consul removes only headers containing exact matches. Header names are not case-sensitive.list of strings

Use variable placeholders

For add and set, if the service is configured to use Envoy as the proxy, the value may contain variables to interpolate dynamic metadata into the value. For example, using the variable %DOWNSTREAM_REMOTE_ADDRESS% in your configuration entry allows you to pass a value that is generated when the routing occurs.

Examples

The following examples demonstrate common service router configuration patterns for specific use cases.

Path prefix matching

The following example routes HTTP requests for the web service to a service named admin when they have /admin at the start of their path.

  1. Kind = "service-router"
  2. Name = "web"
  3. Routes = [
  4. {
  5. Match {
  6. HTTP {
  7. PathPrefix = "/admin"
  8. }
  9. }
  10. Destination {
  11. Service = "admin"
  12. }
  13. },
  14. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceRouter
  3. metadata:
  4. name: web
  5. spec:
  6. routes:
  7. - match:
  8. http:
  9. pathPrefix: /admin
  10. destination:
  11. service: admin
  1. {
  2. "Kind": "service-router",
  3. "Name": "web",
  4. "Routes": [
  5. {
  6. "Match": {
  7. "HTTP": {
  8. "PathPrefix": "/admin"
  9. }
  10. },
  11. "Destination": {
  12. "Service": "admin"
  13. }
  14. }
  15. ]
  16. }

Match a header and query parameter

The following example routes HTTP traffic to the web service to a subset of canary instances when the requests have x-debug in either the header or the URL parameter.

  1. Kind = "service-router"
  2. Name = "web"
  3. Routes = [
  4. {
  5. Match {
  6. HTTP {
  7. Header = [
  8. {
  9. Name = "x-debug"
  10. Exact = "1"
  11. },
  12. ]
  13. }
  14. }
  15. Destination {
  16. Service = "web"
  17. ServiceSubset = "canary"
  18. }
  19. },
  20. {
  21. Match {
  22. HTTP {
  23. QueryParam = [
  24. {
  25. Name = "x-debug"
  26. Exact = "1"
  27. },
  28. ]
  29. }
  30. }
  31. Destination {
  32. Service = "web"
  33. ServiceSubset = "canary"
  34. }
  35. },
  36. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceRouter
  3. metadata:
  4. name: web
  5. spec:
  6. routes:
  7. - match:
  8. http:
  9. header:
  10. - name: x-debug
  11. exact: '1'
  12. destination:
  13. service: web
  14. serviceSubset: canary
  15. - match:
  16. http:
  17. queryParam:
  18. - name: x-debug
  19. exact: '1'
  20. destination:
  21. service: web
  22. serviceSubset: canary
  1. {
  2. "Kind": "service-router",
  3. "Name": "web",
  4. "Routes": [
  5. {
  6. "Match": {
  7. "HTTP": {
  8. "Header": [
  9. {
  10. "Name": "x-debug",
  11. "Exact": "1"
  12. }
  13. ]
  14. }
  15. },
  16. "Destination": {
  17. "Service": "web",
  18. "ServiceSubset": "canary"
  19. }
  20. },
  21. {
  22. "Match": {
  23. "HTTP": {
  24. "QueryParam": [
  25. {
  26. "Name": "x-debug",
  27. "Exact": "1"
  28. }
  29. ]
  30. }
  31. },
  32. "Destination": {
  33. "Service": "web",
  34. "ServiceSubset": "canary"
  35. }
  36. }
  37. ]
  38. }

gRPC routing

The following example routes gRPC requests to the invoice-generatorservice when they come from an HTTP path that is exact match for mycompany.BillingService/GenerateInvoice. Because gRPC method calls use HTTP/2, you can use an HTTP path match rule to re-route traffic.

  1. Kind = "service-router"
  2. Name = "billing"
  3. Routes = [
  4. {
  5. Match {
  6. HTTP {
  7. PathExact = "/mycompany.BillingService/GenerateInvoice"
  8. }
  9. }
  10. Destination {
  11. Service = "invoice-generator"
  12. }
  13. },
  14. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceRouter
  3. metadata:
  4. name: billing
  5. spec:
  6. routes:
  7. - match:
  8. http:
  9. pathExact: /mycompany.BillingService/GenerateInvoice
  10. destination:
  11. service: invoice-generator
  1. {
  2. "Kind": "service-router",
  3. "Name": "billing",
  4. "Routes": [
  5. {
  6. "Match": {
  7. "HTTP": {
  8. "PathExact": "/mycompany.BillingService/GenerateInvoice"
  9. }
  10. },
  11. "Destination": {
  12. "Service": "invoice-generator"
  13. }
  14. }
  15. ]
  16. }

Retry logic

The following example configures Consul so that when a request for the orders service passes through the service mesh, Consul routes the traffic to the products service or the procurement service based on the HTTP path that originated the request:

  • If it originates from the /coffees path, the request routes to the products service, times out after 15 seconds, and attempts 5 retries.
  • If it originates from the /orders path, the request routes to the procurement services, times out after 10 seconds, and attempts 3 retries.
  1. Kind = "service-router"
  2. Name = "orders"
  3. Routes = [
  4. {
  5. Match {
  6. HTTP {
  7. PathPrefix = "/coffees"
  8. }
  9. }
  10. Destination {
  11. Service = "products"
  12. RequestTimeout = "15s"
  13. NumRetries = 5
  14. RetryOnConnectFailure = true
  15. RetryOn = ["reset"]
  16. }
  17. },
  18. {
  19. Match {
  20. HTTP {
  21. PathPrefix = "/orders"
  22. }
  23. }
  24. Destination {
  25. Service = "procurement"
  26. RequestTimeout = "10s"
  27. NumRetries = 3
  28. RetryOnConnectFailure = true
  29. RetryOn = ["reset"]
  30. }
  31. }
  32. ]
  1. apiVersion: consul.hashicorp.com/v1alpha1
  2. kind: ServiceRouter
  3. metadata:
  4. name: orders
  5. spec:
  6. routes:
  7. - match:
  8. http:
  9. pathExact: /coffees
  10. destination:
  11. service: products
  12. requestTimeout: 15s
  13. numRetries: 5
  14. retryOnConnectFailure: true
  15. retryOn: ['reset']
  16. - match:
  17. http:
  18. pathExact: /orders
  19. destination:
  20. service: procurement
  21. requestTimeout: 10s
  22. numRetries: 3
  23. retryOnConnectFailure: true
  24. retryOn: ['reset']
  1. {
  2. "Kind": "service-router",
  3. "Name": "orders",
  4. "Routes": [
  5. {
  6. "Match": {
  7. "HTTP": {
  8. "PathPrefix": "/coffees"
  9. }
  10. },
  11. "Destination": {
  12. "NumRetries": 5,
  13. "RequestTimeout": "15s",
  14. "RetryOnConnectFailure": true,
  15. "RetryOn": ["reset"],
  16. "Service": "procurement"
  17. }
  18. },
  19. {
  20. "Match": {
  21. "HTTP": {
  22. "PathPrefix": "/orders"
  23. }
  24. },
  25. "Destination": {
  26. "NumRetries": 3,
  27. "RequestTimeout": "10s",
  28. "RetryOnConnectFailure": true,
  29. "RetryOn": ["reset"],
  30. "Service": "procurement"
  31. }
  32. }
  33. ]
  34. }