HAPROXY: -Configure using Ansible
By: Pritee Dharme .
Tags: LinkedIn, AWS, Ansible, Haproxy, Apache

Deploying Apache webservers, configuring & updating HAProxy dynamically using Ansible

Hello….

In this article we are going to see that how we can do the configuration of haproxy loadbalancer using ansible playbook .

Ansible :

Ansible is an open source IT Configuration Management, Deployment & Orchestration tool. It aims to provide large productivity gains to a wide variety of automation challenges. This tool is very simple to use yet powerful enough to automate complex multi-tier IT application environments. … Ansible in DevOps.

LoadBalancer:

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.

In computing, load balancing refers to the process of distributing a set of tasks over a set of resources, with the aim of making their overall processing more efficient.

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.

HAProxy (High Availability Proxy) is a TCP/HTTP load balancer and proxy server that allows a webserver to spread incoming requests across multiple endpoints. This is useful in cases where too many concurrent connections over-saturate the capability of a single server.

Now , let’s start our practical . For that we are going to lunch 4 instance in AWS . Here 1 instance for manage node , 1 for LoadBalancer, and other two for web server.

After that we are write our playbook in manage node so for that we have python3 installed in our instance . Install python3 using command..

“ yum install python3 ”

python3 is pre-requisite for install ansible . Now, install ansible using pip command of python.

“ pip3 install ansible ”

Now check version of ansible for conformation.

Here our ansible is installed.

Now , First we have to create an inventory file for storing or giving the ip’s of target node. Here we create an groups for giving ip’s of loadbalancer and webserver.

Now, Let’s create the configuration file for ansible . for that create one folder as “mkdir /etc/ansible” and in that folder create one file having name ansible.cfg . This is an configuration file of ansible in that we write some useful information about target node.

Here in this file we add some more information and that is privilege escalation and this is for .. here we enter in target node as a ec2-user but for install any package we have to use root user or give sudo power so for that we give that information in privilage_escalation block.

So, Make sure first, the IPs which we give in inventory file are attached or not with Ansible’s config.cfg file using the below command.

“ ansible all — list-hosts ”

Also, check that all managed nodes are pingable or not, using the below command.

“ansible all -m ping”

Now, here we are creating the haproxy cluster for that we have to configure haproxy and web-server.

So here we are creating separate playbook for haproxy and web-server and for that we use roles. Now, let’s see what is meant by roles..??

Roles :

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components. … Roles are not playbooks.

Now, For creating rule we first have to create one directory i.e. mkdir /etc/ansible/roles. Now in that directory we create roles for webserver and haproxy(load_balancer) using following command…

“ ansible-galaxy init webserver ”

“ ansible-galaxy init lbserver ”

And also create one directory for writing main playbook . In my case I am creating projects directory for writing playbook.

Now, here we configure haproxy , but for that we required the haproxy configuration file i.e. haproxy.cfg file . for that we have to first install haproxy software using “ yum install haproxy ” command. After that Open the haproxy.cfg file in the controller node and bind the port 8080.

Also, write the below-mentioned jinja code to update the haproxy.cfg file to load balancer dynamically.

Now, copy this haproxy file to other folder for giving location in playbook of haproxy.

So, after that write a playbook for haproxy i.e. load-balancer in lbserver role . So go inside lbserver role in that we see tasks directory , go inside that directory and we see the main.yml file in that file we have to write a playbook.

Write a playbook for loadbalancer in lbserver role.

- name: "install HAPROXY"

package:

name: "haproxy"

state: present

- name: "copy HAPROXY configuration files to LoadBalancer"

template:

src: "haproxy.cfg"

dest: /etc/haproxy/



- name: "restart HAPROXY"

service:

name: "haproxy"

state: "started"

Now, let’s create a playbook for web-server for that we have to first create one html page for copy in target node.

Now for writhing playbook go to /etc/ansible/roles/webserver/task folder and in that we see main.yml file. Write playbook for webserver in it.

Now write playbook for webserver.

- name:"install httpd"

package:

name: "httpd"

state: present

- name: "copy the content"

copy:

src: "/etc/ansible/roles/index.html"

dest: /var/www/html/

- name: "restart httpd"

service:

name: "httpd"

state: started

Here our both roles are created . So we have to create a main playbook now. For that go to projects directory and create one file there having extension .yml and call to roles there.

- hosts: webserver

roles:

- role: webserver







- hosts: lbserver

roles:

- roles: lbserver

Now let’s run the final playbook i.e. main.yml file in projects directory.

Here our playbook is run without any error . Now let’s check the web-page in browser by give loadbalancer ip and 8080 port.

Here it connect to first server , now let’s refresh the page and check the output.

Yes..Your task completed successfully. Hope this article help you …

****If you like it then Clap and Share….!!!!

*If you like it then follow me in LinkedIn ………

And For any suggestion feel free to connect me in LinkedIn..

**Thank You Everyone For visiting a Blog……!!