Route Resource Configuration

This topic describes how to create and configure Route resources. Routes are independent configuration objects that are associated with specific listeners.

Create a Route

Declare a route with either kind: HTTPRoute or kind: TCPRoute and configure the route parameters in the spec block. Refer to the Kubernetes Gateway API documentation for each object type for details:

The following example creates a route named example-route associated with a listener defined in example-gateway.

Routes - 图1

routes.yaml

  1. apiVersion: gateway.networking.k8s.io/v1beta1
  2. kind: HTTPRoute
  3. metadata:
  4. name: example-route
  5. spec:
  6. parentRefs:
  7. - name: example-gateway
  8. rules:
  9. - backendRefs:
  10. - kind: Service
  11. name: echo
  12. port: 8080

Configuration model

The following outline shows how to format the configurations for the Route object. The top-level spec field is the root for all configurations. Click on a property name to view details about the configuration.

Specification

This topic provides details about the configuration parameters.

parentRefs

This field contains the list of Gateways that the route should attach to. If not set, the route will not attach to a Gateway. The following table describes the objects you can configure in the parentRefs block:

ParameterDescriptionTypeRequired
groupSpecifies the Kubernetes API group of the Gateway to attach to. You can specify the following values:
  • gateway.networking.k8s.io
. Defaults to gateway.networking.k8s.io.
StringOptional
kindSpecifies the Kubernetes kind of the Gateway to attach to. you can specify the following values:
  • Gateway
. Defaults to Gateway.
StringOptional
nameSpecifies the name of the Gateway the route is attached to.StringRequired
namespaceSpecifies the Kubernetes namespace containing the Gateway to attach to. If the Gateway is in a different Kubernetes namespace than the Route, then you must specify a value. Defaults to the Route namespace.StringOptional
sectionNameSpecifies the name of a specific listener on the Gateway to attach to. The Route attempts to attach to all listeners on the Gateway.StringRequired

rules

The rules field contains a list of objects that define behaviors for network traffic that goes through the route. The rule configuration contains the following objects:

  • backendRefs: Specifies which backend services the Route references when processing traffic.
  • filters: Specifies which operations Consul API Gateway performs when traffic goes through the Route.
  • matches: Determines which requests Consul API Gateway processes.

Rules are optional.

rules.backendRefs

This field specifies backend services that the Route references. The following table describes the parameters for backendRefs:

ParameterDescriptionTypeRequired
groupSpecifies the Kubernetes API Group of the referenced backend. You can specify the following values:
  • “”: Specifies the core Kubernetes API group. This value must be used when kind is set to Service. This is the default value if unspecified.
  • api-gateway.consul.hashicorp.com: This value must be used when kind is set to MeshService.
StringOptional
kindSpecifies the Kubernetes Kind of the referenced backend. You can specify the following values:
  • Service (default): Indicates that the backendRef references a Service in the Kubernetes cluster.
  • MeshService: Indicates that the backendRef references a service in the Consul mesh. Refer to the MeshService documentation for additional information.
StringOptional
nameSpecifies the name of the Kubernetes Service or Consul mesh service resource.StringRequired
namespaceSpecifies the Kubernetes namespace containing the Kubernetes Service or Consul mesh service resource. You must specify a value if the Service or Consul mesh service is defined in a different namespace from the Route. Defaults to the namespace of the Route.
To create a route for a backendRef in a different namespace, you must also create a ReferenceGrant. Refer to the example route configured to reference across namespaces.
StringOptional
portSpecifies the port number for accessing the Kubernetes or Consul service.IntegerRequired
weightSpecifies the proportion of requests sent to the backend. Computed as weight divided by the sum of all weights in this backendRefs list. Defaults to 1. A value of 0 indicates that no requests should be sent to the backend.IntegerOptional

Example cross-namespace backendRef

The following example creates a route named example-route in namespace gateway-namespace. This route has a backendRef in namespace service-namespace. Traffic is allowed because the ReferenceGrant, named reference-grant in namespace service-namespace, allows traffic from HTTPRoutes in gateway-namespace to Services in service-namespace.

Routes - 图2

route_with_referencegrant.yaml

  1. apiVersion: gateway.networking.k8s.io/v1beta1
  2. kind: HTTPRoute
  3. metadata:
  4. name: example-route
  5. namespace: gateway-namespace
  6. spec:
  7. parentRefs:
  8. - name: example-gateway
  9. rules:
  10. - backendRefs:
  11. - kind: Service
  12. name: echo
  13. namespace: service-namespace
  14. port: 8080
  15. ---
  16. apiVersion: gateway.networking.k8s.io/v1alpha2
  17. kind: ReferenceGrant
  18. metadata:
  19. name: reference-grant
  20. namespace: service-namespace
  21. spec:
  22. from:
  23. - group: gateway.networking.k8s.io
  24. kind: HTTPRoute
  25. namespace: gateway-namespace
  26. to:
  27. - group: ""
  28. kind: Service
  29. name: echo

