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

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

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

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

  • istio的环境准备

  • istioVirtualService

    • isti VirtualService 和 k8s Ingress
    • 使用 istio Gateway 允许外部访问
    • VirtualService 使用路径重写
    • 使用 DestinationRule Subset 进行路由分组(版本控制)
    • VirtualService 使用 header 重写路由
    • VirtualService 路由重定向
      • http redirect
      • 兼顾内群内外的重定向
        • 相同路由规则下 redirect 和 route 互斥
        • 使用多路由规则无法兼顾鱼和熊掌
        • 使用多配置兼得鱼和熊掌(不优雅)
    • VirtualService 重试机制
    • VirtualService 混沌测试/错误注入
    • VirtualService 服务委托
    • VirtualService Header 操作
    • Gateway 支持 https 访问 - 标准模式
    • 使用 DestionationRule 流量控制策略 - 简单负载均衡
  • 老麦 Go
  • istioVirtualService
老麦
2023-02-01
目录

VirtualService 路由重定向

# 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
上次更新: 2023/04/05, 05:23:58
VirtualService 使用 header 重写路由
VirtualService 重试机制

← VirtualService 使用 header 重写路由 VirtualService 重试机制→

最近更新
01
How the creator of Angular is dehydrating the web (Ep 574)
06-07
02
For those who just don’t Git it (Ep 573)
06-07
03
Modern work requires attention. Constant alerts steal it
06-07
更多文章>
Theme by Vdoing | Copyright © 2022-2024 IT七剑客 | MIT License
  • 闽ICP备2021006579号-4
  • 闽公网安备 35012102500470号
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式