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
      • 测试
      • 不同的 404 not found
    • dr-subset
    • vs-http-rewrite-by-header
    • 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-uri

# VirtualService 使用路径重写

有了 VirtualService 的路径重写功能后, 就更符合 Ingress 的标准定义了。

但 VirtualService 不仅仅如此, 路径重写包含了三种方式

  1. prefix: 前缀匹配。 只要 uri 路径的 前段 匹配则转发。 后端 自动补齐。
  2. exact: 精确匹配。 只有 uri 全部 匹配才转发, 并且只能转发某一个固定地址。
    • 精确匹配
  3. regex: 正则匹配。 只有 uri 全部路径 满足正则规则 才转发。
    • 正则规则: https://github.com/google/re2/wiki/Syntax
    • 精确匹配, 正则模式也是精确匹配目标路径。

补充: 关于正则匹配模式官网资料也很少。

# https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRewrite
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: vs-prod
  namespace: myistio
spec:
  gateways:
    - istio-tangx-in
  hosts:
    - svc-prod
    - istio.tangx.in
  http:
  - name: "prefix-match"  # 规则名称
    match:
    - uri:
        prefix: "/p1"  # 新路径, prefix 前缀匹配, 满足 /p1 的都要被重写
    rewrite:
      uri: "/prod"    # 老路径
    route:
    - destination:
        host: svc-prod  # 后端服务

  - name: "exact-match"
    match:
    - uri:
        exact: "/p2-list" # 新路径, exact 精确匹配, 只能满足 /p2-list
    rewrite:
      uri: "/prod/list"   # 精确匹配
    route:
    - destination:
        host: svc-prod

  - name: "regex-match"
    match:
    - uri:
        regex: "/pr[1-3]/.*" # 新路径, regex 正则匹配。 正则匹配的整个 uri,因此允许所有要 使用 `.*`。 正则规则使用: https://github.com/google/re2/wiki/Syntax
    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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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

# 测试

执行命令, 部署环境。 (快捷键见第一章)

ka -f istio-samples/05
1

使用 05.http 中的测试用例, 进行测试。


### GET,原访问地址
#     现在已经 404
GET http://istio.tangx.in/prod/list


### GET 使用路径重写: prefix 前缀匹配
GET http://istio.tangx.in/p1/list


### GET 使用路径重写: exact 精确匹配
GET http://istio.tangx.in/p2-list


### GET 使用路径重写: regex 正则匹配(有效)
GET http://istio.tangx.in/pr3/list

### GET 使用路径重写: regex 正则匹配(无效)
GET http://istio.tangx.in/pr4/list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 不同的 404 not found

客户端请求后得到的 404 not found 有两种

  1. istio 没有匹配到路由规则而返回的 404.

### GET,原访问地址
#     现在已经 404, istio 返回
GET http://istio.tangx.in/prod/list

    # HTTP/1.1 404 Not Found
    # date: Mon, 15 Nov 2021 04:19:43 GMT
    # server: istio-envoy
    # connection: close
    # content-length: 0

1
2
3
4
5
6
7
8
9
10
11
  1. istio 成功将请求转发到后端server, 后端 server 找不到路由而返回的 404。
### GET 使用路径重写: prefix 前缀匹配
#     404 not found, server 返回。
GET http://istio.tangx.in/p1/list2

    # HTTP/1.1 404 Not Found
    # content-type: text/plain
    # date: Mon, 15 Nov 2021 04:20:09 GMT
    # content-length: 18
    # x-envoy-upstream-service-time: 0
    # server: istio-envoy
    # connection: close

    # 404 page not found
1
2
3
4
5
6
7
8
9
10
11
12
13
编辑 (opens new window)
上次更新: 2023/02/05, 02:48:13
gateway
dr-subset

← gateway dr-subset→

最近更新
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号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式