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.
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 HTTPBin service:
curl -I httpbin:8000Expected 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
Use the following content to create a virtual service and add a direct response configuration. For more information, see Manage virtual services.
Check whether the configurations take 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 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 error
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