如何在 istio 虚拟服务中使用 OR 逻辑 header 完全匹配?

How to use OR logic in istio virtual service header exact match?

我有两个服务 运行 和 4 个不同的服务 headers。我想将 headers ac 的请求定向到一项服务,将 bd 的请求定向到另一项服务。在虚拟服务清单中实现该目标的最佳方式是什么?

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 虚拟服务中没有逻辑运算符。

它们仅适用于 rules and mixer expression language


正如你提到的,目前唯一的选择是多次使用同一条路线。

所以虚拟服务看起来像这样。

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_namespace 中部署了虚拟服务,请记住 gateway 应该部署在同一名称空间中。如果不是,您应该像下面的示例一样添加它。

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 的干净解决方案,它允许我们将请求发送到不同 headers 的相同目的地,而无需多次提及相同的路由目的地清单文件。

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