Deploy HAProxy LoadBalancer in AWS using Ansible in 30 seconds

Gaurav Gupta
3 min readSep 9, 2020

In this article, we are going to deploy HA-Proxy LoadBalancer on EC2-instance using Ansible.

Why Ansible…?

Ansible automates and simplifies repetitive, complex, and tedious operations. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers.

I create Ansible-Roles for launching EC2 instance and deploy webserver and LoadBalancer Service using the Dynamic-Inventory concept.

If you don’t know how to set up Dynamic inventory for AWS, please refer to my previous article.

So Let’s Start…

For this task, I’m creating three Ansible-roles…

  1. For Launch AWS EC2 instances.
  2. For launching Apache Webserver
  3. And one more for HA-Proxy LoadBalancer

To create an Ansible-role, First create a directory /etc/ansible/roles and after that run command…

ansible-galaxy init role_name

1. Launch AWS EC2 instance:-

Code for launching the EC2 instance is below, I’m created 3 hosts for Webserver and 1 for LoadBalancer…

By Default, Ansible doesn’t refresh the inventory in the middle of the running playbook, so we use the meta keyword refresh_inventory in the last of this code.

So, Ec2-Instance is launched…

2. Launch Webserver On EC2 instance:

Now I need to install httpd software on EC2 instance named webserver using Ansible

After running this role, my webserver is configured.

3. Configure HA-Proxy LoadBalancer:

Now I need to configure my load-balancer service on ec2 instance named lbserver.

Now, all roles are created successfully.

Now I created one playbook for running all roles in a single click…

Save this playbook as lb.yml and then run…

ansible-playbook lb.yml

Finally, LoadBalancer is configured in a single command…✌

Let’s see the output of this…

So let’s run my LoadBalancer IP and see it is working or not…

Yo… It’s working :)

GitHub Link:

Please clap, if you found it useful…❣

Connect me on LinkedIn for any suggestion or query…You can find some interesting articles there too…👍

Happy Learning :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Gaurav Gupta
Gaurav Gupta

Written by Gaurav Gupta

Tech Enthusiasts, Passion to learn and share

No responses yet

Write a response