amazon-ec2load-balancinghaproxyscalr

What's the best practice for getting EC2 instances to join HAProxy automatically?


We're working on scaling out our EC2 architecture to a point where we'd like to manage our own load balancing. We currently have a series of machines configured on HAProxy to do basic load balancing, but we're looking for the 'best practice' means to have a new instance come online and automatically (or nearly automatically) join HAProxy.

Ideally, we'd monitor load on our systems or rely on a few years worth of analytics data to work out a rouch schedule, and when we reach a threshold or scheduled time, have a process fire up a new instance, have that new node connect to a system on our HAProxy machine to write its hostname into the config and reload HAProxy so it becomes part of the pool.

We're considering Amazon's ELB once we grow big enough to need multiple zone coverage, but until then, we need a simple setup that can add/remove machines from HAProxy.

I know there are services out there that we can pay to manage this stuff, but Scalr seems to limit us to very specific instance types, and Rightscale is too expensive, so like many others, we're looking to roll our own solution.

Unfortunately, those who roll their own solution seem to be a little hush-hush on their process.


Solution

  • You don't need to over-think this solution ;)

    You can simply "pre-configure" servers in your HAProxy configuration file. They will appear "down" and will never receive requests until you actually bring them online.

    Here's an example, assuming you only have 5 machines online, and expect to have 10 in the next 2 years:

    listen web *:80
        balance source
        server  web1 192.168.0.101:80 check inter 2000 fall 3
        server  web2 192.168.0.102:80 check inter 2000 fall 3
        server  web3 192.168.0.103:80 check inter 2000 fall 3
        server  web4 192.168.0.104:80 check inter 2000 fall 3
        server  web5 192.168.0.105:80 check inter 2000 fall 3
        server  web6 192.168.0.106:80 check inter 2000 fall 3
        server  web7 192.168.0.107:80 check inter 2000 fall 3
        server  web8 192.168.0.108:80 check inter 2000 fall 3
        server  web9 192.168.0.109:80 check inter 2000 fall 3
        server  web10 192.168.0.110:80 check inter 2000 fall 3
    

    With this config, you won't need to restart HAProxy or do any kind of ugly hacks for at least a year (unless you need more than 10, then just add 100 and you'll be set).

    You can also write a quick shell script to automatically generate this configuration, actually you SHOULD write a script for that if you're adding 100 servers to your pool.