All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure a direct response

Last Updated:Oct 11, 2023

For a matched request, a service mesh proxy can directly return an HTTP response to the client according to the configured HTTP rule, instead of forwarding the request to the destination service. This topic describes how to configure a direct response.

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: istio-envoy
      date: Thu, 10 Aug 2023 09:32:09 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: 2
  2. Use the following content to create a virtual service and add a direct response 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.default.svc.cluster.local
      http:
        - directResponse:
            body:
              string: unknown error
            status: 503
  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 503 Service Unavailable
      content-length: 13
      content-type: text/plain
      date: Thu, 10 Aug 2023 09:33:52 GMT
      server: istio-envoy
    3. Run the following command to send a request to the HTTPBin service:

      curl httpbin:8000

      Expected output:

      unknown error