Header Based Routing
In this tutorial, we are going to discuss about header based routing and how to route the request based on few conditions.
As you all know whenever a request getting into the system it’s going to have a set of headers and that’s going to have what user and lot other information. Based on the value that we have within the header I can route the request to different subset available within a service.
I’m going to consider the same book info example where I’m going to consider the user name that is available as a part of the header and route the request to the reviews to different subset version.
Let’s go ahead and do the demo. Within the cluster I’m going to create a virtual service something like this.
So the kind is VirtualService. Within the specification I am going to mention the host as reviews because the requests going to land up within the review.
As a part of the match condition, I will be having the route destination to subset version two when the header with end-user as the key that’s matching with the value jason or otherwise I will be having the route destination to the host reviews and subset V1
Before Login
So all the requests having the header as jason will get routed to version 2 and other requests or the other users will get routed to version 1. Let me go ahead and create the virtual service.
$ kubectl apply -f samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml
virtualservice.networking.istio.io/reviews created
Let me go ahead and access the web UI. Now Let me refresh the page.
So whatever the number of times I’m refreshing, It’s going to land in version 1. So what’s happening now? the end user header is not available. So directly it’s getting into the destination version 1. That’s the reason it doesn’t have any stars.
After Login
And if I log in as the user jason, it should route me to version 2. Sign in, let me provide the user say sign in Now, I logged in as the user jason, no need to provide any password.
And it got the review from version 1 and I’m getting the stars in black color. That means landed in version 2. Here, let me go ahead and refresh the page. What ever the number of times I’m refreshing I’m going to get the same review page.
So we’re able to route the request based on the header that we’re setting within the application.
Summary
So in a quick summary we saw how to route the request based on the user header that we are setting within the application using the virtual service.