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
    • vs-http-redirect
      • http redirect
      • 兼顾内群内外的重定向
        • 相同路由规则下 redirect 和 route 互斥
        • 使用多路由规则无法兼顾鱼和熊掌
        • 使用多配置兼得鱼和熊掌(不优雅)
    • 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-redirect

# VirtualService 路由重定向

在 VirtualService 配置中, 除了 http rewrite 路由重写之外, 还有 http redirect 路由重定向。 即常说的 30x。

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

# http redirect

VirtualService 重定向配置如下。 有三个重要参数

  1. uri: 重定向后的 uri
  2. redirectCode: 重定向时的 http response code。 ex: 301, 302。 默认值为 301 。
  3. authority: 重定向后的 http host。 即 http response header 中的 location 字段。
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: review-http-redirect
  namespace: myistio
spec:
  gateways:
    - istio-tangx-in
  hosts:
    - svc-review
    - istio.tangx.in
  http:
    - match:
        - uri:
            exact: /review
      redirect:
        uri: /review/all
        redirectCode: 302
        authority: svc-review  # 重定向后的地址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

使用 curl 命令请求测试, 结果如下。

curl -I  http://istio.tangx.in/review

    HTTP/1.1 302 Found
    location: http://svc-review/review/all
    date: Mon, 15 Nov 2021 10:32:59 GMT
    server: istio-envoy
    transfer-encoding: chunked
1
2
3
4
5
6
7

可以看到已经正常实现重定向。

# 兼顾内群内外的重定向

但是 location: http://svc-review/review/all 结果是集群内部地址, 而我们的请求时从集群外部发起的访问。

虽然可以将 authority 字段的值修改为 集群外部地址。

  http:
    - match:
        - uri:
            exact: /review
      redirect:
        uri: /review/all
        redirectCode: 302
        # authority: svc-review
        authority: istio.tangx.in
1
2
3
4
5
6
7
8
9

但这是一个 蠢到爆 的方式。

  1. 每次请求都必须要走 外部网关
  2. 外部地址与 VirtualService 强耦合, 无法适配多地址的情况。

# 相同路由规则下 redirect 和 route 互斥

下面这个规则是不合法的, 在 同一条 路由规则下, redirect 和 route 互斥。

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: review-http-redirect
  namespace: myistio
spec:
  gateways:
    - istio-tangx-in
  hosts:
    - svc-review
    - istio.tangx.in
  http:
    - match:
        - uri:
            exact: /review
      redirect:
        uri: /review/all
        redirectCode: 302
      route:    # redirect 和 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

报错如下

for: "istio-samples/10-http-redirect/vs.yml": admission webhook "validation.istio.io" denied the request: configuration is invalid: HTTP route cannot contain both route and redirect
1

# 使用多路由规则无法兼顾鱼和熊掌

遗留问题: 虽然 redirect 和 route 不能在 同一个 规则下。 但是他们可以在 不同 规则下。 因此使用 多条 路由规则即可兼得鱼和熊掌 ???

经测试发现, 如下包含 gateway 字段的 VirtualService 定义, 无法完成内网的 http-redirect。

---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: review-http-redirect
  namespace: myistio
spec:
  gateways:
    - istio-tangx-in
  hosts:
    - svc-review
    - istio.tangx.in
  http:
    # 规则重定向
    - match:
        - uri:
            exact: /review
      redirect:
        uri: /review/all
        redirectCode: 302

    # 路由转发
    - match:
        - uri:
            prefix: /
      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

在集群内部的 toolbox 容器中的执行命令, 出现 not found 错误。

curl -I http://svc-review/review

    HTTP/1.1 404 Not Found
    date: Mon, 15 Nov 2021 11:08:00 GMT
    server: istio-envoy
    transfer-encoding: chunked
1
2
3
4
5
6

# 使用多配置兼得鱼和熊掌(不优雅)

没办法, 只能创建两个配置实现内外网的重定向

  1. 不包含 gateway 的 vs.yml
  2. 包含 gateway 的 vs-gateway.yml
编辑 (opens new window)
上次更新: 2023/02/05, 02:48:13
vs-http-rewrite-by-header
vs-http-retry

← vs-http-rewrite-by-header vs-http-retry→

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