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 路由规则。但是我无法让它运行。

指针将非常有用,因为睡眠服务返回的产品页面类似于 POSTMAN,而没有匹配条件的含义!

如果您没有定义子集(版本(的DestinationRule,则此虚拟服务本身将不起作用。

我将演示如何使用 0.8 版本中打包的 HelloWorld 示例来完成此操作:

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

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

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

您需要通过ingressgateway调用评论服务。如果您不这样做,有两种方法可能会遇到问题:

  1. 如果您正在调用产品页面 (curl <ingress url>/productpage -H "foo: bar"(,则没有任何逻辑可以将foo: bar标头从产品页面传播到评论服务。带有user-agent的示例之所以有效,是因为user-agent是自动传播的(特殊情况(。如果要使用foo: bar,则必须向产品页面服务添加逻辑以获取foo标头并将其发送到评论服务。

  2. 直接调用评论服务(例如,您为评论服务提供了一个节点端口(。这将失败,因为您的请求不是由 Istio 代理路由的,而是由 k8s 服务负载均衡器处理的。您需要调用 Istio 代理,例如ingressgateway

最新更新