gRPC Load Balancer
grpc_load_balancer
is a Python package that allows you to easily configure and manage separate gRPC server and client instances using a metrics-based load balancing approach. This is useful when you have multiple services running and want to distribute the load efficiently among them. The package includes a connection forwarder, a metrics-based server finder, and a configuration loader.
It implemetns the following design:
Features
- Forward connections from clients to gRPC servers.
- Use Prometheus metrics to select the best server to handle a request.
- Configure and manage gRPC server finder using environment variables.
Installation
You can install grpc_load_balancer
using pip:
pip install grpc_load_balancer
Install promethus-client:
pip install prometheus-client
Usage
Configuring and running a gRPC server
Here's a simple example of how to use grpc_load_balancer with a gRPC server and prometeus-client:
Import the required modules:
import grpc
from grpc_load_balancer import GrpcConnnectionForwarder
from prometheus_client import start_http_server, Gauge
Initialize your gRPC server(s)
grpc_server = create_example_grpc_server()
connection_counter = Gauge('connections_num', 'Number of connections forwarded')
forwarder = GrpcConnnectionForwarder(
grpc_server, callback=lambda value: connection_counter.set(value)
)
start_http_server(prometheus_port)
forwarder.serve(host="0.0.0.0", port=forwarder_port)
Configuring and running a gRPC client
Import the required modules:
import grpc
from grpc_load_balancer import EnvConfigLoader, MetricsBasedServerFinder
Initialize the configuration loader
env_config_loader = EnvConfigLoader(prefix="LB")
Initialize the server finder, find the best server and connect to it
metrics_based_server_finder = env_config_loader.init_metrics_based_server_finder()
best_host = metrics_based_server_finder.fetch_metrics()
channel = grpc.insecure_channel(f"{best_host[0]}:{best_host[1]}")
client = create_example_grpc_client(channel)
Troubleshooting
If you have any problems with grpc_load_balancer
, please open an issue on GitHub.
Also, you can check tests/test_grpc_load_balancer.py for more information.
Contributing
We welcome contributions to grpc_load_balancer
. If you find a bug or want to propose a new feature, please open a GitHub issue or submit a pull request.
License
This project is licensed under the MIT License. See LICENSE for details.