Creating Load Balancer with HAProxy server and Apache httpd servers.

Meherchaitanya
3 min readJan 3, 2021

Hello there!👋 Welcome to 2021. Let’s Hope that we make this year productive no matter what happens 😄.

You may have known about Load Balancers. If not basically the balance the load as the name says. So you may ask why we need a load balancer. For the Industry use cases or for your startup, you may need to have multiple websites. But setting and configuring these servers for scaling them is a difficult task to do. This is where Ansible comes to help us. With the help of ansible, we can automate the configuration process and create a Load Balancer on your workstation, your company servers or on your cloud.

So let’s start with writing the configuration files.

Create a main.yml file and along with that we need to create template files to be exported.

haproxy.cfg

// some config above...frontend main
bind *:{{portno}}
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server static 127.0.0.1:4331 check
backend app
balance roundrobin
{%for app_url in groups['webserver']%}
server app1 {{ app_url }}:80 check
{%endfor%}

we have written a Jinja template file where we have our app servers which will be hosted with httpd server will be written with the help of ansible facts variable called groups and we loop all the servers available for us to use so that we wont need to manually change the file again. Also we neeed to host this server with a port which is mentioned as {{ portno }}

Now we need to create an index.html for the apache server to host something.

<h1>This index page comes form {{ ansible_hostname }}</h1>

what ansible does in the above HTML file does is to embed the hostname of that server.

Now we start writing the config yml

Let’s start with the webserver config

we basically need to

  • install and start the apache httpd server
  • open the 80 port
  • copy the index.html to /var/www/html/ dir
- hosts: webserver
tasks:
- name: install httpd server
package:
name: httpd
state: present
- name: open webserver port
ansible.posix.firewalld:
port: 80/tcp
permanent: yes
state: enabled
- name: create index.html file
template:
src: ./templates/index.html
dest: /var/www/html/
- name: start httpd server
service:
name: httpd
state: started

Now we need to configure the haproxy server to manage the load form the multiple httpd servers. And for this we need to:

  • Install Haproxy
  • copy the config file template
  • when there is change in the file, run a handler to restart the server
  • open the port number
  • start haproxy server
- hosts: lbserver
vars:
portno: 8080
tasks:
- name: install haproxy
ansible.builtin.package:
name: haproxy
state: present
- name: copy conf file to load-balancer server
ansible.builtin.template:
src: ./templates/haproxy.cfg
dest: /etc/haproxy/haproxy.cfg
notify: lb-restart
- name: open haproxy port
ansible.posix.firewalld:
port: '{{portno}}/tcp'
permanent: yes
state: enabled
- name: start haproxy
ansible.builtin.service:
name: haproxy
state: started
handlers:
- name: lb-restart
ansible.builtin.service:
name: haproxy
state: restarted

After this, just run the main.yml file with ansible-playbook command and then you will get the output you need, A server that will balance the load between the httpd servers.

--

--