I am trying to implement following routing strategy in my K8S cluster.
- route
foo/bar/*
requests tobar-service
- route
foo/*
requests (exceptfoo/bar/*
) requests tofoo-service
I am new to istio and this strategy was already implemented with nginx ingress controller using regex but could not find a way in istio gateway and virtual service.
3
Answers
Try with edit the virtulaservice
match:
prefix: /foo/bar/*
rewrite:
uri: /
route:
host: bar-service
port:
number: 80
match:
prefix: /foo/*
rewrite:
uri: /
route:
host: foo-service
port:
number: 80
This can be accomplished with a VirtualService and HTTP route rules. Let’s say you have App1:
And a second app, App2:
In the event that you want
app2
to serve all requests to/specific-route
, and all other requests toapp1
, you’d first have to create aGateway
:Now wire up a
VirtualService
to thisGateway
, which will handle the routing:If you look at
VirtualService.spec.http
you’ll find the list ofHTTPRoute
s.Here is where you define the ordered list of routes. The first one that is matched will be used, so you have to make sure the sub route is listed first.
Now if you do the following:
You’ll see the request goes to app2. But if you do a different route that doesn’t match that, it’ll fall back to app1:
Below virtual service can route requests to different foo, bar service respectively. if further change is required on request path change the regex pattern accordingly