[Direct Response] Direct Response

Updated at:

For a request that matches, the HTTP rule can route a direct response, which specifies the fixed response to be sent to the client. That is, an HTTP Response is returned directly to the client instead of being handed over to the application for processing. This topic describes how to configure a direct response.

Prerequisites

You have completed the Preparations and deployed the httpbin service and the sleep service. For instructions, see Preparations.

Procedure

  1. Verify that the service configuration takes effect.

    1. In the KubeConfig environment that corresponds to the ACK cluster, run the following command to enter the sleep environment and start bash.

      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 the Direct Response configuration. For instructions, 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. Verify that the configuration takes effect.

    1. In the KubeConfig environment that corresponds to the ACK cluster, run the following command to enter the sleep environment and start bash.

      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