Configuration of load balancer using haproxy on aws cloud with the help of ansible.
In this task we will configure load balancer using haproxy and backend servers will be configured as webserver which are ec2 instances using ansible.
What is load balancer and how it works?
Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.
For load balancing we use haproxy.
What is ansible?
Ansible works by connecting to your nodes and pushing out small programs, called "Ansible modules" to them. These programs are written to be resource models of the desired state of the system. Ansible then executes these modules (over SSH by default), and removes them when finished.
What is 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. It is written in C and has a reputation for being fast and efficient (in terms of processor and memory usage)
Steps:
Step 1:
Deploy EC2 instance
Here deploy ec2 instance using ec2 module the values for parameters in module are taken from variable file.
Here set count to 3 which will launch 3 ec2 instances.
ec2.yml:
aws_vars.yml:
aws_cred.yml is vault containing access key and secret key of aws account.
vault creation:
aws_cred.yml:
In this file provide credentials
And now you can run ansible playbook.
Three ec2 instances will be launched.
Step 2:
Creation of Dynamic inventory
What is dynamic inventory?
As described in Working With Dynamic Inventory, Ansible can pull inventory information from dynamic sources, including cloud sources. You can also create a new dynamic inventory provider by creating a script or program that can output JSON in the correct format when invoked with the proper arguments.
Use dynamic inventory for ec2 instances which will automatically tell us about ec2 instances launched. These inventories are available on ansible community.
Download those files using wget command from github.
And set their path as inventory in ansible config file.
Change python version from ec2.py according to version of pyhton on your VM.
And export ansible credentials variables.
You can see ec2 instances using list hosts option of ansible.
Step 3:
Configure EC2 instance as webserver using role.
Set path of roles in ansible configuration file.
Roles:
tasks/main.yml:
Apply the role using yml file.
Webservers are configured.
Step 4:
Configure localhost as loadbalancer by applying created role.
lb/tasks/main.yml:
templates/haproxy.cfg:
This file is config file of haproxy the changes are done for bind port and set loop for backend servers (Here are ec2 instances)
What is bind?
A bind setting assigns a listener to a given IP address and port. The IP can be omitted to bind to all IP addresses on the server and a port can be a single port, a range, or a comma-delimited list. You’ll often use the ssl and crt arguments to instruct HAProxy to manage SSL/TLS terminations, rather than having your web servers doing that.
-Apply the loadbalancer role using yml file
Loadbalancer is configured.
Output:
Here you can see load balancing is achieved.
Thank you for reading!!
SDE I at Hewlett Packard Enterprise
4 年Nice