IT七剑客 IT七剑客
首页
wresource
郭霖
孤寒者
IT邦德
沉默王二
老麦
stackoverflow
GitHub (opens new window)
首页
wresource
郭霖
孤寒者
IT邦德
沉默王二
老麦
stackoverflow
GitHub (opens new window)
  • 从零开始写 k8s 发布工具(一)

  • 从零开始写 k8s 发布工具(二)

  • 从零开始写 k8s 发布工具(三)

  • 从零开始写 k8s 发布工具(四)

  • istio的环境准备

  • istioVirtualService

    • vs-and-ingress
    • gateway
    • vs-http-rewrite-by-uri
    • dr-subset
    • vs-http-rewrite-by-header
      • 匹配模式
        • 1. 前缀模式
        • 2. 精确模式
        • 3. 正则模式
      • 只检测 key 不检测 value
      • 一些违反规范规则的测试
    • vs-http-redirect
    • vs-http-retry
    • vs-http-fault-injection
    • vs-http-delegate
    • vs-http-header-operation
    • gw-https-support-standard
    • dr-simple-loadbalance
  • 老麦 Go
  • istioVirtualService
老麦
2023-02-01
目录

vs-http-rewrite-by-header

# VirtualService 使用 header 重写路由

在 istio 中, 除了 path 之外还可以使用 Header 进行路由规则管理。

为了更好的展示 header 路由效果, 这里配合使用了 uri 的精确匹配模式。 实现之后, 只能访问地址 http://istio.tangx.in/ , 其他均为 404。 具体哪个服务应答, 完全根据 header 匹配选择。

效果如下:

header-route

使用 Header 匹配有几个必要条件

  1. Header 的 key 只能包含 小写字母 和 连字符 -。
    • 从实际测试中来看。 这个规则只是一个 建议。 使用 驼峰(SrvReview) 时依旧可以转发。
  2. Header 的 value 大小写敏感。
  3. 如果 Header 的值为 空, 则只检测 key 是否存在。
    • 将 key 的匹配 值 设置为 空。 prefix, exact, regex 模式均可。

https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPMatchRequest

# 匹配模式

与之前的 路径匹配 相似, header 匹配也具有三种模式。

  1. prefix: 前缀模式
  2. exact: 精确模式
  3. regex: 正则模式

# 1. 前缀模式

header 的 key 相同, value 从零开始相同。 与正则规则 ^value.* 等价

请求方式如下

### GET prod: 前缀模式: 匹配 x-prod
GET http://istio.tangx.in/
app: x-prod-anything
1
2
3

VirtualService 配置如下

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-header-route
  namespace: myistio
spec:
  gateways: # 选择 gateway
    - istio-tangx-in
  hosts:
    - istio.tangx.in
  http:
  - name: "prod-route"
    match:
    - headers:
        app:
          prefix: x-prod  # 使用 前缀模式
      uri:                # 为了更好的展示 header 路由。 这里配合 uri 的精确匹配模式
        exact: /
    rewrite:
      uri: /prod/list
    route:
    - destination:
        host: svc-prod

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

# 2. 精确模式

header 的 key,value 必须完全匹配规则。

请求方式如下

### GET reivew: 精确模式, app 的值必须是 review
GET http://istio.tangx.in/
app: review
1
2
3

VirtualService 规则如下

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-header-route
  namespace: myistio
spec:
  gateways: # 选择 gateway
    - istio-tangx-in
  hosts:
    - istio.tangx.in
  http:
  - name: "review-route"
    match:
    - headers:
        app:
          exact: review   # 使用精确模式
      uri:
        exact: /
    rewrite:
      uri: /review/all
    route:
    - destination:
        host: svc-review
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 3. 正则模式

略

# 只检测 key 不检测 value

将任意匹配规则 prefix / exact / regex 的匹配值结果设置为 空。

## 省略
  # If the value is empty and only the name of header is specfied, presence of the header is checked.
  - name: "key without value"
    match:
    - headers:
        onlykey:
          prefix: ""   # 有 key 没有 value
        #   exact: ""   # 有 key 没有 value
        #   regex: ""   # 有 key 没有 value
      uri:
        exact: /
    rewrite:
      uri: /review/all
    route:
    - destination:
        host: svc-review
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 一些违反规范规则的测试

# https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPMatchRequest

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-header-route
  namespace: myistio
spec:
  gateways: # 选择 gateway
    - istio-tangx-in
  hosts:
    - istio.tangx.in
  http:

## 一些违反规则的测试 ###
  # The keys uri, scheme, method, and authority will be ignored.
  # 其实不会被忽略
  - name: "key (uri) in header"
    match:
    - headers:
        uri:    
          exact: "app"
      uri:
        exact: /
    rewrite:
      uri: /review/all
    route:
    - destination:
        host: svc-review

  # The header keys must be lowercase and use hyphen as the separator, e.g. x-request-id.
  # 其实可以为大写
  - name: "uppercase in keys"
    match:
    - headers:
        SrvReview:    
          exact: "review"
      uri:
        exact: /
    rewrite:
      uri: /review/all
    route:
    - destination:
        host: svc-review

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

测试结果

# 违反规则的测试

#### 1. header key 出现关键字将被忽略
#  > 关键字出现, 不会被忽略
GET http://istio.tangx.in/
uri: app

#### 2. header key 只能是 `小写字母` 和 `-`
#  > 可以为大写
GET http://istio.tangx.in/
SrvReview: review

1
2
3
4
5
6
7
8
9
10
11
12
编辑 (opens new window)
上次更新: 2023/02/05, 02:48:13
dr-subset
vs-http-redirect

← dr-subset vs-http-redirect→

最近更新
01
Coding 102 Writing code other people can read
02-26
02
Kotlin Flow响应式编程,StateFlow和SharedFlow
02-05
03
Kotlin Flow响应式编程,操作符函数进阶
02-05
更多文章>
Theme by Vdoing | Copyright © 2022-2023 IT七剑客 | MIT License
  • 闽ICP备2021006579号-4
  • 闽公网安备 35012102500470号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式