Configure HAProxy Server on the top of AWS with the help of Ansible-playbook

Shubham Jangid
6 min readApr 18, 2021

--

What is apache httpd?

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.

What is 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 (in terms of processor and memory usage).

What is AWS?

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 200 fully featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

What is Ansible-playbook?

An Ansible® playbook is a blueprint of automation tasks — which are complex IT actions executed with limited or no human involvement. Ansible playbooks are executed on a set, group, or classification of hosts, which together make up an Ansible inventory.

👉 Pre requisites For this setup is:-

  1. Three system which have Rhel8 installed or “for perform this setup I use virtualization tool”
  2. Local dnf Configured in all three system
  3. Install python3 on the system which you want to make as “Controller Node”, for this you use CMD “dnf install python3 -y”
  4. Install ansible package in controller node for this use CMD:- “pip3 install ansible -y”

👨‍💻 Lat’s start working on the setup:-

Step 1: Launch three instances over the AWS

Step 2: Change the hostname of 2 instances as Webserver

Step 3: Create an inventory file where you will put the IP , user name, user password and protocol for connectivity:-

vim /ansible/ip.txt

Step 4: Create a configuration file for the ansible where you will put the info about inventory file i.e. “vim /etc/ansible/ansible.cfg”

vim /etc/ansible/ansible.cfg

Step 5: Check connectivity between the hosts

Step 6: Create a haproxy configuration file which we want to update

vim updated_haproxy.cfg

#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:8080
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
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
{% for i in groups['apache_web']%}
server app{{ loop.index }} {{ i }}:80 check
{% endfor %}

Step 7: Create an index.php file which will be used to test the setup

vim index.php

<pre><?phpprint "Haproxy Setup on the top of aws Using Ansible-playbook \n \n";
print `/usr/sbin/ifconfig`;
?></pre>

Step 8: Create an ansible playbook which will setup webserver in two instances and setup HAProxy as well

vim ha_proxy.yml

- hosts: apache_web
tasks:
- name: "installing the httpd software"
package:
name: "httpd"
state: present
- name: "installing the php on the webserver"
package:
name: "php"
state: present
- name: "copying the content into webserver"
copy:
dest: "/var/www/html/index.php"
src: "/home/ec2-user/index.php"
- name: "Start services of the httpd"
service:
name: "httpd"
state: started
enabled: yes
- hosts: 127.0.0.1
tasks:
- name: "Install HAProxy for the LoadBalancer"
package:
name: "haproxy"
state: present
- name: "Configure the loadbalancer"
template:
dest: "/etc/haproxy/haproxy.cfg"
src: "/home/ec2-user/updated_haproxy.cfg"
- name: "Start services of the loadBalancer"
service:
name: "haproxy"
state: started
enabled: yes

Step 9: Run ansible-playbook

ansible-playbook -vv ha_proxy.yml

Step 10: Now to check that both instances are successfully configured as webserver or not ,for this we’ll be checking package — “httpd & php” in both systems(webserver) and also checking the “index.php” file in both systems(webserver)

Step 11: Now check HAProxy is installed in other instance or not

Step 12: Now Check the url of HAPROXY server in the browser

Step 13: Again refresh the page

As we are obtaining webpages from different webservers every time ,so we conclude that HAProxy server is being successfully configured on the top of AWS.

Thank you

--

--

No responses yet