Pages - Menu

2022年4月11日 星期一

[istio] virtual service 常用參數

前言

有些東西偶爾會用,但碰到時就忘了怎麼寫,
還要回去翻一下,
此篇包含

  1. set/remove header
  2. match header host 正規表示
  3. fault 回傳特定狀態
  4. allow cors
  5. rewrite
  6. match uri 正規表示



正文

當同一個virtual service內,有多個match uri時,優先順序,是由上到下。
 
例如:

match:
    - uri:
            prefix : /api
    - uri:
            prefix: /api/video      

這樣是永遠到不了 /api/video的
<br>

  1. set/remove header

add or modify header

spec:
  http:
    - name: api
      headers:
        request:
          set:
            x-forward-for: 127.0.0.1
      match:
        - uri:
            prefix: /api
      
      route:
        - destination:
            host: reverse-proxy.abc.svc.cluster.local
            port:
              number: 80

remove header

spec:
  http:
    - name: api
      headers:
        request:
          remove:
            - x-forward-for
      match:
        - uri:
            prefix: /api
      
      route:
        - destination:
            host: reverse-proxy.abc.svc.cluster.local
            port:
              number: 80
  1. match header host
    當 header host有對應到 這兩個 domain其中一個的話,
    則轉到對應的路徑
http:
  - match:
      - headers:
          Host:
            regex: (api.asia.com|api.american.com)
        uri:
          prefix: /api-v2
    rewrite:
      uri: /api
    route:
      - destination:
          host: reverse-proxy.abc.svc.cluster.local
          port:
            number: 80
        weight: 100

符合正則的皆進入


hosts:
  - '*'
gateways:
  - istio-system/istio-gateway-external
  - istio-system/istio-gateway-internal
http:
  - match:
      - headers:
          Host:
            regex: sxqa2.?\.777.+\.com
    route:
      - destination:
          host: video-admin-api-primary
          port:
            number: 80
        weight: 100
  1. fault

當路徑有倒到 api/v1/abc時,直接回傳 487的錯誤碼。
(這其實是拿來模擬錯誤用的)

http:
  - fault:
      abort:
        httpStatus: 487
        percentage:
          value: 100
    match:
      - uri:
          prefix: /api/v1/abc
    name: fault
    route:
      - destination:
          host: reverse-proxy.abc.svc.cluster.local
          port:
            number: 80
  1. allow cors
    開啟 cors的允許範圍
http:
  - corsPolicy:
      allowHeaders:
        - '*'
      allowOrigin:
        - '*'
    match:
      - uri:
          prefix: /api
    name: filedownload
    route:
      - destination:
          host:  reverse-proxy.abc.svc.cluster.local
          port:
            number: 80
  1. rewrite
    覆寫本來的uri
http:
  - match:
      - uri:
          prefix: /apmapi
    name: api
    rewrite:
      uri: /api
    route:
      - destination:
          host: reverse-proxy.abc.svc.cluster.local
          port:
            number: 80

在api之後的通通進去,但這種就無法使用rewrite了。
因為會將uri全部取代掉。

http:
  - match:
      - uri:
          regex: ^/api/.*
    route:
      - destination:
          host: yabo-api-qa-primary
          port:
            number: 80
        weight: 100

沒有留言:

張貼留言