url-routing - virtualservice(istio)下如何添加多个请求匹配器和重写?
问题描述
我正在使用 istio 和 Kubernetes 进行开发。我搜索了很多文章和帖子,但没有找到预期的答案。下面是我的虚拟服务脚本。
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
helm.fluxcd.io/antecedent: default:helmrelease/hello-11
creationTimestamp: "2020-10-15T11:52:02Z"
generation: 2
name: hello-11
namespace: default
resourceVersion: "4178089"
selfLink: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/hello-11
uid: kfk9580d-0fedc-11eb-b8a2-pp155d6a1e13
spec:
gateways:
- istio-system/istio-ingressgateway
- istio-system/istio-ingressgateway
hosts:
- ext-auth-host
- no-ext-auth-host
http:
- gateways:
- istio-system/istio-ingressgateway
match:
- headers:
x-jwt-extracted-xx-id:
exact: "9980098"
name: hello-11-ext-auth
route:
- destination:
host: hello-11.default.svc.cluster.local
- gateways:
- istio-system/istio-ingressgateway
headers:
request:
add:
x-jwt-extracted-xx-id: "9980098"
match:
- uri:
prefix: /9980098/api/show
name: hello-11-no-ext-auth
rewrite:
uri: /api/show
route:
- destination:
host: hello-11.default.svc.cluster.local
我想添加多个匹配以及多个重写。但是,它没有按预期工作。我预期的虚拟服务应该如下所示。
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
helm.fluxcd.io/antecedent: default:helmrelease/hello-11
creationTimestamp: "2020-10-15T11:52:02Z"
generation: 2
name: hello-11
namespace: default
resourceVersion: "4178089"
selfLink: /apis/networking.istio.io/v1beta1/namespaces/default/virtualservices/hello-11
uid: kfk9580d-0fedc-11eb-b8a2-pp155d6a1e13
spec:
gateways:
- istio-system/istio-ingressgateway
- istio-system/istio-ingressgateway
hosts:
- ext-auth-host
- no-ext-auth-host
http:
- gateways:
- istio-system/istio-ingressgateway
match:
- headers:
x-jwt-extracted-xx-id:
exact: "9980098"
name: hello-11-ext-auth
route:
- destination:
host: hello-11.default.svc.cluster.local
- gateways:
- istio-system/istio-ingressgateway
headers:
request:
add:
x-jwt-extracted-xx-id: "9980098"
match:
- uri:
prefix: /9980098/api/show
name: hello-11-no-ext-auth
rewrite:
uri: /api/show
route:
- destination:
host: hello-11.default.svc.cluster.local
match:
- uri:
prefix: /9980098/api/send
name: hello-11-no-ext-auth
rewrite:
uri: /api/send
route:
- destination:
host: hello-11.default.svc.cluster.local
多个 url 重写是:
uri: /api/send
uri: /api/show
任何建议或参考都会有所帮助和感激。
谢谢
解决方案
很难说这里出了什么问题,对我来说整个虚拟服务都写错了。我将从修复虚拟服务中的多个问题开始,例如 http 部分中的多个网关或名称。
按照以下文档和示例修复您的虚拟服务。
- https://istio.io/latest/docs/reference/config/networking/virtual-service/#Headers
- https://istio.io/latest/docs/reference/config/networking/virtual-service/#HTTPRewrite
- https://istio.io/latest/docs/tasks/traffic-management/request-routing/
- https://stackoverflow.com/a/60826906/11977760
- Istio 添加和删除标头,但不会覆盖
- https://www.istiobyexample.dev/multiple-traffic-rules
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-route
spec:
gateways:
- mygateway
http:
- name: "example-1"
match:
- uri:
prefix: "/wpcatalog"
rewrite:
uri: "/newcatalog"
route:
- destination:
host: reviews
- name: "example-2"
match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
- name: "example-3"
headers:
response:
add:
foo: "bar"
match:
- uri:
prefix: /a
rewrite:
uri: /
route:
- destination:
host: reviews
推荐阅读
- typescript - 如何解决错误:(4, 1) TS2349:无法调用其类型缺少调用签名的表达式。类型“typeof moment”没有兼容的调用签名
- java - 圆圈扇区内的孩子的最大尺寸
- html - 输入类型复选框无法正确样式
- php - 如何为我的 API 程序清理 [null] 数组输入?
- c# - 使用 XmlCDataSection 格式化 XmlDocument
- mongodb - MongoDB:嵌套数组上的聚合查询
- c# - ASP.NET Core 3.0 get_HostingEnvironment() 在扩展中找不到方法
- python - Exec 无法访问其父环境中的变量
- javascript - 在栅格上使用 d3.drag-event 拖动一条线
- javascript - reCaptcha V3 仅在首次提交表单时验证失败