istio virtualservice:使用正则过滤流量
Istio regular expressions use the RE2open in new window 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 中。
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
reference
- [1] Google. regular expressionopen in new window
- [2] istio. InvalidRegexpopen in new window
- [3] istio. Virtual Serviceopen in new window