All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure header manipulation

Last Updated:Oct 11, 2023

You can add or remove request and response headers before forwarding requests or responses. If you want to modify request or response headers without modifying the application code, you can use a virtual service. This topic describes how to configure header manipulation.

Prerequisites

The preparations are completed, and the HTTPBin and sleep services are deployed. For more information, see Preparations.

Procedure

  1. Check whether the service configurations take effect.

    1. 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 -- sh
    2. Run the following command to send a request to the HTTPBin service:

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:50:24 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      x-envoy-upstream-service-time: 3
  2. Use the following content to configure a traffic rule for the HTTPBin service and add a header manipulation configuration. For more information, see Manage virtual services.

  3. apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: httpbin-vs
      namespace: default
    spec:
      hosts:
        - httpbin
      http:
        - route:
            - destination:
                host: httpbin
              headers:
                response:
                  add:
                    httpbin: hello
                  remove:
                    - x-envoy-upstream-service-time
  4. Check whether the configurations take effect.

    1. 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 -- sh
    2. Run the following command to send a request to the HTTPBin service:

      curl -I httpbin:8000

      Expected output:

      HTTP/1.1 200 OK
      server: envoy
      date: Fri, 11 Aug 2023 09:49:37 GMT
      content-type: text/html; charset=utf-8
      content-length: 9593
      access-control-allow-origin: *
      access-control-allow-credentials: true
      httpbin: hello

      The output indicates that the header configuration in the virtual service takes effect. The x-envoy-upstream-service-time field is deleted and the httpbin: hello field is added.