首页 > 解决方案 > Istio VirtualService HTTP 标头匹配问题

问题描述

以下 Istio 0.8 VirtualService 无法匹配 HTTP 标头。

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - match:
    - headers:
        foo:
          exact: bar
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v3

我遵循了https://github.com/istio/issues/issues/38并且基于标头用户代理的 Istio RouteRule 不起作用。但是我无法让它发挥作用。

指针会非常有用,因为 sleep 服务会返回类似于 POSTMAN 的产品页面,而不会暗示匹配条件!

标签: istio

解决方案


如果您没有DestinationRule定义子集(版本),则此 VirtualService 本身将不起作用。

我将演示如何使用 0.8 版本中包含的 HelloWorld 示例来完成它:

第 1 步:部署samples/helloworld/helloworld.yaml

第 2 步:DestinationRule为两个可用版本定义 a :

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: helloworld
spec:
  host: helloworld
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

第 3 步:将默认值替换为VirtualService与路由的标头属性匹配的值:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: helloworld
spec:
  hosts:
  - "*"
  gateways:
  - helloworld-gateway
  http:
  - match:
    - headers:
        foo:
          exact: bar
    route:
    - destination:
        host: helloworld
        subset: v2
  - route:
    - destination:
        host: helloworld
        subset: v1

第4步:测试它:

  • 没有标题:curl http://$INGRESS_GATEWAY/hello

    输出:

    你好版本:v1,实例:helloworld-v1-fd9b784bb-wcnj9

  • 带标题:curl -H "foo: bar" http://$INGRESS_GATEWAY/hello

    输出:

    你好版本:v2,实例:helloworld-v2-56694b7d6d-gbhqb


推荐阅读