当前位置:网站首页>Istio FAQ: virtualservice route matching sequence

Istio FAQ: virtualservice route matching sequence

2022-06-24 16:15:00 imroc

This article comes from Istio Learning notes

background

Writing VirtualService When routing rules , Usually match Various paths forward to different back-end services , Sometimes names conflict accidentally , As a result, only the previous services are always matched , such as :

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: test
spec:
  gateways:
  - default/example-gw
  hosts:
  - 'test.example.com'
  http:
  - match:
    - uri:
        prefix: /usrv
    rewrite:
      uri: /
    route:
    - destination:
        host: usrv.default.svc.cluster.local
        port:
          number: 80
  - match:
    - uri:
        prefix: /usrv-expand
    rewrite:
      uri: /
    route:
    - destination:
        host: usrv-expand.default.svc.cluster.local
        port:
          number: 80

istio Matching is matching in order , Unlike nginx Then use the longest prefix to match . Use here prefix Match , The first is /usrv, Indicates that as long as the access path prefix contains /usrv It will be forwarded to the first service , Due to the second matching path /usrv-expand It also belongs to the belt /usrv The prefix of , Therefore, it will never be forwarded to the service with the second matching path .

Solution

In this case, you can adjust the matching order , If the prefix has a conflicting relationship with inclusion , The longer it is placed in front of it :

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: test
spec:
  gateways:
  - default/example-gw
  hosts:
  - 'test.example.com'
  http:
  - match:
    - uri:
        prefix: /usrv-expand
    rewrite:
      uri: /
    route:
    - destination:
        host: usrv-expand.default.svc.cluster.local
        port:
          number: 80
  - match:
    - uri:
        prefix: /usrv
    rewrite:
      uri: /
    route:
    - destination:
        host: usrv.default.svc.cluster.local
        port:
          number: 80

You can also use regular matching :

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: test
spec:
  gateways:
  - default/gateway
  hosts:
  - 'test.example.com'
  http:
  - match:
    - uri:
        regex: "/usrv(/.*)?"
    rewrite:
      uri: /
    route:
    - destination:
        host: nginx.default.svc.cluster.local
        port:
          number: 80
        subset: v1
  - match:
    - uri:
        regex: "/usrv-expand(/.*)?"
    rewrite:
      uri: /
    route:
    - destination:
        host: nginx.default.svc.cluster.local
        port:
          number: 80
        subset: v2
原网站

版权声明
本文为[imroc]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210501170856013u.html