Deploying HAProxy Load Balancer and webserver over AWS EC2 using Ansible

In this article, we are going to set Apache webserver and HAProxy load balancer over AWS using the Ansible automation tool. Before going to learn this article you must have some basic knowledge about the Ansible. You can refer to my previous blogs for that and also you can learn how to set up that in your RedHat 8 or CentOS 8 operating system.
What is Ansible and How it is so Powerful?
How to install Ansible on RHEL8?
After having some basic knowledge about Ansible you also have some knowledge about HAProxy Loadbalancing as well.
Load Balancing
Load balancing is a technique of distributing networks over different servers. This ensures that no single server will have to bear all the traffic. It maintains the record of every server and sends traffic to all the network evenly. It solved many use cases of today like high availability of data and less latency time and better responsiveness.
HAProxy
HAProxy is a free open source software that provides a high availability load balancer and proxy server for the TCP and HTTP applications for Linux, Solaris, and FreeBSD. It’s the most common use to distribute the workload over multiple servers. By using this we get rid of the non-availability of the servers and also reduce the latency for the high traffic webserver’s.
In today’s production environment this software has a great demand where thousands of servers are running but providing the same content over the same URL to a huge amount of people is really a tough task. This HAProxy uses the load balancing technique and solves all the big use cases. To know more you can follow this article.
Let’s come to our task for today
Task Description
Create an Ansible playbook of different roles that deploys an HAProxy load balancer and multiple webservers on AWS.
- Provision EC2 instance.
- Retrieve IP addresses using the dynamic inventory concept.
- Configure the webserver.
- Configure the HAProxy load balancer.
- The target node of the webserver should auto-update.
Code
It’s good to check that Ansible is installed or not and for that, we can use ansible –version.
In order to complete the task, I have created three different playbooks:
task3– To deploy the instance and respective key-pairs and security groups.
webserver- To provision the apache webserver
lb – To provision the load balancer
Provision EC2 instance
In this, I created a key pair and two security groups(one for webserver and other for HAProxy) and 3 EC2 instances one for the load balancer and the other for webservers.
---
# tasks file for task3
- name: Create Key Pair
ec2_key:
name: task3key
aws_region: "{{ region }}"
register: ec2_key
- name: Copy Key to Local File
copy:
content: "{{ ec2_key.key.private_key }}"
dest: "{{ key_dest }}"
mode: '0600'
- name: Create Security Group - Allow SSh, HTTP
ec2_group:
name: task3_web_sg
description: sg for web inventory
region: "{{ region }}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
register: task3_web_sg
- name: Create Security Group - Allow SSh, HAProxy
ec2_group:
name: task3_lb_sg
description: sg for lb inventory
region: "{{ region }}"
rules:
- proto: tcp
from_port: 8080
to_port: 8080
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
register: task3_lb_sg
- name: Launch EC2 Instance for webserver
ec2:
key_name: task3key
instance_type: t2.micro
image: "{{ image_id }}"
wait: yes
region: "{{ region }}"
count: 2
vpc_subnet_id: "{{ subnet }}"
group_id: "{{ task3_web_sg.group_id }}"
assign_public_ip: yes
state: present
instance_tags:
Name: webserver
register: web
- name: Launch EC2 Instance for lbserver
ec2:
key_name: task3key
instance_type: t2.micro
image: "{{ image_id }}"
wait: yes
region: "{{ region }}"
count: 1
vpc_subnet_id: "{{ subnet }}"
group_id: "{{ task3_lb_sg.group_id }}"
assign_public_ip: yes
state: present
instance_tags:
Name: lbserver
register: lb
- name: Refresh Inventory File
meta: refresh_inventory
- pause:
minutes: 2
Here I created the key pair and two security groups on for webserver allowing Http port 80 and other for HAProxy allowing port 8080. We also added to refresh the inventory option so that after the creation of an instance the inventory file dynamically fetches the IP.
Configuring the webserver
<pre class="wp-block-syntaxhighlighter-code">- name: Install Required Package
package:
name: python3
state: present
become: true
- name: Install Apache Server
package:
name: httpd
state: present
become: true
- name: Copy webpage
get_url:
dest: "/var/www/html"
url: "<a href="https://raw.githubusercontent.com/anubhavsinghgtm/aws-ansible-dynamic-inventory/master/index.html">https://raw.githubusercontent.com/anubhavsinghgtm/aws-ansible-dynamic-inventory/master/index.html</a>"
become: true
- name: Start Apache Service
service:
name: httpd
state: started
become: true</pre>
This helps to set up the Apache webserver and start the service as well.
Configuring HAProxy webserver
- name: install haproxy software
package:
name: "haproxy"
state: present
become: true
- name: copy my conf file of lb
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
become: true
- name: start service lb
service:
name: "haproxy"
state: started
become: true
This helped to provision the HAProxy load balancer and done.
All the roles are created successfully. Now, we will create a playbook that will run all of the above.
- hosts: localhost
roles:
- task3
- hosts: tag_Name_webserver
remote_user: ec2-user
roles:
- webserver
- hosts: tag_Name_lbserver
remote_user: ec2-user
roles:
- lb
Once the code is done let’s run this playbook and have a look at the output
use ansible-playbook task3.yml
security groups:
Instances:
Our setup is ready. Let’s look run our system and get the output.
Our Output:
Hurrah!!! We have successfully completed this. Our load balancer is working fine showing the output of our webserver to our client. In this task, I have used only two instances but in real industry use cases, we can extend the number of instances and can manage the whole server accordingly.
Stay tuned with us for more such amazing tasks. You can also follow us on our social media handle so that you never miss our future updates.
Sharing is caring and hence share this task with your friends and help them to grow.
In case of any query, you can comment down below or can connect with the author on other social platforms like
12 Comments
seo tips · November 9, 2020 at 11:20 PM
Great post! We will be linking to this great post on our site.
Keep up the good writing.
quality seo · November 13, 2020 at 9:41 AM
Hi, I log on to your blog like every week.
Your writing style is awesome, keep doing what you’re doing!
guaranteed seo service · November 13, 2020 at 10:11 PM
I’m not sure exactly why but this weblog is loading very slow for me.
Is anyone else having this issue or is it a issue on my end?
I’ll check back later on and see if the problem still exists.
essential wordpress plugins · November 16, 2020 at 12:17 AM
Wow, marvelous blog layout! How long have you been blogging for?
you make blogging look easy. The overall look
of your website is excellent, let alone the content!
passive income blogs · November 16, 2020 at 1:39 AM
Good day I am so glad I found your blog, I really found you by
mistake, while I was browsing on Digg for something
else, Regardless I am here now and would just like to say thank you
for a remarkable post and a all round exciting blog (I also
love the theme/design), I don?t have time to look over
it all at the minute but I have book-marked it and also
included your RSS feeds, so when I have time I will be back to read a great deal more, Please
do keep up the awesome work.
post-installation wordpress · November 18, 2020 at 10:46 PM
Spot on with this write-up, I seriously believe this
website needs a lot more attention. I?ll probably be back again to read more, thanks for the advice!
Web host · November 21, 2020 at 10:46 PM
Hey there! I know this is kinda off topic but I was
wondering which blog platform are you using for this site?
I’m getting tired of WordPress because I’ve had problems with
hackers and I’m looking at options for another platform.
I would be great if you could point me in the direction of a good platform.
web trends · November 23, 2020 at 12:47 AM
You really make it seem so easy with your presentation but I find this matter to be
actually something that I think I would never understand.
It seems too complicated and very broad for me.
I am looking forward for your next post, I’ll try to get
the hang of it!
http://winstoryquest.website · February 19, 2021 at 1:22 AM
Hey There. I found your blog using msn. This is a very well written article.
I’ll be sure to bookmark it and come back to read more of your useful info.
Thanks for the post. I’ll certainly comeback.
http://winstoryquest.website · February 20, 2021 at 4:59 AM
Hello, Neat post. There is a problem along with your web site in web explorer, may test this?
IE nonetheless is the market leader and a good section of
other people will pass over your magnificent writing because of
this problem.
http://winstoryquest.website · February 23, 2021 at 5:00 PM
I do consider all the concepts you’ve offered to your post.
They are really convincing and will certainly work.
Still, the posts are too short for beginners.
Could you please lengthen them a little from next time?
Thanks for the post.
Mary · March 16, 2021 at 12:32 AM
Hi, yes this paragraph is genuinely pleasant and I have
learned lot of things from it on the topic of blogging.
thanks.