Search code examples
kubernetesmicroservicesistioistio-gatewayistio-operator

Is there a way were we can specify two services for single context path in istio virtual service?


I have two different micro-services running in same name space, both have same context path (ex - my/context/path), further controllers are different in both of them, for example service one supports - my/context/path/service1 and service2 supports my/context/path/service2 now when i defined vs like this, its always redirecting to the service1, is there a possible way to achieve this? below is my VS:


        apiVersion: networking.istio.io/v1alpha3
        kind: VirtualService
        metadata:
        name: test-service
        namespace: ns-ns
        spec:
         gateways:
         - gateway.ns-ns
        hosts:
         - '*'
       http:
       - match:
        - uri:
          prefix: /my/context/path
        route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
      - route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

I also tried below VS, but this also seems to redirect to first service.

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-service
  namespace: ns-ns
spec:
  gateways:
  - gateway.ns-ns
  hosts:
  - '*'
  http:
    - match:
      - uri:
          prefix: /my/context/path
      route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
    - match:        
      - uri:
          prefix: /my/context/path/service2
      route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

i am not sure if this is achievable or not, or do i need to separate the context part of both the services?


Solution

  • The routes are matched in order. Thus you need to start from the most specific to the most generic. e.g.

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: test-service
      namespace: ns-ns
    spec:
      gateways:
      - gateway.ns-ns
      hosts:
      - '*'
      http:
        - match:        
          - uri:
              prefix: /my/context/path/service2
          route:
            - destination:
                host: service2.ns-ns.svc.cluster.local
                port:
                  number: 9000
        - match:
          - uri:
              prefix: /my/context/path
          route:
            - destination:
                host: service1.ns-ns.svc.cluster.local
                port:
                  number: 9000