danih
June 25, 2018, 5:39pm
25
Baptiste, I tried to use multiple servers but the requests are not balanced evenly between the servers.
I built an example using Docker and docker-compose. The backend servers count each request they receive and print the number of requests received after 60 seconds.
Code:
app.js
const http = require('http');
const os = require('os');
let num = 0;
setTimeout(() => {
console.log(os.hostname(), num);
process.exit(0);
}, 60000);
This file has been truncated. show original
docker-compose.yml
---
version: '2'
services:
haproxy:
image: haproxy
ports:
- '80:80'
volumes:
- './haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg'
This file has been truncated. show original
haproxy.cfg
resolvers docker
nameserver default 127.0.0.11:53
hold valid 1000ms
frontend proxy
mode http
bind 0.0.0.0:80 maxconn 10000
option http-server-close
option forwardfor
timeout client 5000
This file has been truncated. show original
The output is
api_4 | f8e1414ea551 0
api_1 | 860eb040651c 0
api_2 | 6af96d901ea8 179
api_5 | 1f15abd0d461 60
api_3 | 271ae04ff5cc 60
One API server receives 180 requests, two receive 60 each and another two receive 0.
Is it possible to round robin to servers which were resolved with DNS?