Configure HAPROXY Using Ansible Automation Tool
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhCucjQDFkpSb8UydHx_KzaqgFZLuCTuzFA_MR8u7Ua0edQ8NvxzzY5xDAJdlu0hYPiDlpMdV1qWmARp_9656DDxowbmm7WaY7OltjXaFPaPHPDd_d6fp2aDDZzyJtINoiLNL4PwN5fUGs/s16000/1_lghGHI6xFZg8FjbLCkjYew.png)
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
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbHzBZXAjJP3MFQGmuGOCWA4ka8yDI4WtHSUhqf-kC8OnpF2RL3sVCuxAN5YP08KdJC2P769MzRJ1v4p-bLvIy3ooqmehPpaTinjQtKaZuh80J9WtSLUH9KcchC-UJEP4mS7A1lEt0Kdg/w640-h320/1_4v946t45ANnLztXH3npoyA.png)
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
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjnvaLT2b9C7bjXAlIC-BPxYWgtOc90RJac7CidlBTL9vkI7HA4bZJGp97n6ykfzVNMqMFSQtDh71iKqeWo6XNTZTMUTK6whEqYm0Qv0tB-LT9EdEozWym230y735NNlJnKX4q7STbkefA/w640-h320/1_S_MmB8cHc_f-cHDDbe_OmQ.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTx9NRjxN-Po6jBpmk5iikL934-JKWGqqSUH18nPqHSUhgkbcHuwpcgy2bZWUHaWmcXNNKxxA9xRETg_Ov8Vvv6-YCkEmA2T8-wAbG5pdHqYzym6H2w2ZNsWTOMkhTfNjSs4OcwzWOFmE/w640-h320/1_WMVeIq6-swd5GY4V90aC0w.png)
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 %}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjbobea7lO8LkYsH-HjekHVbEE2AUhbJf8v5xNuGodmxzNGbUYhHGPdEHh8T8c8jkoygqPR66hNNtzFrtwyKFUPXEpW46bPxSvVWTi88GZxmitZij_h27cu-sVob5DEYLZTAoVmH277vI0/w640-h320/1_6oWivg-jNwxMErYULLPk0w.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlYvbn8kJAORsbHbZhbfyT7AvBRy-bPT-766X25MD8k6iPIGhyphenhyphenoWNmIeyEx7n-x8D3sNZpOq8UW4wA5BXiFRIuBHJsz3AvRVIPJ7cJdgC-Q6gQbuPBBOsEvaKpJeQ2Hqlhp1Qe8V6gnN0/w640-h320/1_YLuTTveOx8vBmbvGwkZsLA.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii8tEUUGhb6l2jFLgD03syzjW_Q2C1zmPlq393ThojkefmFcSz8bIshoNYmCwUTv_p-qXCNElKctcUvyCYPhj2jNj45O4DyWTSxNt7_N_f2QWKiGoDJJqeeInEA51EMoU_h69RatEfFN4/w640-h320/1_ALbEDjjUJRpqOF2LHaih1A.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqIivlunVEOrdXBtcUdSfircbEsZ8WYSE-d0OdxeloCwuHqGWQ-dmvXUTJJ2YcKRN0eoO0UFGE9xS-6x_nOBNnJ3F01faz8_TgG8o0svKTisG2Lp2ctW3mGbrt9o75Tx8F4x9C2whiYEY/w640-h320/1_7YzjEMAnQ-T1sya9sWTqbQ.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWUq17AGlN5l1Fm5j7omO7b4fTFRSkuqN93-YsLYtbBzT2G99YwOLL2B5FYrbo-L6aU8tbuNNsI5t4cfLG3y7vej8Y5lWDyal2wWxPT_pEnvNhUuCK3L35M0kCwMZsM6Brq5l9ch0W9WE/w640-h300/1_dfcuekfTqxyks10bnFooIQ.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhDhWArsdEfxvgklNIKwMKfkv9UgCfROTHXpC3nU0tuklvF2e4nwQy-u_yEHp_KY5FAUsVe-olY7r_4k9T7v0fcg6pOwz9cxrpLkTTdN-2AXbPWqYoUScDyjH0ZAVlWxUGZiQ7O186GOA/w640-h274/1_kS2S5S0Koe2uVyKYbhiNIA.png)
step5. start the HAPROXY services
Now all the setting up part is done so we will start haproxy.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhESFuR4H3Vdj-2HLxPNNyWF1DnYMBW9ym87zYxfk6Umq_WCZgyXdRkfjSlz-uVSE23YMPZ2o2ggNVYE1XyfgW4d-mTyVka84RZl_EZR_w4BUwjDrWQjifp_LlH5EceOYV7-kWcg8cd61o/w640-h320/1_L-Ic1puB3ngbTdgCOhCclA.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLwbA7cKzwuteSX9DJWRJHcRBadaK5VeULPKhIc1jzRfqNwy7-LcqZi-W_s7XAsZGopCMqkosXU5TMMvz_Qjt-NfxzVY3OWOFH2qd9N1v5QLKp8NHZBobqs_2nITe4v2YahuHDF2JZDAY/w640-h320/1_b6F3Ky_UExT2BbC_kCC6Bg.png)
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.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2x9eEuhXqBlQjpKPB1QMRNndvJgnyPl_x5LqkcXigsmf9rRI2D7ofcZjIAU1X_ehttZrcYAwxgP0b1LQmwMt516lbnkU277QjE2czOFMc9EgU0dUH_MzIqF4lptwZDd4aijWMJFGoRVg/w640-h320/1_YM19x98snfTgwpOq8tDt0A.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiYVNkbjxB9htWbQ8EMHkK-7ZMP9Pp0LgI3uYmsKtT-hLB-YG1Oh0NfHCitWTyDbKWbfht-WthaQ-qU5eL1XAGSW2iKckOgJwuXk4SBJ1WNY3yUxsjWRWggdD8EZrjv5CYgbbw4SrNFtY/w640-h320/1_aJZD9_swEYINLN-LlJuhzg.png)
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