HAProxy on AWS using ansible
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8153KBHbMYdi7XpPdVWim5kUSYgV5kgALwY6k-A6VBXlfwpL75Lrux4Q6oF_D9_ro6lPWVRDPpGPoyRsRlNe5Yxg5kNu38J44OcMKeCXq9EqZH-e9iBdhzZg2GaE3tgVWi1Uny3e0pYM/s16000/1_13GO0_FEtBUCfOlaGnceuA.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.
RED HAT ANSIBLE ROLES
ansible roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.
In ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex ansible playbooks, and it makes them easier to reuse. The breaking of the playbook allows you to logically break the playbook into reusable components.
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.
Task Objective:
1. Provision EC2 instance through ansible.
2. Retrieve the IP Address of the instance using the dynamic inventory concept.
3. Configure the webserver and HAProxy through ansible Role so we can use this infrastructure for our website!
NOTE: all the source code is in GitHub. the link provided in the last
Let’s Get Started
make sure you have the following prerequisite satisfied on your controller node
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic71iPgC_5dQ7EwiyyL1kFtB_eRpPlBdig4igQR0-RhH0Ph8KO1BhPP514ERO4V5j1khYb6DXUX_7wchzoqe-2a2_g2XWDOcywkkQp3mmcMJceXW4-4qjNDTxkHTtz1HDer6n4K8UnHjY/s16000/image.png)
https://boto.readthedocs.io/en/latest/boto_config_tut.html
step 1: Setup dynamic inventory
As we are going to implement this task on AWS so we don’t know about IP(No instance by default) so we can not use static inventory but we have some ways to get IPs on the fly after provisioning an instance using dynamic inventory scripts.
the dynamic inventory can be created using some script and the following is the link for that script
https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.py
https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.ini
Both files need to be in executable format:
chmod +x ec2.py
chmod +x ec2.ini
After that, you need user credentials for accessing AWS. As ec2.py make an API call to AWS for your requirement. so we need to set 2 environment variable in the following file
Create one file at '~/' with name ".boto"// add following data in file [Credentials]
aws_access_key_id = <your_access_key_here>
aws_secret_access_key = <your_secret_key_here>
Add the inventory file in the ansible.cfg As:
Note: we also need a private key file because EC2 uses key-based authentication
[defaults]inventory = ./ec2.pyremote_user = ec2-userdeprecation_warnings = Falsehost_key_checking = Falseinterpreter_python = auto_silentprivate_key_file = newKey.pemroles_path = rolesask_pass = False[privilege_escalation]become=truebecome_method=sudobecome_user=rootbecome_ask_pass=False
step 2: Provision EC2 instance.
Run the playbook EC2.yml to provision instance. In this playbook, pass all parameters according to your requirements.
To run Playbook use the following command.
ansible-playbook FileName.yml
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwnpTKsnX2KdgGrhE1IF1OkMLj-riQvzb-3GkE3EkJJrVHzq3vcGxkz_qEAVlI4hjIFfp6hE16z3ZrchFeR_Rm40zUUuGK-ItSapCl3-XDSCfm_ZD1CQXQXFV0GKk7NyH1PnQ0JXj_HB4/s16000/1_bZ6S3c_VryhmvU2E-uXJmA.png)
if you wanted to see your dynamic inventory Run the ec2.py file on the command line
./ec2.py // run the command on terminal
Here tag_Name_HAProxyServer and tag_Name_WebServer are the names of your proxy server and web server you need to use this name whenever you need to configure them.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDq7zp5JOMtuFjg27S_Z9FsgsSm5Tg0f6PvaREExR640v__mwjkRqhfcRfBfQLQpUG6YvCa7PRx-K9V6a9Vd7yvNsIpQ-OjOzbj4WPdBSgg4jD4C6d9yYr-BjeydZM18m8jghwYu7eQdE/s16000/1_FDxaV0tyUvVvVeW7NJge2g.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhon0IWkljjzn7QJhb-dIgFT73-nitpAqK-Jen3ULUQkD8BY7aJCf4FScmcslBUqc1t6StJAFdLQM5gqcunpe8laMtYRWvMfVgOS841P7SJKItSXhqRQiuDCxV3oO4s3OMzvdM22MLtYB4/s16000/1_QlGIdR-SEoaG0WKapPHEbQ.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVFK-ajOKCJa8AbgmsVI2e05-otkRWn9GdrxJ0BZjf9R6XYQ7ZHSNyS3bkAEzOGxSPaahTeCYt7mNho1b6YZxJi_f4v3xFvVaa78rbNmqogmwrlgK7GHO4rXGDNoWavrlHgX32efi1UtA/s16000/1_EZi-pWr-ifeHPlv01Gz5pw.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhihGiELM6fE_I0Cipx5fV_9_2CgxDXVVjt2ElU1k0OweiBSvSZwgGpVI-fztKOuCK2IndI7n70B3gRdyAobDuOFlBftaoqbjxihfolwAcjbP-UCJgbp9uGja1EVlXMCWNZPfRe_sSRH0c/s16000/1_EZi-pWr-ifeHPlv01Gz5pw+%25281%2529.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEij8lYCn8z-5SKckC-3djNK7z1ciG-gZnV4w1nqZpO0DVjTBTLKhB_NvxS2CIYVtpduLzZC8TtgiMi1NKtF7CNT7cHDg3a-zWC_mt2Bdafx2dn0pFbj1jQE-slVv2USChPM7jWjcC0h0Dc/s16000/1_PfrcyAl-rKPqwzOcfRqJxw.png)
step 3: 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/AVvXsEgjWEJmZ3nJjphKD0j11mSOlBkwJU6oLVTbc_oDVmGUUtsw-tKGDzjxLSUC5trBbiXiN-g8f1j5F2WEyp2OSQ5tOIBIrgOYP7lkJwSe89YgvYeaXVeind16NiDZyhIiNWecjJM2G3vaBW8/s16000/1_kX3sKTeRQCBkYG1YHhK1CA.png)
step 4: apply the roles to create a webserver and haproxy
Run the playbook SetUp.yml to configure the instance. In this playbook, pass all parameters according to your requirements.
Note: here we don’t have a hostname but we can use tags as a hostname
All the roles are given in my Github repo
folder name is
- apache
- HAPROXY
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiriLnvSDNWXrLVIlbWpEfRxeurLHy7HefdtBRL3KgizXPWdc8G2k6R6tmi9aa3q1Np2qPMDx0PzY_4QTHFqNUDwg99ECabzVpTqFRnmIfJkTt419Cj_-9y88mwCI1kfWlWjW08Utph_Q/w640-h330/1_apK2UZzKOffdZKUM2EuNWw.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR-Nqp7RlTJG21r2sNOVtFIY3AYFFiEO-SoCikJNVOszSiOu5nnnFR7NBxCZW4mvQclDKerY4gckJ1TuhsZ1a9c2poImfZlOhQKEO-3BE1cZ2QuA4NKQGil3stD0Sq8rCo_q6fGo_PtPo/w640-h322/1_hoIqrMNrtuJEo4Bf7vodVA.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgBaBCOU8NaUuGPb-VWyh6EHti8wc57xqA8HHn56BmRliVOd2IqcfnirKsxcyAmspe7_mTJJRTurvpdO4OEgWFp1bA5QwhUpCQ5JJbZZQRXs4Z2Iv9sTf4n_2322_sy-3WueGybfRPjDq4/w640-h380/1_kZRdorJo5H4sR4hWvPmvjw.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi154m2eUvbhah5TlGxIF-gJo7AaYf4tNW8kQzVTDIeaEcm6dhEAn16wYORU44CcBHIeyuWrZlV6gsJsjgVAtfFxZ0btTrkbMOonfhitG48MMOszGdj70Ac2P5hKfD3a4v-7d2MTQkMPOE/w640-h348/1_GYdoR5NdT-BymUs9fcJ-4w.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGS_ed2cdJgsIn9lJP0C2n7jD6PL-w2wd6d_r6XEXSl6fGBXOZQQyBPaQsjU0NGzfT2kopZdn7GAtK2AtCSESZJarmFPNDdGSDjSG_o5pqDcW5ikXKAlxqTfmGMVhjcalapR8V2lHt1u0/w640-h384/1_K2VEL6TKDX14X9DcLjdfKg.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcQaiQB1ROIA1JSAYsXqdzt4CJqty7WP_Pb6mDeD9P5oFLeaDtdIsMLjXTG9WdSsaVOCinuDqe_HkrDaxLzcRXiEhDbPV0DsBuShdTgqLjI3KS6gbpMlv_Lr0OT4vMp8TbVDsYnl3fZXM/w640-h366/1_2kCWXTH1LM5288z_7McULQ.png)
Finally, we are ready with our reverse proxy that is haproxy in the following images you can see haproxy working fine, and they did work of load balancing.
to access use the following URL in the browser.
http://"haproxy server ip":"listening port"
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8eN073yVo-MwUrppj-WKwU1h7Ma-RynvgtP3gQmyj2PpqA83UL0zW9es-T3W5Kkd-OUQrCvyjkLMRt-o85XEHqsG_P_miCdoSPU5JldBLhC1XICvEjwcUzXGA4dm7JuzwQjdnZzxlIWY/s16000/1_CYSqz_RfVB9J7cJeUD28yw.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGdskzaZnMHdJ_Z1uxaLQ74kFQd0QzLr1Cj6ezami5G6ae4Sh2bK6REY_LTh9pUujyVUi1uStP4LQfTStv0GUZBtBks1myP3F7lJW1ocWUghwWDMCKmx18g3elFqT2L0KYBJiRBuVvYh8/s16000/1_UrRkLJllWWrYpGZzGXI2cw.png)
GitHub Link:
https://github.com/venkateshpensalwar/ARTH/tree/main/Ansible/Configure%20HAPROXY_AWS
Conclusion:
In this ansible tutorial, we have learned how to configure Haproxy Software using the ansible playbook on AWS. In this way, we can see that how easily we able to configure such a complex task using ansible roles. and this is the power of ansible roles in the configuration world.
Hope this blog is helpful to you!!!!
When you are looking for hosting packages to provide online visibility for your website, it is advisable to research on the kinds of services you will expect to get from a web host. When you choose dedicated server hosting plans, you can enjoy unlimited resources from the hosting provider. https://onohosting.com/
ReplyDelete