Envoy Proxy provides a range of filters and plugins that can be used to customize its behavior to meet the specific needs of your application. One useful filter is envoy.filters.network.direct_response, which can be used to instruct Envoy to reply with a static response and fixed HTTP response code. This can be useful in situations where you need to return a response without forwarding the request to the intended service, such as for health checks or service monitoring. With this filter, you can easily configure Envoy to return a predefined response with a fixed HTTP status code, simplifying the management of your application and reducing the load on your services.
Let’s create an Envoy container with a direct response filter, add the direct_response filter to the network filter chain, and configure it to return the desired status response.
$ mkdir envoy $ cd envoy
Create an Envoy configuration file called envoy-fixed-response-1.yaml with the contents provided below.
static_resources: listeners: - name: listener_0 address: socket_address: address: 0.0.0.0 port_value: 8000 filter_chains: - filters: - name: envoy.filters.network.http_connection_manager typed_config: "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager stat_prefix: envoy-fixed-response-1 http_filters: - name: envoy.filters.http.router typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router route_config: name: route-1 virtual_hosts: - name: direct_response_service domains: ["*"] routes: - match: prefix: "/" direct_response: status: 200 body: inline_string: "This is a direct response from envoy-fixed-response-1 container"
Create a Docker compose configuration file called docker-compose.yaml with the contents provided below.
version: '3' services: envoy-fixed-response-1: image: envoyproxy/envoy:v1.25.1 container_name: envoy-fixed-response-1 ports: - "8000:8000" volumes: - ./envoy-fixed-response-1.yaml:/etc/envoy/envoy.yaml networks: envoy: ipv4_address: 10.10.0.11 networks: envoy: driver: bridge ipam: config: - subnet: 10.10.0.0/16
Start the Docker container.
$ docker-compose up -d
You should be able to see a fixed response being returned from Envoy.
$ curl localhost:8000 This is a direct response from envoy-fixed-response-1 container