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
Check whether the service configurations take effect.
In the kubeconfig environment for the ACK cluster, run the following command to start a bash session in the sleep environment.
kubectl exec -it deploy/sleep -- shRun the following command to send a request to the HTTPBin service:
curl -I httpbin:8000Expected output:
HTTP/1.1 200 OK Server: gunicorn/19.9.0 Date: Mon, 28 Aug 2023 06:15:30 GMT Connection: keep-alive Content-Type: text/html; charset=utf-8 Content-Length: 9593 Access-Control-Allow-Origin: * Access-Control-Allow-Credentials: true
Use the following content to configure a traffic rule for the HTTPBin service and add a direct response configuration. For more information, see Manage virtual services.
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: 503Check whether the direct response configuration takes effect.
In the kubeconfig environment for the ACK cluster, run the following command to start a bash session in the sleep environment.
kubectl exec -it -n default deploy/sleep -- shRun the following command to send a request to the HTTPBin service:
curl -I httpbin:8000Expected 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-envoyRun the following command to send a request to the HTTPBin service:
curl httpbin:8000Expected output:
unknown errorThe output results indicate that the direct response configuration takes effect.