Configure HAPROXY Using Ansible Automation Tool
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.
RED HAT ANSIBLE
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
Ansible is Designed for multi-tier deployments, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.
APACHE WEB SERVER
The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.
if you wanted to know how big organzation uses ansible please go through my this blog:
https://all-about-devops.blogspot.com/2021/03/nasa-case-study.html
Task Objective :
Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically each time a new Managed node (Configured With Apache Webserver) joins the inventory.
Configuration plan for Haproxy :
step1. checks connectivity with the managed node.
step2. download HAPROXY and install it on one of the managed nodes.
step3. Configure haproxy.
- create haproxy config file using jinja language.
step4. install Apache web server on Backend nodes.
- install PHP on the backend server.
- put PHP(index.php) files in the document root.
- start web server services.
step5. start the HAPROXY services.
pip3 install ansible
you need to create an ansible directory in
etc/ansible/ // ansible directory
Inventory File[Data.txt]
Ansible uses this file to check host connectivity it is a kind of IP database.
[HAPROXY]192.168.0.112 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root[WebServer]192.168.0.113 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root192.168.0.108 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root
Variable file[Variable.txt]
#### variables used in HAPROXY configuration file #####- LoadBlancer_port: 8080- WebServer_port: 80
step 1: Connect to the managed node
to check the connectivity of the managed node run the following command.
ansible all -m ping
step 2: download HAPROXY on one of the managed nodes.
to download haproxy we can use yum or DNF command using ansible-playbook we can do this by running the following play-book
To run Playbook use the following command
ansible-playbook FileName.yml
step3. Configure haproxy.
- create haproxy config file using jinja language.
haproxy have one config file we just need to change haproxy port and backend server IP through which your website will display and their ports so we use jinja language to do this
Note: I will show only the changed part of the file you can see the full file in my GitHub repository
haproxy.cfg filefrontend mainbind *:{{ LoadBlancer_port }} // here we will add port for haproxy#-------------------------------------------------------------------# round robin balancing between the various backends#-------------------------------------------------------------------backend appbalance roundrobin{% for ip in groups['WebServer'] %}
// we used loop because we have many servers server app{{loop.index}} {{ ip }}:{{ WebServer_port }} check{% endfor %}
step4. install Apache web server on Backend nodes.
- install PHP on the backend server.
- put PHP(index.php) files in the document root.
- start web server services.
Here in this step we will install and configure HTTPD and PHP on Backend servers so actually backend server are the servers that actually holding your website and the haproxy server will do just reverse proxy with load balancing
after installing both things we will copy our index.php file to the Document Root of the Apache webserver. after copying files our all the settings are configured for backend servers so we will start our services. These all things I have done in a single ansible-playbook you can see in the following playbook.
step5. start the HAPROXY services
Now all the setting up part is done so we will start haproxy.
all the services have been configured so We are able to see the working of haproxy
to access GUI use the following URL in the browser.
http://"haproxy server ip":"listening port" Actual Backend server that is used by haproxy
192.168.0.108
192.168.0.113
you can see haproxy doing the load balancing and the technique they are using is round-robin.
GitHub Link:
https://github.com/venkateshpensalwar/ARTH/tree/main/Ansible/Configure%20HAproxy
Conclusion:
we have learned how to configure Haproxy Software using the Ansible automation tool. In this way, we can set up Haproxy with the help of Ansible Automation amazing thing is that we can scale out this to as many backend servers as you want to increase load balancing.
Hope this blog is helpful to you!!!!
No comments