istio virtualservice:使用正则过滤流量

2021-03-14 07:56:35 浏览数 (1)

Istio regular expressions use the RE2 regular expression syntax.

重点提一下,regex 匹配需要完整

regex: .*(?i)(curl|python|go|java|javascript|php|ruby|perl).*留意两边有 .*,如果 User-agent 是 curl/7.64.1 则能匹配成功。另外, (?i)是忽略大小写。以下为完整示例:将 user-agent 为程序的流量导入到 query-ip 服务中,git 流量导入到 gitbook 中。

代码语言:txt复制
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: query-ip
spec:
  hosts:
  - "*"
  gateways:
  - bookinfo-gateway
  http:
  - match:
    - headers:
        user-agent:
          regex: .*(?i)(curl|python|go|java|javascript|php|ruby|perl).*
    route:
    - destination:
        host: query-ip.query-ip.svc.cluster.local
        port:
          number: 1080 ## service port     
  - match:
    - headers:
        user-agent:
          prefix: git
    route:
    - destination:
        host: gitbook.default.svc.cluster.local
        port:
          number: 10080 ## service port       

reference

  • 1 Google. regular expression
  • 2 istio. InvalidRegexp
  • 3 istio. Virtual Service
  • 4 k8s. 部署 istio 1.8.2

0 人点赞