[Header matching] Request header matching
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
-
Verify that the service configuration takes effect.
-
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 -
Send a request to the helloworld service.
curl helloworld:5000/helloThe 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****
-
-
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 -
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-headerrequest header. When the value isv1, traffic is sent to helloworld-v1. When the value isv2, 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 -
Verify that the configuration takes effect.
-
In the KubeConfig environment of the ACK cluster, open a shell in the sleep pod.
kubectl exec -it deploy/sleep -- sh -
Send a request with the
test-header: v1request header to the helloworld service.curl -H "test-header:v1" helloworld:5000/helloExpected output:
Hello version: v1, instance: helloworld-v1-6d77f4c4cf-p**** -
Send a request with the
test-header: v2request header to the helloworld service.curl -H "test-header:v2" helloworld:5000/helloExpected output:
Hello version: v2, instance: helloworld-v2-6b96c5684-4****
-