haproxy_cadvisor.py is a small utility, created to set backend servers weight on HAProxy based on CPU utilization data from cAdvisor.
This utility will adjust servers balancing weight to make the load on all servers in given backend equal.
You can read more about it in my blog (in Russian).
First, you need to install Python requests lib:
pip install requests
# or apt-get install python-requests on Debian/Ubuntu
Then clone this repo and set up initial config
git clone https://github.com/abulimov/haproxy-cadvisor
cd haproxy-cadvisor
cp config.json.example config.json
Then you have to edit config.json, and change some parameters as described in Configuration section.
Your HAProxy must have admin socket configured, it is done with settings like this in haproxy.cfg:
global
stats socket /var/run/haproxy.sock level admin
More info about configuring HAProxy socket is in official documentation.
Last step is configuring cron task - type crontab -e
as
user that can write to HAProxy socket and
add something like this:
* * * * * /usr/bin/python /path/to/haproxy_cadvisor.py /path/to/config.json > /dev/null
Don't forget to change path to haproxy_cadvisor.py and config.json to real values.
All configuration is done in plain JSON. You can find example configuration in config.json.example.
parameter | type | description |
---|---|---|
haproxy_socket | string | socket for communication with HAProxy |
backend | string | name of HAProxy backend we will control |
pattern | string | regexp to match container aliases we get from cAdvisor and HAProxy server names |
urls | list of strings | list of URLs we'll use to access cAdvisor API |
As you can see, you'll have to change at least urls, backend and pattern to match your environment.
Set up the cron task and watch backend servers weight adjust as the load to servers change.
Licensed under the MIT License.