ANSIBLE : Configuration of Haproxy


 

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.


Its architecture is simple and effective. It works by connecting to your nodes and pushing small programs to them. These programs make the system comply with a desired state, and, when they have finished their tasks, they are deleted.


To know more about ansible, check my this blog :  ANSIBLE : How Industries are solving Challenges using Ansible


HAPROXY

HAProxy is an open source, free, very fast and reliable solution offering high availability, load balancing and proxying for TCP and HTTP-based applications. 
It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. It distributes a workload across a set of servers to maximize performance and optimize resource usage.

HAProxy can be used for Web applications (HTTP/ HTTPS) as well as for TCP based applications (MySQL, SSL, SMTP) etc.


  1. reverse proxy is a type of proxy server that requests network resources on behalf of external clients.
  2. forward proxy is a type of proxy server that requests network resources on behalf of internal clients.

LOAD BALANCER

A load balancer can be deployed as software or hardware to a device that distributes connections from clients between a set of servers. A load balancer acts as a ‘reverse-proxy’ to represent the application servers to the client through a virtual IP address (VIP). This technology is known as server load balancing(SLB). SLB is designed for pools of application servers within a single site or local area network (LAN).


Load balancers are used to provide availability and scalability to the application. The application can scale beyond the capacity of a single server. The load balancer works to steer the traffic to a pool of available servers through various load balancing algorithms. If more resources are needed, additional servers can be added.

Because the load balancer is sitting in between the client and application server and managing the connection, it has the ability to perform other functions. The load balancer can perform content switching, provide content-based security like web application firewalls (WAF), and authentication enhancements like two factor authentication (2FA).


CONFIGURATION OF HAPROXY 

USING ANSIBLE


Lets configure Reverse Proxy that is Haproxy using ansible Playbook.

Check this blog to know how to configure HTTPD on the top of docker container using Ansible playbook : ANSIBLE : Apache Web Server on DOCKER Container using Ansible


As every server has a hard coded limit. At one point in time, only limited clients can connect to the web server. If more clients try to connect, the will get some error. That's why we are using Load Balancer. 

Here we want to achieve high availability and extra security for our web server using haproxy.

But the problem is that, if we need to add more servers, we can add them by adding their IP in inventory file and run the playbook.
But how Load balancer will come to know that more servers have been added????

For this we will have to manually update the configuration file of Haproxy. And manually updating is not good as we are using ansible.

So the solution is, we can use JINJA language to write code in configuration file. The best thing about this language is we can write some code in between the texts. We can use template module for uploading configuration file of Haproxy.

So my configuration file for Haproxy in playbook is :


Here you can see i have used for loop in jinja to upload ip of webservers.

This is my ansible playbook code :






This is the output I got after running the playbook. 
Here you can see no red colored output come up. That means there is no error in the code. 



And this is my web page code in php :



After running the playbook, we can check in load balancer:

Haproxy has been installed .
And its running on port number 8080



Configuration file in load balancer :
You can see ip is updated in con file of haproxy.

Here i have used only one system as web server, but you can choose any number of webservers.
This ansible playbook will run for multiple servers.


In backend server you can see httpd has been installed and service started.
Our webpage is uploaded.



Using "ifconfig" command , we can check the ip of the system. 
Here ip of my system is 192.168.43.176 and webpage is running on port number 80.


Now to check the load balancer is working properly or not, search for <ip>:<port> of load balancer on any browser .


Here you can see my Load balancer is working. I'm getting ip of my backend server that means this webpage is running on my backend server.


Hope you like this..


Comments

Popular posts from this blog

Automation Using Python

NETWORK TOPOLOGY

Chat Server using UDP