HOW TO CREATE OUR OWN LOAD BALANCER BY HAPROXY USING ANSIBLE.
If you are looking to know HOW WE CREATE OUR OWN LOAD BALANCER OF WEB SERVER BY CONFIGURING REVERSE PROXY USING HAPROXY USING ANSIBLE, then WELCOME OF YOU. You are in the right blog.
Directly going deep-dive into Practical Part, let’s first discuss about WHAT IS ANSIBLE, LOAD BALANCER, REVERSE PROXY, HAPROXY, etc.
1. WHAT IS ANSIBLE?
Ansible was written by Michael DeHaan and acquired by RED HAT in 2015. Ansible is an open source community project sponsored by RED HAT, it’s the simplest way to automate IT. Ansible is the only automation language that can be used across entire IT teams from systems and network administrators to developers and managers to automate lifecycle- from servers to clouds to containers and everything in between. It is only used for CONFIGURATION MANAGEMENT. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers. Ansible have Two Types of Nodes-
CONTROLLER NODE- The System where we have to Install Ansible.
TARGET OR MANAGED NODE- The System which we have to Configure or Manage.
In this practical we will have 4 Nodes, 1 Controller Node and 3 Target Nodes respectively.
It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own DECLARATIVE LANGUAGE to describe system configuration means it is that much Intelligence that we just only have to tell Ansible that WHAT IT HAVE TO BE DO in Target Node or Managed Node.
2. WHAT IS LOAD BALANCER?
A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications. They improve the overall performance of applications by decreasing the burden on servers associated with managing and maintaining application and network sessions, as well as by performing application-specific tasks. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.
Load balancers ensure reliability and availability by monitoring the “health” of applications and only sending requests to servers and applications that can respond in a timely manner.
3. WHAT IS REVERSE PROXY?
A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client, appearing as if they originated from the reverse proxy server itself. A reverse proxy is an intermediary for its associated servers to be contacted by any client. In other words, a proxy is associated with the client(s), while a reverse proxy is associated with the server(s); a reverse proxy is usually an internal-facing proxy used as a ‘front-end’ to control and protect access to a server on a private network.
It acts as a Web Server for a client, but internally it is only a Proxy of many web servers. Client can’t know from where the current page is come and from which IP it comes.
4. 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 this practical, we will use HAProxy Software, to create our own Load Balancer.
Finally, we understand some Basic concepts that we will use in this practical. So, let’s move towards our Practical.
5. Ansible playbook to Configure Reverse Proxy i.e. HAProxy and How to update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.
This is the task that we have to complete in this blog.
STEP-1:
We have our Controller node where we have Ansible already installed.
We are using ansible 2.10.3 version of Ansible in 118 IP.
To install ansible you will use the following command-
“pip3 install ansible”.
STEP-2:
Now, we have to create one inventory file of Ansible from where Ansible pick up the IP’s of our Target Node and some more information like USER, PASSWORD, CONNECTION etc. Command to create inventory file-
“vim inv.txt”.
In this file I have created Two Hosts_Groups ( Load_Balancer , Web_Server ), in Load_Balancer group I have provided the credentials of Node where I have to Configure HAProxy. And in Web_Server group I have provided the credentials of Nodes which I have to make web servers.
STEP-3:
Now the file which we have created, we have to tell the Ansible that you will have to use this file as your inventory file. Directory of Config file of ansible-
“/etc/ansible/ansible.cfg”
STEP-4:
Now we have to create one playbook of ansible, where we have to write the code of ansible in YAML format. To create the playbook-
“vim task_haproxy.yml”
Let’s understand the code of Playbook line by line.
1-
Here, as we know we have to first have to define for which HOSTS OR NODES, we have to run the code. Here I have used the HOST_GROUP= LOAD_BALANCER. This group is we already created inside our Inventory File. It means this code will only run in that IP which we have to make as our Load Balancer.
To make our Node as a Load Balancer we should have HAProxy Software installed in our Node. The first task of our playbook will install HAProxy software in the Load Balancer server.
“PACKAGE” is the module of an Ansible which is used to install the softwares, here I have given the “NAME” of the software that I waana install in my Node and the “STATE” is present.
Now, after HAProxy installed successfully we have to configure the HAProxy file. In this file we have to update the PORT_NUMBER, and the Web Servers IP’s, so that HAProxy LB understand for which IP’S it have to acts as a Proxy. We have some ways to update the config file of HAProxy, either by going inside the Target Node and will do it manually. But here we are using ansible which is used for Automation then why we will do manual things here. So to make it automatic, we should have that config file in our Controller Node, either take it from Internet or just copy from the Target Node using “scp” command of Linux. Directory of Config file of HAProxy-
“/etc/haproxy/haproxy.cfg”
“TEMPLATE” is the module of an Ansible, that is used to copy files from source to Destination nodes. In the “SRC” I have given the directory where the config file have present in my Controller Node and in “DEST” I have provided the directory where I wanna copy my file.
“SERVICE” is the module of an Ansible, that is used start or stop the services. Here “NAME” of my service is “haproxy” and “STATE” is restarted.
“You have to first remove the config file from Target Node before running this playbook”
2-
Now, the next tasks we have to do inside our Web Servers, that’s why I have used the “HOSTS” as a “HOST_GROUP”- Web_Server that we already created inside the inventory file of Ansible.
To make our Nodes as a Web Server, they should have Web Server installed. Again I have used the “PACKAGE” module of Ansible to install webserver “NAME” as httpd (APPACHE WEB-SERVER).
As we know that the document root directory of Apache Web Server is “/var/www/html/” only from this directory apache web server reads the web pages. By using “COPY” module of ansible same module like “TEMPLATE” I copied my web page (index.html) from my “SRC” directory to the “DEST” directory.
To start the Web Server service, I have used “SERVICE” module of Ansible which is used to start or stop the services. Here I have given the “NAME” of my service which is httpd and the “STATE” as restarted, whenever the playbook run my service will restart automatically.
“NOTE: YOU SHOULD HAVE YUM CONFIGURED IN YOUR WEB SERVER NODES BEFORE RUNNING THIS PLAYBOOK”.
3-
Now let’s see the config file of HAProxy that we have to configure. “haproxy.cfg”-
Now, in this file firstly we have to update the PORT NUMBER, here I have given “8080”, normally the Web Server runs in this port number.
And at last we have to update the IP’S of our Web Servers. “GROUPS” is pre created variable of Ansible, where they stores the information about the Nodes or Hosts that we have putted inside the Inventory File. Here I have used “GROUPS[‘WEB_SERVER’]” this variable gives me IP’S of my Web Servers that I have already putted in the “Web_Server” Host group inside the Inventory File.
As we know that Ansible supports “JINJA LANGUAGE”. “{% %}”- this symbol is used in Jinja to Use Pre-created keywords like FOR,IF, ELSE etc. Here I have used FOR loop in my config file, for updating the IP’S of my web servers. “{{ }}”-this symbol is used to tell Jinja that treat it as a Variable. “{% endfor %}”- To tell Jinja that it’s end of FOR LOOP.
By coding of only 3 lines, we have automated our Load Balancer. Now whenever the new Web Server come up, we just only have to update the inventory file of Ansible, the config file of HAProxy will automatically update.
“WE HAVE COMPLETED OUR CODING PART, NOW WE HAVE TO RUN OUR PLAYBOOK”.
STEP-5:
We have to run the Playbook that we have Created. Before running the playbook always check the connectivity with our Target Nodes. Command-
“ansible all -m ping”
I have connectivity with my all the Target Nodes. In your case if some error come up then this output will comes in Red Color.
Now, to run the playbook use this command-
“ansible-playbook task_haproxy.yml”
In my case I already run my Playbook, that’s the reason the output contains green color, but in your case if you will get output in Yellow Color it means your playbook has run successfully, but if you get some red color output then you have missed something in your code or your code is not proper.
“My web page INDEX.HTML that I have created-”
Now, my playbook is run succesfully. Now if you use the IP of your Load Balancer Node with port number 8080. You will get the output-
Here I have used my Load Balancer node IP with port number 8080. (192.168.43.34:8080).