rules.filters

The filters block defines steps for processing requests. You can configure filters to modify the properties of matching incoming requests and enable Consul API Gateway features, such as rewriting path prefixes (refer to Reroute HTTP requests for additional information).

  • Type: Array of objects
  • Required: Optional

rules.filters.type

Specifies the type of filter you want to apply to the route. The parameter is optional and takes a string value.

You can specify the following values:

rules.filters.requestHeaderModifier

Defines operations to perform on matching request headers when rules.filters.type is configured to RequestHeaderModifier. This field contains the following configuration objects:

ParameterDescriptionTypeRequired
setConfigure this field to rewrite the HTTP request header. It specifies the name of an HTTP header to overwrite and the new value to set. Any existing values associated with the header name are overwritten. You can specify the following configurations:
  • name: Required string that specifies the name of the HTTP header to set.
  • value: Required string that specifies the value of the HTTP header to set.
List of objectsOptional
addConfigure this field to append the request header with a new value. It specifies the name of an HTTP header to append and the value(s) to add. You can specify the following configurations:
  • name: Required string that specifies the name of the HTTP header to append.
  • value: Required string that specifies the value of the HTTP header to add.
List of objectsOptional
removeConfigure this field to specify an array of header names to remove from the request header.Array of stringsOptional

rules.filters.urlRewrite

Specifies rules for rewriting the URL of incoming requests when rules.filters.type is configured to URLRewrite.

  • Type: Object
  • Required: Optional

rules.filters.urlRewrite.path

Specifies a list of objects that determine how Consul API Gateway rewrites URL paths (refer to Reroute HTTP requests for additional information).

The following table describes the parameters for path:

ParameterDescriptionTypeRequired
replacePrefixMatchSpecifies a value that replaces the path prefix for incoming HTTP requests. The operation only affects the path prefix. The rest of the path is unchanged.StringRequired
typeSpecifies the type of replacement to use for the URL path. You can specify the following values:
  • ReplacePrefixMatch: Replaces the the matched prefix of the URL path (default).
StringOptional

rules.matches

Specifies rules for matching incoming requests. You can apply filters to requests that match the defined rules. You can match incoming requests based on the following elements:

Each rule matches requests independently. As a result, a request matching any of the conditions is considered a match. You can configure several matching rules for each type to widen or narrow matches.

rules.matches.path

Specifies a list of objects that define matches based on URL path. The following table describes the parameters for the path field:

ParameterDescriptionTypeRequired
typeSpecifies the type of comparison to use for matching the path value. You can specify the following types.
  • Exact: Returns a match only when the entire path matches the value field (default).
  • PathPrefix: Returns a match when the path has the prefix defined in the value field.
  • RegularExpression: Returns a match when the path matches the regex defined in the value field.
StringRequired
valueSpecifies the value to match on. You can specify a specific string when type is Exact or PathPrefix. You can specify a regular expression if type is RegularExpression.StringRequired

rules.matches.headers

Specifies a list of objects that define matches based HTTP request headers. The following table describes the parameters for the headers field:

ParameterDescriptionTypeRequired
typeSpecifies the type of comparison to use for matching the header value. You can specify the following types.
  • Exact: Returns a match only when the entire header matches the value field (default).
  • RegularExpression: Returns a match when the header matches the regex defined in the value field.
StringRequired
nameSpecifies the name of the header to match on.StringRequired
valueSpecifies value to match on. You can specify a specific string or a regular expression.StringRequired

rules.matches.queryParams

Specifies a list of objects that define matches based query parameters. The following table describes the parameters for the queryParams field:

ParameterDescriptionTypeRequired
typeSpecifies the type of comparison to use for matching a query parameter value. You can specify the following types.
  • Exact: Returns a match only when the query parameter match the value field (default).
  • RegularExpression: Returns a match when the query parameter matches the regex defined in the value field.
StringRequired
nameSpecifies the name of the query parameter to match on.StringRequired
valueSpecifies value to match on. You can specify a specific string or a regular expression.StringRequired

rules.matches.method

Specifies a list of strings that define matches based on HTTP request method. You may specify the following values: