Configuring Load Balancer on AWS using Ansible with dynamic inventory!

Configuring Load Balancer on AWS using Ansible with dynamic inventory!


No alt text provided for this image

Task To Be Done:

  • Provision EC2 instances using ansible.
  • Retrieve IP Addresses of instances using the dynamic inventory concept.
  • Configure web servers through ansible role.
  • Configuring load balancer through ansible role.
  • The target nodes of the load balancer should auto-update as per status of web servers.

Summary: One-Click Instance Launched, Web Servers provisioned and Load Balancer is ready!

What is a Load Balancer? 

A load balancer serves as a single point of contact for clients. A load balancer distributes incoming application traffic across multiple targets, such as EC2 instances, in multiple Availability Zones. This increases the availability of your application. You add one or more listeners to your load balancer.


CREATING A LOADBALANCER USING ANSIBLE:

First, we will set up an Ansible environment in our system. For doing anything on aws using local system with the help of ansible, we will install the boto3 library of python.

 # pip3 install boto3

Now we launch the ec2 instances on AWS by writing .yml code.

webserver.yml:

No alt text provided for this image

loadbalancer.yml:

No alt text provided for this image

Create one more pass.yml file for storing the my-access and my-secret keys.

# vi pass.yml

myaccess: "ACCESSKEY"
mysecret: "SECRETKEY"
 

After creating and saving the pass.yml file we have a vault which we want to keep secured. For this we have used:

 # ansible-vault encrypt pass.yml (File_Name)

Now we run the ansible-playbook to launch the Webservers and Load Balancer:

  • webserver.yml:
No alt text provided for this image
  • loadbalancer.yml:
No alt text provided for this image

After this, check if the aws instances if they have successfully launched or not.

No alt text provided for this image

Now, set up the Dynamic inventory environment by using python:

host.py:

No alt text provided for this image

Now to make these files executable run the following command:

# chmod +x host.py

Now, we need to configure inventory and add some other details.

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

After saving the inventory file and running the following commands, we will get the IP of the ec2-instances. The Python code fetches the IP of aws ec2-instances, and will also play the role of dynamic inventory. following are the IP of webserver and loadbalancer instances respectively.

No alt text provided for this image

Now we check if that IP is properly pinging or not:

  • Webservers:
No alt text provided for this image
No alt text provided for this image
  • Webservers:
No alt text provided for this image

Now we create roles for the configuration of haproxy into the Load-Balancer instance and httpd into the Web-Server instances.

We'll use the following command for creating roles load balancer and webserver:

# ansible-galaxy init <Role_Name>

For checking the Roles List we will execute:

No alt text provided for this image

Now configure Haproxy service inside the load balancer role:

No alt text provided for this image

handlers:

No alt text provided for this image

haproxy.cfg:

No alt text provided for this image

 Now configuring httpd server inside the webserver role:

No alt text provided for this image

roles.yaml:

No alt text provided for this image

Running ansible-playbook command:

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

Now checking that haproxy service has started or not in the Load-Balancer instance:

No alt text provided for this image

haproxy.cfg:

No alt text provided for this image

Outputs:

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

Thank you for reading my article. I hope it will help you understand the concepts worked on. If any improvement is required, please let me know.

Nilesh Gopale

Associate Software Engineer @Veritas Technologies LLC | Building Kubernetes Protection | Top 1% @Leetcode | Specialist @Codeforces

4 年

Great sir?

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

社区洞察

其他会员也浏览了