You can include custom fields in the header of a request to distinguish requests and perform different processing. This topic describes how to configure header matching.
Prerequisites
The preparations are completed, and the helloworld and sleep services are deployed. For more information, see Preparations.
Procedure
Check whether the service configurations take effect.
Use kubectl to connect to the Container Service for Kubernetes (ACK) cluster based on the information in the kubeconfig file, and run the following command to enable bash for the sleep service:
kubectl exec -it deploy/sleep -- shRun the following command to send a request to the helloworld service:
curl helloworld:5000/helloThe expected output is that helloworld-v1 or helloworld-v2 randomly responds to the request.
Hello version: v2, instance: helloworld-v2-7d48f6b995-6**** Hello version: v1, instance: helloworld-v1-7567d85fd7-d****
Use the istioctl tool to run the following commands to deploy a Layer 7 waypoint proxy for the helloworld service:
istioctl x waypoint apply --service-account helloworld-v1 istioctl x waypoint apply --service-account helloworld-v2Use the following content to create 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: v2Use the following content to create a virtual service and add a header matching configuration. For more information, see Manage virtual services.
The following configuration indicates that the
test-headerrequest header is exactly matched. If the value isv1, the request is sent to helloworld-v1. If the value isv2, the request is 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: v2Check whether the header matching configuration takes effect.
Use kubectl to connect to the ACK cluster based on the information in the kubeconfig file, and run the following command to enable bash for the sleep service:
kubectl exec -it deploy/sleep -- shRun the following command to send a request with the header
test-header: v1to the helloworld service:curl -H "test-header:v1" helloworld:5000/helloExpected output:
Hello version: v1, instance: helloworld-v1-7567d85fd7-d****Run the following command to send a request with the header
test-header: v2to the helloworld service:curl -H "test-header:v2" helloworld:5000/helloExpected output:
Hello version: v2, instance: helloworld-v2-7d48f6b995-6****The preceding results indicate that the header matching configuration takes effect.