[Header matching] Request header matching

Updated at:

When you send a request, you can include a custom field in the request header to distinguish requests and handle them differently.

Prerequisites

You have completed the preparations and deployed the helloworld service and the sleep service.

Procedure

  1. Verify that the service configuration takes effect.

    1. In the KubeConfig environment of the Container Service for Kubernetes (ACK) cluster, open a shell in the sleep pod.

      kubectl exec -it deploy/sleep -- sh
    2. Send a request to the helloworld service.

      curl helloworld:5000/hello

      The expected output is one of the following two because helloworld-v1 and helloworld-v2 respond randomly:

      Hello version: v2, instance: helloworld-v2-6b96c5684-4****
      Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****
  2. Use the following content to configure a destination rule. For more information, see Manage destination rules.

    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. Use the following content to create a virtual service and add the Header Matching configuration. For more information, see Manage virtual services.

    The following configuration specifies an exact match on the test-header request header. When the value is v1, traffic is sent to helloworld-v1. When the value is v2, requests are sent to helloworld-v2.

    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
  4. Verify that the configuration takes effect.

    1. In the KubeConfig environment of the ACK cluster, open a shell in the sleep pod.

      kubectl exec -it deploy/sleep -- sh
    2. Send a request with the test-header: v1 request header to the helloworld service.

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

      Expected output:

      Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p****
    3. Send a request with the test-header: v2 request header to the helloworld service.

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

      Expected output:

      Hello version: v2, instance: helloworld-v2-6b96c5684-4****