文档

【Header Matching】请求头匹配

发送请求时,可以在请求Header中携带自定义字段,以区分请求并做不同的处理。本文介绍如何配置请求头匹配。

前提条件

已完成准备工作,并部署helloworld服务和sleep服务。具体操作,请参见准备工作

操作步骤

  1. 验证服务配置是否生效。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令,向helloworld服务发送请求。

      curl helloworld:5000/hello

      预期输出为以下两者之一,helloworld-v1和helloworld-v2随机响应。

      Hello version: v2, instance: helloworld-v2-6b96c5684-4****
      Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****
  2. 使用以下内容,配置目标规则。具体操作,请参见管理目标规则

    apiVersion: networking.istio.io/v1beta1
    kind: DestinationRule
    metadata:
      name: helloworld
      namespace: default
    spec:
      host: helloworld
      subsets:
        - labels:
            version: v1
          name: v1
        - labels:
            version: v2
          name: v2
    
  3. 使用以下内容,创建虚拟服务,添加Header Matching配置。具体操作,请参见管理虚拟服务

    以下配置表示精确匹配test-header请求头,值为v1时流量发往helloworld-v1,值为v2时请求发往helloworld-v2。

  4. apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: helloworld-vs
      namespace: default
    spec:
      hosts:
        - helloworld
      http:
        - match:
            - headers:
                test-header:
                  exact: v1
          route:
            - destination: 
                host: helloworld
                subset: v1
        - match:
            - headers:
                test-header:
                  exact: v2
          route:
            - destination: 
                host: helloworld
                subset: v2
  5. 验证配置是否生效。

    1. 在ACK集群对应的KubeConfig环境下,执行以下命令,进入sleep环境开启bash。

      kubectl exec -it deploy/sleep -- sh
    2. 执行以下命令,向helloworld服务发送带有test-header: v1请求头的请求。

      curl -H "test-header:v1" helloworld:5000/hello

      预期输出:

      Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****
    3. 执行以下命令,向helloworld服务发送带有test-header: v2请求头的请求。

      curl -H "test-header:v2" helloworld:5000/hello

      预期输出:

      Hello version: v2, instance: helloworld-v2-6b96c5684-4****
  • 本页导读 (1)
文档反馈