Creating Load Balancer setup on AWS using Ansible

Tirth Patel
3 min readDec 25, 2020

--

Let’s start with understanding what Load Balancer actually is. As the name suggest and you can understand balancing the load on the server. So here, if we have traffic coming continuously and there is load on server 1 so in this condition load balancer will transfer some client to another server using Round Robin Algorithm for traffic distribution.

To achieve this setup, follow below steps.

Step1 : Lets create the group of host by which it will be easier to write code and execute it.

Step2 : Let’s configure the global configuration file.

As in AWS, we can only do ssh with private key, also we can only login with ec2-user so we have to provide privilege escalation as root user to the ec2-user so that we can install different software. For this, we have to specify the path to key in conf file and provide privilege escalation details along with remote user as ec2-user.

Step3 : Below is the code to setup haproxy and backend webservers.

Step4 : Create a simple php page.

<pre>
<?php
print `/usr/sbin/ifconfig`;
?>
</pre>

Step5 : Configure the haproxy.yml file. Add the IP from inventory using Jinja Templating.

Step6 : Run the playbook

ansible-playbook <playbook_name>.yml

Step7 : Check whether it is working fine or not.

Refresh it, and you will notice now it is redirecting to another server.

So, it is working fine.

Thank You!

--

--

No responses yet