Deploy HAProxy LoadBalancer in AWS using Ansible in 30 seconds

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 :)




Tech Enthusiasts, Passion to learn and share

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Adding Reflection Probes in Unity

Getting started with Awesome Signals on Profit Trailer

Interview with EMPEX LA Speaker Pete Gamache

Data is Beautiful: DataCamp

How to Solve a 1 kyu Kata on Codewars (or any difficult programming exercise)

Qlik Data Visualization Extension: Introducing Custom Drawing Over Custom Theme

Top 10 DevOps Things to Be Grateful for in 2022

Protobuf Lab Session

Get the Medium app

Gaurav Gupta

Gaurav Gupta

Tech Enthusiasts, Passion to learn and share

More from Medium

Coursera-clone with Backend

DB migration using AWS DMS Service

About Kubernetes architecture 3; Debugging

Install Confluent Kafka on AWS EC2