How to deploy HAProxy LoadBalancer over AWS in a dynamic way using Ansible Plugins?
Ansible is a very powerful tool for automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
In today’s demo, First I’m going to launch Instance over AWS using Ansible, then configure WebServer and LoadBalancer over those instances using Ansible Roles. It also fetches the webserver’s IP dynamically and updates it in the LoadBalancer config file dynamically. You don’t need to update the config file of HAProxy LoadBalancer as Ansible updates it dynamically.
Steps to Configure this whole Setup:
- Setup Dynamic Inventory for AWS Instance using Ansible Plugin.
- Launch EC2 Instance over AWS tagged for webserver and lbserver using Ansible.
- Setup Webserver on AWS instance tagged webserver using Ansible.
- Setup LoadBalancer on AWS instance tagged lbserver using Ansible and also update the webserver IP in loadbalancer config file dynamically.
- Finally run the playbook, which runs all the roles created using one playbook.
Before start, let’s first set up the local Ansible environment:
- You can use any Linux Distro for this, I’m using AMAZON Linux here…
- Upgrade pip version using below command first:
pip3 install --upgrade pip
- Having Ansible installed with Python3. I’m using pip command to install Ansible. After that, you need to create directory and file manually for the ansible config file and inventory file:
pip install ansible
- Setup Ansible config file and Inventory:
Press i and then write the below lines or do changes in the path according to your preference…
Write this in your config file and then Press ESC then :wq to save and exit the file.
- Now in same directory(/etc/ansible), create one directory for creating Ansible roles and one for ansible inventory file.
- Install Boto and Boto3 python library:
pip install boto
pip install boto3
- Need one IAM role in AWS because boto can automatically source my AWS API credentials provided by an Amazon EC2 Identity and Access Management (IAM) role to run the playbook.
We need to provide AWS credentials so that Ansible fetch the account details. For providing AWS credential, We have multiple Way…
One way, you’ll need to set environment variables for your Secret and Access key:
And another way is for providing Credential is by using awscli software. You only need to download software and provide AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY using command aws configure.
- Now our local environment setup is done.
Now let’s start with steps…
Step 1 - Setup Dynamic Inventory for AWS Instance using Ansible Plugins
Plugins are pieces of code that augment Ansible’s core functionality. Ansible uses a plugin architecture to enable a rich, flexible, and expandable feature set.
Ansible ships with a number of handy plugins, and you can easily write your own.
You can use
ansible-doc -t inventory -l to see the list of available plugins. Use
ansible-doc -t inventory <plugin name> to see plugin-specific documentation and examples.
We are going to use one of the Inventory Plugin here provided by Ansible for dynamically fetch IP over AWS.
Go to /etc/ansible/hosts directory and create one file named main_aws_ec2.yaml. Make sure your file name, as mentioned in the given documentation should end with aws_ec2.(yml/yaml).
Now our dynamic inventory setup is done.
Step 2 - Launch EC2 Instance over AWS tagged for webserver and lbserver using Ansible:
It is better to create roles in ansible for better management of code. To launch EC2 instances, I’m creating one role named ec2_host using command:
ansible-galaxy init <role_name>
This role creates key_pair and stores it for further use, creates security group for loadbalancer and webserver, and finally launched instance over AWS.
Step 3 - Setup Webserver on AWS instance tagged webserver using Ansible:
This role configures WebServer over instance launched in the above step. You only need to put your web pages in template directory of this role.
Step 4 - Setup LoadBalancer on AWS instance tagged lbserver using Ansible and also update the webserver IP in loadbalancer config file dynamically:
This ansible role helps in setup loadbalancer over EC2 instance for the webserver which we configured above.
In haproxy.cfg file, write below code in last which helps in updating the IP dynamically.
Step 5 - Create one playbook to run all the roles created above:
Finally, create one playbook to run these all roles…
Save this playbook as lb.yml and then run…
Finally, LoadBalancer is configured.✌
I’m uploading all the codes in GitHub Repo, you can use it as a reference…
Configure HAProxy LoadBalancer over AWS using Ansible Roles and Plugins - gaurav-gupta-gtm/ansible_haproxy
Now let’s see the output of this…
If I visit my loadbalaner URL, it go to different host each time.
Thanks for reading :)
Feel free to connect on linkedin…😊
Having any issue related to task, please DM me…