Role: HAProxy in HA with Keepalived in Ubuntu

https://github.com/rcastells/ansible

This role is created to work with:

  • 2 LoadBalancers
  • 1 or more WebServers

First of all review your inventory file and create a group for loadbalancers and another one for webservers:

[loadbalancers]
 ha01 ansible_host=ec2-52-50-164-246.eu-west-1.compute.amazonaws.com
 ha02 ansible_host=ec2-52-48-101-228.eu-west-1.compute.amazonaws.com

[webservers]
 web01 ansible_host=ec2-52-50-143-215.eu-west-1.compute.amazonaws.com
 web02 ansible_host=ec2-52-31-14-253.eu-west-1.compute.amazonaws.com

Then, take a look at host_vars directory and inside the role at default vars:

host_vars

You must define for ha01 and ha02 which one will be the master and the VIP between servers:

keepalived_interval: 2
keepalived_debug: 2
keepalived_interface: eth0
keepalived_MASTER_SLAVE: MASTER (or SLAVE)
keepalived_priority: 101 (or 100)
keepalived_virtualip: 172.31.39.157 (your VIP)

default vars

You could need to change these vars.

---
# Set default haproxy installation package if not defined at host_vars
haproxy_url: http://www.haproxy.org/download/1.6/src/haproxy-1.6.7.tar.gz
haproxy_version: 1.6.7

Finally take a look at the initial playbook (haproxy-ha.yml):

---
# HAProxy servers
- hosts: webservers
become: yes

- hosts: loadbalancers
become: yes
roles:
- haproxy-ha

We want to connect to webservers just to gather their facts and then use them in haproxy.cfg template file.

To run the playbook you must run:

ansible-playbook -i <inventory_file>  haproxy-ha.yml -k -K -u <username>

As an example if you have ssh key connection to all servers you could run:

ansible-playbook -i inventory/production haproxy-ha.yml

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s