SunLynx
SunLynx

Reputation: 57

How to use Haproxy in Aws auto scaling instead of ELB?

How to use Haproxy in Aws auto scaling instead of ELB and also how to integrate existing aws instance setup into auto scaling.

Upvotes: 1

Views: 3930

Answers (3)

Filipe Felisbino
Filipe Felisbino

Reputation: 2902

Here's an article I wrote describing how to do it using one autoscaling group for the loadbalancer and lifecycle hooks to trigger a lambda function to update route53 upon ASG changes. Here are the 3 main steps:

1. Clone the aws-load-balancer repository

Most of the magic is handled by ansible so lets get that code:

git clone https://github.com/filipenf/aws-load-balancer.git

2. Edit the my-lb configuration file

Inside the aws-load-balancer directory, there's a vars/my-lb.yml file defining some variables. You'll need to edit that file and change some of them. Namely:

  • domain_name - the top-level domain name that will be used ( i.e: example.com )
  • auto_register_domain - FQDN that will be updated upon ASG's lifecycle hooks ( instances being created/deleted from the group )
  • subnets - the subnets that the loadbalancers will live
  • lb_mappings - the mapping between your domain names and backend instances

3. Execute the launch playbook

Now you're all set to execute the launch playbook which will create the necessary AWS resources for our load balancer to work.

ansible-playbook -i localhost, launch-lb.yml \
    -e server_config=mylb \
    -e aws_account_id=<your_aws_account_id> \
    -e key_name=<some_key>

If everything went well, you should have a new ASG with one instance. That instance will already have haproxy properly configured for the domains specified in the lb_mappings variable.

This playbook will also create the lambda function, SNS topic, lifecycle hooks and IAM roles needed for it to work.

The haproxy configuration is set to use sticky sessions and to synchronize the sticky-table with other ASG members. Additionally to that there's a script called "update-peers" that will detect changes to the autoscaling group and updated the peers configuration accordingly.

Upvotes: 4

Jhonatan Alarcon
Jhonatan Alarcon

Reputation: 339

I recomend haproxy v 1.6 or superior, because this version saves server state each time that you add or deregister instances to HAProxy with the following configuration:

global
stats socket /tmp/socket
server-state-file /tmp/server_state

backend bk
load-server-state-from-file global
server s1 10.0.0.3:4444 check weight 11
server s2 10.0.0.4:4444 check weight 12

Before reloading HAProxy, we save the server states using the following command:

socat /tmp/socket - <<< "show servers state" > /tmp/server_state

You can save:

  • Operational state
  • Administrative state (maint)
  • Weight (including slowstart relative weight)

More information What's new in HAProxy 1.6

You should have a script to adjust your HAProxy configuration in reaction to each autoscaling event like says @Julio.

Regards

Upvotes: 2

Julio Faerman
Julio Faerman

Reputation: 13501

You can add a notification to your auto scaling group. That will add a message to a SNS topic every time an auto scaling action occurs. You can subscribe an SQS queue to the topic and consume using a script to adjust your HAProxy configuration in reaction to each event.

Another option would be to add instances to the load balancer in the user-data script, but then you need to make sure the balancer can gracefully remove them if they disappear.

Upvotes: 2

Related Questions