Gateway Routes
Gateway resources can specify rich rules to define how traffic is routed, and how additional capabilities are applied.
Getting Started
These examples use the bookinfo application, which is deployed and exposed as follows:
The bookinfo app consists of several services and in these examples we will call these services directly:
| Service | Name | Port | Example URL |
|---|---|---|---|
| productpage | bookinfo/productpage.bookinfo.svc.cluster.local | 9080 | / or /productpage |
| reviews | bookinfo/reviews.bookinfo.svc.cluster.local | 9080 | /reviews/0 |
| ratings | bookinfo/ratings.bookinfo.svc.cluster.local | 9080 | /ratings/0 |
| details | bookinfo/details.bookinfo.svc.cluster.local | 9080 | /details/0 |
A basic Gateway configuration that routes to each service would look like the following:
apiVersion: gateway.tsb.tetrate.io/v2
kind: Gateway
metadata:
organization: tetrate
tenant: default
workspace: bookinfo-ws
group: bookinfo-gwgroup
name: bookinfo-gateway
spec:
workloadSelector:
namespace: bookinfo
labels:
app: bookinfo-gw
http:
- name: bookinfo
port: 80
hostname: "bookinfo.tetrate.io"
routing:
rules:
- match:
- uri:
prefix: "/reviews"
route:
serviceDestination:
host: "bookinfo/reviews.bookinfo.svc.cluster.local"
port: 9080
- match:
- uri:
prefix: "/ratings"
route:
serviceDestination:
host: "bookinfo/ratings.bookinfo.svc.cluster.local"
port: 9080
- match:
- uri:
prefix: "/details"
route:
serviceDestination:
host: "bookinfo/details.bookinfo.svc.cluster.local"
port: 9080
- route:
serviceDestination:
host: "bookinfo/productpage.bookinfo.svc.cluster.local"
port: 9080
Extending the configuration
Individual routes can be extended with custom timeouts, retry policies and CORS policies. Timeouts and CORS policies can also be defined at a top-level:
apiVersion: gateway.tsb.tetrate.io/v2
kind: Gateway
metadata:
organization: tetrate
tenant: default
workspace: bookinfo-ws
group: bookinfo-gwgroup
name: bookinfo-gateway
spec:
workloadSelector:
namespace: bookinfo
labels:
app: bookinfo-gw
http:
- name: bookinfo
port: 80
hostname: "bookinfo.tetrate.io"
routing:
# default CORS policy
corsPolicy:
allowOrigin:
- "https://app.example.com"
allowMethods:
- GET
allowHeaders:
- "content-type"
- "authorization"
allowCredentials: true
rules:
- match:
- uri:
prefix: "/reviews"
# path-level CORS Policy and timeout for /reviews
corsPolicy:
allowOrigin:
- "https://app.example.com"
allowMethods:
- GET
- POST
- OPTIONS
allowHeaders:
- "content-type"
- "authorization"
allowCredentials: true
timeout: 2s
route:
serviceDestination:
host: "bookinfo/reviews.bookinfo.svc.cluster.local"
port: 9080
- match:
- uri:
prefix: "/ratings"
# path-level retry policy for /ratings
retryPolicy:
attempts: 3
perTryTimeout: 2s
retryOn: "5xx,gateway-error"
route:
serviceDestination:
host: "bookinfo/ratings.bookinfo.svc.cluster.local"
port: 9080
- match:
- uri:
prefix: "/details"
route:
serviceDestination:
host: "bookinfo/details.bookinfo.svc.cluster.local"
port: 9080
- route:
serviceDestination:
host: "bookinfo/productpage.bookinfo.svc.cluster.local"
port: 9080
From TSB 1.12.16, a route's timeout overrides the request timeout that a TrafficSetting configures for the destination, for that route only. Previously, the TrafficSetting timeout took precedence. Routes that don't set timeout keep the destination's request timeout.
Route-level retries
From TSB 1.12.16, you can set a retryPolicy on an individual route rule. It overrides the retry policy that a TrafficSetting configures for the destination, for that route only. Routes that don't set retryPolicy keep the destination's retry policy.
A retryPolicy has these settings:
attempts: the number of retries for a request. This setting is required. Set it to0to disable retries for the route.perTryTimeout: the timeout for each attempt, such as2s.retryOn: a comma-separated list of the conditions to retry on, such as5xx,gateway-error. See the Envoy retry policies for the conditions you can use.retryBackOff: thebaseIntervalandmaxIntervalbetween retries. By default, the base interval is 25ms and the maximum interval is 10 times the base interval.
You can only set retryPolicy on a rule that forwards traffic, not on a rule that redirects or returns a direct response.
The HTTP settings
The HTTP Settings form the anchor for configuring a service to listen for. Within the http settings, you can define:
- The internal
name, and aportandhostnameto listen for - Optional
tlssettings, where the Gateway should listen for and terminate HTTPS traffic authenticationandauthorizationsettings work together to define how requests are authenticated, and if the requesting identity is authorized to access the resourcerateLimitingsettings define rate-limits for requests, either local or globally, and grouped by user-defined criteriafailoverSettingssettings define how the gateway should select another target if the preferred or default target is not available
... and finally, but perhaps most importantly, routing settings use rules to define specific routes, upstreams and traffic management settings for certain paths.
The HTTP Route Rule settings
The HTTP Route Rule settings are used to define specific routing and traffic management rules for certain paths.
Within the rules, define:
- The
matchcondition, identifying whether this rule matches each incoming request - Statements to
modify,redirectordirectResponse(respond directly to the request) - The target destination (
route) for the matching request - Additional traffic management settings, such as
timeout,retryPolicyandcorsPolicy