Ghisa Ioachim
Ghisa Ioachim

Reputation: 11

how can I add a rewrite uri in the destination of virtual service

I am trying to set up the routes of two services in a kubernetes cluster, and I want to do a canary release of 2 different services with specific weighs. For example: if /endpoint1 is reached then it routes 50% of the traffic to service1, and for the other 50% I want to rewrite, then route to service2. If I try this virtual service

  http:
  - match:
      uri:
        exact: /path1
    rewrite:
      uri: /path1modified
    route:
    - destination:
        host: service1host
        weigh: 50
    - destination:
        host: service2host
        weigh: 50

it will rewrite then route with path1modified. so I would need something like this

  http:
  - match:
      uri:
        exact: /path1
    route:
    - destination:
        host: service1host
        weigh: 50
    - destination:
        host: service2host
        weigh: 50       
        rewrite:
          uri: /path1modified

I am curios if this is possible. Thanks a lot.

UPDATE Thank you all for the answers given, eventually I found a way basically I had a webapp(which used path1) and I had a service(which used modifiedPath1 in the backend, but it needed path1 in browser, use rewrite) and I wanted to weight the traffic between these 2. I tried with virtual service, but I couldn't manage to find a way to include both paths. My solution was to add a traffic manager, which sends weighted traffic to webapp and my service, then for the webapp leave the endpoints as they are, and for the service if match uri exact /path1 then rewrite to modifiedPath1.

Upvotes: 1

Views: 4056

Answers (2)

suren
suren

Reputation: 8786

There are couple of things you are not doing, I would say. First of all to do canary, you don't do it for two different services, but your do for two subsets of the same service. In the end, there are two different deployments, but you need to define just one service, then through a DestinationRule you define the subsets.

In other words, you have two deployments right? These two deployments will have one common label, and one label that differs. Through the common labels you will define DESTINATION_HOST_THAT_HAS_SUBSETS, and through the labels that differs you would define the subsets service1host and service2host.

Then, your VirtualService will look like this:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: server-vs
spec:
  hosts:
  - DESTINATION_HOST_THAT_HAS_SUBSETS
  http:
  - match:
    - uri:
        prefix: /path1
    rewrite:
      uri: /path1modified
    route:
    - destination:
        host: DESTINATION_HOST_THAT_HAS_SUBSETS
        subset: service1host
      weight: 50
    route:
    - destination:
        host: DESTINATION_HOST_THAT_HAS_SUBSETS
        subset: service2host
      weight: 50

UPDATE

After jt97's comment, which is correct about being possible to have two separate services, this VirtualService should work as well.

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: server-vs
spec:
  hosts:
  - DESTANTION_HOST
  http:
  - match:
    - uri:
        prefix: /path1
    rewrite:
      uri: /path1modified
    route:
    - destination:
        host: service1host
      weight: 50
    route:
    - destination:
        host: service2host
      weight: 50

Upvotes: 2

Jakub
Jakub

Reputation: 8830

As far as I know that's not possible to do.

If you take a look at istio documentation about rewrite in virtual service, rewrite is part of http, so it cannot be used in destination.

Link which might be helpful with weight-based routing:

Upvotes: 0

Related Questions