CREATE A LOADBALANCER WITH HAPROXY ON AWS USING ANSIBLE




HAproxy

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

Ansible

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.


This story demonstrates how one can create a loadbalancer with HAproxy on AWS using Ansible.

Pre-requisites:

  1. Ansible

  2. AWS Account



Process

The first and foremost task is to create a playbook that will bind the processes of creating an instance, and deploying the load balancer web servers. I have used the concept of roles in Ansible for easier management of the whole process. I have also used vaults for the important credentials that I will be requiring for the task.

Configuration file for ansible


ansible-playbook


- hosts: localhost
  roles:
          - role: launch1- hosts: tag_Name_LoadBalancer
  remote_user: ec2-user
  become: yes
  become_user: root
  roles:
          - role: lbserver- hosts: tag_Name_Webserver
  remote_user: ec2-user
  become: yes
  become_user: root
  roles:
          - role: webserver

Now lets discuss about different parts of the playbook.

Creating the instances:

1. Each playbook requires one or more hosts where the task is going to be carried.

- hosts: localhost
  roles:
          - role: launch1

This segment selects localhost as the host and uses the role launch1 to create the instances for the loadbalancer and web servers.