ANSIBLE - CREATING LOAD BALANCER ON AWS USING ANSIBLE

ANSIBLE - CREATING LOAD BALANCER ON AWS USING ANSIBLE

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

What is Load Balancer ?

Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.

So lets start this task.

Create 5 ec2 Instances. Here i am taking mixture of t2.micro and t3.micro. You can take any instance type. But be sure take t3.micro for Ansible Node. In which you have to Install Ansible.

1 - - Ansible Node (Controller Node) -- t3.micro

1 - - LoadBalancer -- t2.micro

3 - - Webserver -- t2.micro

here i have used ansible to launch ec2 instances. code for launching ec2 instance using Ansible.

No alt text provided for this image

Below is the figure you will understand better. ??

No alt text provided for this image

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

Set the webservers and loadbalancers inside ansible hosts.

No alt text provided for this image

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  loadbalancer           ---> role created for LoadBalancer
No alt text provided for this image

Also create a directory to manage hosts files. Here i have created

below are file which i have created handlers for both loadbalancer and webserver

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


mkdir     /etc/ansible/roles/projects

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

Now we can start writing the playbook inside projects directory.

No alt text provided for this image
- hosts: WebServer
  roles:
  - role: webserver



- hosts: LoadBalancer

  roles:
  - role: loadbalancer

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

now we can write the tasks inside main.yml.

No alt text provided for this image
- name: "install httpd"
  package:
    name: "httpd"
    state: present
- name: "copy the content"
  copy:
    src: "/etc/ansible/roles/index.html"
    dest: /var/www/html/
- name: "restart httpd"
  service:
    name: "httpd"
    state: started

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
No alt text provided for this image

Then you need to change the port number binding. You can use any port eg 1234. Here i have used 8080. also you need to provide the public 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.

No alt text provided for this image
backend app
    balance     roundrobin
{% for  host  in  groups['WebServer'] %}
    server  app1  {{host}}:80  check
{% endfor %}

Now go to configuration file of haproxy -- > /etc/haproxy/haproxy.cfg and copy the haproxy.cfg and paste inside /etc/ansible/roles/loadbalancer/templates. You can also give the location of configuration file of haproxy.

No alt text provided for this image

Now we are ready to run the ansible-playbook. go inside /etc/ansible/roles/projects.From here we can run the play book.

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

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

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

As You can see we have created the load balancer using Ansible on AWS.

I hope you had liked the article. Thank you.

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

Apurv Waghmare的更多文章

社区洞察

其他会员也浏览了