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

What is apache httpd?

What is HAProxy?

What is AWS?

What is Ansible?

What is Ansible-playbook?

👉 Pre requisites For this setup is:-

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

vim /ansible/ip.txt
vim /etc/ansible/ansible.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 %}
<pre><?phpprint "Haproxy Setup on the top of aws Using Ansible-playbook \n \n";
print `/usr/sbin/ifconfig`;
?></pre>
- 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
ansible-playbook -vv ha_proxy.yml

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store