CREATING LOAD BALANCER ON AWS USING ANSIBLE Roles
haproxy role

CREATING LOAD BALANCER ON AWS USING ANSIBLE Roles


No alt text provided for this image


No alt text provided for this image

Hello guys !! Back with another article, In this article, you will find how we can create a load balancer on AWS using Ansible Roles...

Task Description:-

?? Create an Ansible role for Apache to configure Httpd WebServer.

?? Create another Ansible role for loadbalancer to configure HAProxy LB.

?? We need to combine both of these roles controlling web server versions and solving the challenge for host IP's addition dynamically over each Managed Node in HAProxy.cfg file.

What is Ansible ROLE???

  • As we develop larger and complex playbooks, we often discover opportunities where we can reuse the same code from playbooks. But, in larger playbooks, many imported files and handlers may present, and copy whole content may not be a good call!
  • Ansible Role provides a way to manage the play and enhance the chances of reusability of the code. We can bundle the play in a directory structure in a standard manner. So, copying the role is as simple as copying files and directories.
  • The role enables the playbook to be highly scalable?? and easily sharable and a place with batter management.

Ansible Galaxy is a centralized repository by the community where a huge amount of pre-created roles are available and can be freely downloaded and used. multiple time tested roles are available here.

So let's start this task...

Create 3 ec2 Instances. Here I am taking type t2.micro. You can take any instance type. And my controller node is on VirtualBox.

1 — — Ansible Node (Controller Node) — VirtualBox

1 — — LoadBalancer — t2.micro

2 — — Webserver — t2.micro

Now we have to first create a role inside ControllerNode. Create two roles one for LoadBalancer and another for Webservers.

The default location for the roles is /etc/ansible/roles

create a role folder mkdir /etc/ansible/roles all the roles will be inside this roles folder.

ansible-galaxy  init  WebServer   ---> role created for webserver

ansible-galaxy  init  LbServer    ---> role created for LoadBalancer

Also, create a directory/workspace to manage host files. Here I have created.

mkdir     /etc/ansible/roles/haproxyWS

Here a complete picture of the above explanation looks like.

roles

Also, you need to set the path of the role inside the Ansible configuration file (ansible.cfg).

ansible cfg

Set the webservers and loadbalancers inside ansible hosts inventory file...

No alt text provided for this image

After this check that the managed nodes pingable or not...

No alt text provided for this image

Now we can start writing the playbook inside /etc/ansible/roles/haproxyWS directory.

No alt text provided for this image

After that, we have to configure webserver of each instance. We have to write the tasks in the tasks folder and handlers in the handlers folder.

Now we can write the tasks inside /etc/ansible/roles/WebServer/tasks/main.yml.

No alt text provided for this image

Here we have completed with configuring webserver. You can also use Handlers to restart apache server. Now we have to configure the Load Balancer. First install haproxy inside Controller node.

yum   install   haproxy   -y     ---->  it will install haproxy

Then you need to change the port number binding. You can use any port eg 1234. Here I have used default port 5000. also, you need to provide the IP of all the instances with 80 port. To give IP randomly here we can use  Jinja Template to extract the hostname of each ec2 instance. As Ansible supports python jinja2 conventions, use the loop that prints the IP address of the servers with the pre-defined group keyword of Ansible. Mention the port (80 default) of the httpd service.

No alt text provided for this image

This haproxy.cfg file put in /etc/ansible/roles/LbServer/templates folder.

And tasks for LbServer Role is as follow...

No alt text provided for this image

Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/haproxyWS folder From here we can run the playbook.

ansible-playbook     playbook name

No alt text provided for this image
No alt text provided for this image

Now we can check whether our load balancer is working or not. Take public IP of load balancer with port 5000 (binding port).

No alt text provided for this image
No alt text provided for this image

Thanks, Everyone for reading...

Keep Learning Keep Sharing !!!





要查看或添加评论,请登录

Surayya Shaikh的更多文章

  • OSPF Routing Protocol (Use Cases)

    OSPF Routing Protocol (Use Cases)

    OSPF is a Link State protocol that’s considered maybe the most famous protocol among the Interior Gateway Protocol…

    4 条评论
  • k-means clustering and its real use-case

    k-means clustering and its real use-case

    Every Machine Learning engineer wants to achieve accurate predictions with their algorithms. Such learning algorithms…

  • JAVASCRIPT AND ITS USE CASES

    JAVASCRIPT AND ITS USE CASES

    The collection of prewritten code that has some special working function in JavaScript is known as the JavaScript…

  • How Cyber Department use Confusion Matrix?

    How Cyber Department use Confusion Matrix?

    Hello Everyone ???????♀? This Article is based on cybercrime cases where we talk about the confusion matrix or its two…

  • MongoDB-Workshop

    MongoDB-Workshop

    MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database…

  • Industry Use Cases - Kubernetes / Openshift

    Industry Use Cases - Kubernetes / Openshift

    Container adoption is growing rapidly in the enterprise-much faster than expected. — Forbes The speakers during the…

  • Industry Expert Practical Demo Session on Ansible & Ansible Tower

    Industry Expert Practical Demo Session on Ansible & Ansible Tower

    Hello Guys ??????? Yesterday on 28dec2020 , Mr. Vimal Daga sir and Mrs.

  • Haproxy (Load Balancer) Configuration Using Ansible on AWS

    Haproxy (Load Balancer) Configuration Using Ansible on AWS

    Hello guys !! Back with another article, In this article you will find how we can create a load balancer on AWS using…

  • Automating Hadoop Using Ansible

    Automating Hadoop Using Ansible

    Hello everyone, Back with another article. In this you will find how we can automate hadoop using the linux automation…

  • How ansible helps in solving challenges faced by big industries?

    How ansible helps in solving challenges faced by big industries?

    In this article we will discuss what is ansible in devops and its use cases? If anyone ask you about ansible you can…

社区洞察

其他会员也浏览了