天天看點

Istio - TrafficeManagement - Match

在Route中最重要的字段是條件字段Match,為一個MatchRequest類型的數組,表示請求滿足條件,支援将HTTP屬性如uri、scheme、method、authority、port等作為條件來比對請求。

一個URL的完整格式是:URI=scheme:[//authority]path[?query][#fragment]

實際上authority的标準定義是:authority=[userinfo@]host[:port]

## authority

1. uri、scheme、method、authority:4個字段都是StringMatch類型,在比對請求時都支援exact、prefix和regex三種模式的比對

2. headers:比對請求中的Header,是一個map類型。map的key是字元串類型,value是StringMatch類型。即對于每一個Header的值,都可以使用精确、字首和正則三種方式進行比對。

3. port:表示請求服務的端口。大部分服務隻開放一個端口,這也是在微服務中推薦的做法,在這種場景下可以不指定port。

4. sourceLabels:是一個map類型的鍵值對,表示請求來源負載比對标簽。這在很多情況有用,可以對一組服務都打一個相同的标簽,然後使用sourceLabels字段對這些服務實施相同的流量規則。在Kubernetes平台上,這裡的Label就是Pod上的标簽。

5. gateway:表示規則應用的Gateway名稱,語義同VirtualService 上面的gateway定義,是一個更細的Match條件,會覆寫在VirtualService上配置的gateway。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata: 
  name: web-vs
  namespace: demo
spec:
  hosts:
  - "*"
  gateways:
  - web-gateway
  http:
  - match:
    - headers:
         location:
  exact: hongkong
      uri:
        prefix: /test
    - uri:
        prefix: /api/health
    route: 
    - destination:
        host: httpd-service      

# 比對到header的location是hongkong并且請求 /test,或者uri 為 /api/health 開頭的請求

繼續閱讀