【问题标题】:Is there a way were we can specify two services for single context path in istio virtual service?有没有办法在 istio 虚拟服务中为单个上下文路径指定两个服务?
【发布时间】:2021-12-29 02:51:08
【问题描述】:

我有两个不同的微服务在同一个命名空间中运行,它们都有相同的上下文路径(例如 - my/context/path),进一步的控制器在它们中是不同的,例如服务一支持 - my/context/ path/service1 和 service2 支持 my/context/path/service2 现在当我像这样定义vs时,它总是重定向到service1,有没有可能的方法来实现这一点? 下面是我的VS:


        apiVersion: networking.istio.io/v1alpha3
        kind: VirtualService
        metadata:
        name: test-service
        namespace: ns-ns
        spec:
         gateways:
         - gateway.ns-ns
        hosts:
         - '*'
       http:
       - match:
        - uri:
          prefix: /my/context/path
        route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
      - route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

我也在VS下面尝试过,但这似乎也重定向到第一个服务。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: test-service
  namespace: ns-ns
spec:
  gateways:
  - gateway.ns-ns
  hosts:
  - '*'
  http:
    - match:
      - uri:
          prefix: /my/context/path
      route:
        - destination:
            host: service1.ns-ns.svc.cluster.local
            port:
              number: 9000
    - match:        
      - uri:
          prefix: /my/context/path/service2
      route:
        - destination:
            host: service2.ns-ns.svc.cluster.local
            port:
              number: 9000

我不确定这是否可以实现,或者我是否需要将两个服务的上下文部分分开?

【问题讨论】:

    标签: kubernetes microservices istio istio-gateway istio-operator


    【解决方案1】:

    路由按顺序匹配。因此,您需要从最具体到最通用的开始。例如

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
      name: test-service
      namespace: ns-ns
    spec:
      gateways:
      - gateway.ns-ns
      hosts:
      - '*'
      http:
        - match:        
          - uri:
              prefix: /my/context/path/service2
          route:
            - destination:
                host: service2.ns-ns.svc.cluster.local
                port:
                  number: 9000
        - match:
          - uri:
              prefix: /my/context/path
          route:
            - destination:
                host: service1.ns-ns.svc.cluster.local
                port:
                  number: 9000
    

    【讨论】:

    • 重新排列订单工作,非常感谢,但如果我所有的服务的初始上下文路径相同('my/context/path'),我们不能在同一个前缀中定义多个路由吗?就像我分享的第一个 VS 一样。
    • 您可以使用不同的主机名。即 app1.example.com、app2.example.com 等。另一种选择,如果您可以修改客户端以发出以应用程序名称为前缀的请求,那么您可以重写路径。例如如果/app1/my/path 匹配,则重写到 /my/path 并转发到具有正确路径的服务器服务。我建议使用不同的主机。
    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 2019-12-21
    • 2015-06-03
    • 1970-01-01
    • 2010-11-09
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多