Connection pools for HTTP
Set up connection pool settings for an HTTP destination, such as the maximum number of HTTP connections that can be opened with the destination.
About
Connection pools are typically set up for apps that must be accessed frequently, such as a web service. Without connection pools, every request to the web service requires a new connection to be opened. This setup might work if the web service is accessed occasionally. However, as clients scale and more requests are sent to the web service, opening a connection for each requests can get very expensive and easily overload the web service.
To prevent the upstream web service from being overloaded, you can use the following connection pool settings:
- Maximum number of requests
- Maximum number of requests per connection
- Maximum number of retries
- Maximum number of pending requests
For more information, see the Gloo connection policy API docs.
If you import or export resources across workspaces, your policies might not apply. For more information, see Import and export policies.
Before you begin
This guide assumes that you use the same names for components like clusters, workspaces, and namespaces as in the getting started. If you have different names, make sure to update the sample configuration files in this guide.
- Set up Gloo Mesh Gateway in a single cluster.
- Install Bookinfo and other sample apps.
Configure an HTTP listener on your gateway and set up basic routing for the sample apps.
Configure connection policies for HTTP destinations
You can apply a connection policy at the destination level. For more information, see Applying policies.
This policy currently does not support selecting ExternalServices as a destination.
The following example applies HTTP connection pool settings to the Kubernetes service of the Bookinfo ratings app.
apiVersion: resilience.policy.gloo.solo.io/v2
kind: ConnectionPolicy
metadata:
annotations:
cluster.solo.io/cluster: ""
name: http-connect
namespace: bookinfo
spec:
applyToDestinations:
- selector:
labels:
app: ratings
config:
http:
idleTimeout: 2s
maxRequestsPerConnection: 1
maxRetries: 1
maxRequests: 500
maxPendingRequests: 500
Review the following table to understand this configuration. For more information, see the API docs.
Setting | Description |
---|---|
applyToDestinations | Use labels to apply the policy to destinations. Destinations might be a Kubernetes service, VirtualDestination, or ExternalService (if supported by the policy). If you do not specify any destinations or routes, the policy applies to all destinations in the workspace by default. If you do not specify any destinations but you do specify a route, the policy applies to the route but to no destinations. |
config | Configure the connection settings to apply to the selected destinations. To set connection pool settings for HTTP destinations, use http as the protocol. For TCP connection pool settings, use tcp . The connection policy in this guide shows how to configure connection pool settings for an HTTP destination. To find an example for a TCP connection policy, see Connection pool settings for TCP. |
http.idleTimeout | The time a connection can stay open without receiving any requests. By default, this value is set to 1 hour. If the idle time is reached, the connection is closed. |
http.maxRequestsPerConnection | The maximum number of requests that can be sent to a destination per connection. If you set this value to 1, you disable keep alive. By default, this value is set to 0, which equals unlimited , and allows a maximum of 2^29 requests per connection. |
http.maxRetries | The maximum number of retries that can be outstanding to all hosts in a cluster at a given time. Defaults to 2^32-1. |
Verify connection policies
Apply the example connection policy for the ratings app.
kubectl apply -f- <<EOF apiVersion: resilience.policy.gloo.solo.io/v2 kind: ConnectionPolicy metadata: annotations: cluster.solo.io/cluster: "" name: http-connect namespace: bookinfo spec: applyToDestinations: - selector: labels: app: ratings config: http: idleTimeout: 2s maxRequestsPerConnection: 1 maxRetries: 1 maxRequests: 500 maxPendingRequests: 500 EOF
Verify that an Istio destination rule is created for the destination.
kubectl get destinationrule --context $REMOTE_CONTEXT1 -n bookinfo
Example output:
NAME HOST AGE ratings-global-virtual-destinat-2ab46384c8b40be3cfab9740ac8fb2c ratings.global 11s
Describe the Istio destination rule.
kubectl describe --context $REMOTE_CONTEXT1 -n bookinfo destinationrule <destination-rule>
In the output, verify that the Connection Pool settings include the http settings that your policy configures.
... Traffic Policy: Port Level Settings: Connection Pool: Http: http1MaxPendingRequests: 500 http2MaxRequests: 500 Idle Timeout: 2s Max Requests Per Connection: 1 Max Retries: 1
Cleanup
You can optionally remove the resources that you set up as part of this guide.
kubectl -n bookinfo delete ConnectionPolicy http-connect