In a microservices world, it is common to have nested URL routes going to various different services. Let’s take this example:
In this case, you want your HTTP requests for /hello/world/*
to go to App2. But all other requests to /hello/*
should go to App1. Thankfully, this routing scenario can be solved with Istio, specifically using HTTP route rules in a VirtualService
. Let’s see this diagram now with a more detailed implementation:
By using a VirtualService
and specifying the ordered HTTPRoute
rules, we can direct traffic to differen services. Here is how we can accomplish that with a VirtualService
:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: appvs
spec:
hosts:
- "httpbin2.com"
gateways:
- app-routing
http:
- match:
- uri:
prefix: /hello/world
route:
- destination:
host: app2
- match:
- uri:
prefix: /hello
route:
- destination:
host: app1
We have to specify the nested (more specific) URL first. In this case, that is /hello/world
that we want to direct to App2. Any requests that don’t match this route rule will continue to the next. So that means any route that is /hello/
but not followed by world
will then be directed to App1.
Note: All of the manifests are available below for this full example.
Let’s curl these endpoints to see this in action:
1
2
3
4
5
6
7
$ INGRESS_IP=$(kubectl get svc -n istio-system istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
$ curl -H "host: httpbin2.com" http://${INGRESS_IP}/hello/galaxy
(app1-549c548585-8cqld)
$ curl -H "host: httpbin2.com" http://${INGRESS_IP}/hello/world
(app2-75548c4dd9-cjdzd)
We see that our /hello/world
goes to App2, and all other /hello
routes go to App1! Using Istio we can achieve complex routing scenarios to match our microservices environment!
The manifests for this entire end-to-end example are below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
apiVersion: apps/v1
kind: Deployment
metadata:
name: app1
spec:
replicas: 1
selector:
matchLabels:
app: app1
template:
metadata:
labels:
app: app1
spec:
containers:
- name: app1
image: ghcr.io/trstringer/httpbin2:0.1.0
command: ["/httpbin2"]
args:
- "--message-hostname"
- "--port"
- "8080"
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: app1
spec:
selector:
app: app1
ports:
- name: http
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: app2
spec:
replicas: 1
selector:
matchLabels:
app: app2
template:
metadata:
labels:
app: app2
spec:
containers:
- name: app2
image: ghcr.io/trstringer/httpbin2:0.1.0
command: ["/httpbin2"]
args:
- "--message-hostname"
- "--port"
- "8080"
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: app2
spec:
selector:
app: app2
ports:
- name: http
port: 80
targetPort: 8080
---
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: app-routing
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "httpbin2.com"
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: appvs
spec:
hosts:
- "httpbin2.com"
gateways:
- app-routing
http:
- match:
- uri:
prefix: /hello/world
route:
- destination:
host: app2
- match:
- uri:
prefix: /hello
route:
- destination:
host: app1