skip to Main Content

I am trying to implement following routing strategy in my K8S cluster.

  1. route foo/bar/* requests to bar-service
  2. route foo/* requests (except foo/bar/*) requests to foo-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


  1. Try with edit the virtulaservice

    • name: "/foo/bar/"
      match:

      • uri:
        prefix: /foo/bar/*
        rewrite:
        uri: /
        route:
      • destination:
        host: bar-service
        port:
        number: 80
    • name: "foo/*"
      match:

      • uri:
        prefix: /foo/*
        rewrite:
        uri: /
        route:
      • destination:
        host: foo-service
        port:
        number: 80
    Login or Signup to reply.
  2. This can be accomplished with a VirtualService and HTTP route rules. Let’s say you have App1:

    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
    

    And a second app, App2:

    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
    

    In the event that you want app2 to serve all requests to /specific-route, and all other requests to app1, you’d first have to create a Gateway:

    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"
    

    Now wire up a VirtualService to this Gateway, which will handle the routing:

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: appvs
    spec:
      hosts:
        - "httpbin2.com"
      gateways:
        - app-routing
      http:
        - match:
            - uri:
                prefix: /specific-route
          route:
            - destination:
                host: app2
        - match:
            - uri:
                prefix: /
          route:
            - destination:
                host: app1
    

    If you look at VirtualService.spec.http you’ll find the list of HTTPRoutes.

    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:

    $ curl -H "host: httpbin2.com" http://20.102.18.97/specific-route/more
    (app2-75548c4dd9-cjdzd)
    

    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:

    curl -H "host: httpbin2.com" http://20.102.18.97/more/things/here
    (app1-549c548585-8cqld)
    
    Login or Signup to reply.
  3. 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

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: fooapplication
      namespace: default
    spec:
      gateways:
      - foo-gateway
      hosts:
      - '*'
      http:
      - match:
        - uri:
            prefix: /foo
         route:
        - destination:
            host: foo-service
            port:
              number: 8080
      - match:
        - uri:
            regex: /foo.*
        rewrite:
          uri: /bar-service
        route:
        - destination:
            host: bar-service
            port:
              number: 8080
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search