Service 如何在istio虚拟服务头中使用或逻辑进行精确匹配?
我有两个正在运行的服务和4个不同的头。我想将标题为Service 如何在istio虚拟服务头中使用或逻辑进行精确匹配?,service,header,httprequest,istio,Service,Header,Httprequest,Istio,我有两个正在运行的服务和4个不同的头。我想将标题为a和c的请求定向到一个服务,将标题为b和d的请求定向到另一个服务。在虚拟服务中实现这一点的最佳方法是什么 apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: sample_virtualservice namespace: sample_namespace spec: hosts: - sample_service.samp
a
和c
的请求定向到一个服务,将标题为b
和d
的请求定向到另一个服务。在虚拟服务中实现这一点的最佳方法是什么
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: sample_virtualservice
namespace: sample_namespace
spec:
hosts:
- sample_service.sample_namespace.svc.cluster.local
http:
- match:
- headers:
x-test:
exact: "a" OR "c" //This doesn't work but I want to achieve.
route:
- destination:
host: service_1.sample_namespace.svc.cluster.local
port:
number: 80
- route:
- destination:
host: service_2.sample_namespace.svc.cluster.local
port:
number: 80
我相信一定有更好的方法,而不是在清单文件中多次提到同一路由目的地。据我所知,istio虚拟服务中没有逻辑运算符
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: sample_virtualservice
namespace: sample_namespace
spec:
hosts:
- sample_service.sample_namespace.svc.cluster.local
http:
- match:
- headers:
x-test:
regex: "a|c"
route:
- destination:
host: service_1.sample_namespace.svc.cluster.local
port:
number: 80
- route:
- destination:
host: service_2.sample_namespace.svc.cluster.local
port:
number: 80
它们仅在和中提供
正如您现在提到的,唯一的选择是多次使用同一路线 所以虚拟服务看起来是这样的
http:
- name: "a"
match:
- headers:
x-test:
exact: "a"
route:
- destination:
host: service_1.sample_namespace.svc.cluster.local
port:
number: 80
- name: "b"
match:
- headers:
x-test:
exact: "b"
route:
- destination:
host: service_1.sample_namespace.svc.cluster.local
port:
number: 80
- name: "c"
match:
- headers:
x-test:
exact: "c"
route:
- destination:
host: service_2.sample_namespace.svc.cluster.local
port:
number: 80
- name: "d"
match:
- headers:
x-test:
exact: "d"
route:
- destination:
host: service_2.sample_namespace.svc.cluster.local
port:
number: 80
另外,我看到您在sample_名称空间中部署了虚拟服务,请记住,应该在同一名称空间中部署虚拟服务。如果不是,您应该像下面的示例中那样添加它
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: my-gateway
namespace: some-config-namespace
检查spec.gateways
部分
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: bookinfo-Mongo
namespace: sample_namespace
spec:
gateways:
- some-config-namespace/my-gateway
我找到了一个使用
regex
而不是exact
的干净解决方案,它允许我们向同一个目的地发送不同头的请求,而无需在清单文件中多次提及同一路由目的地
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: sample_virtualservice
namespace: sample_namespace
spec:
hosts:
- sample_service.sample_namespace.svc.cluster.local
http:
- match:
- headers:
x-test:
regex: "a|c"
route:
- destination:
host: service_1.sample_namespace.svc.cluster.local
port:
number: 80
- route:
- destination:
host: service_2.sample_namespace.svc.cluster.local
port:
number: 80
如果这符合你的要求,你能把它标记为被接受的答案吗?(即使这是你自己的答案,也可以这么做)