ANSIBLE| HAPROXY | LOADBALANCER

ANSIBLE| HAPROXY | LOADBALANCER

What is Ansible?

  • Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery

What is HAPROXY?

No alt text provided for this image
  • HAProxy is free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spread requests across multiple servers.

What is Load Balancer?

  •  Load balancing is the process of distributing network traffic across multiple servers

----------------------------------Architecture of HAPROXY----------------------------------------

No alt text provided for this image

We are going to create an Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update its configuration file automatically each time a new Managed node.

In this practical, I used 4 EC-2 instances as shown in the below images and we are configuring a Load_balancer and two apache web-server.

No alt text provided for this image

In the ansible main, ANSIBLE is installed.

No alt text provided for this image
  • Create an inventory file
[loadBalancer]
65.0.94.232 ansible_user=ec2-user ansible_connection=ssh

[Web]
13.126.196.102 ansible_user=ec2-user ansible_connection=ssh
13.233.31.153  ansible_user=ec2-user ansible_connection=ssh
No alt text provided for this image

Now to connect to all the ec2 instances we have to perform privilege escalation in the ansible configuration file ansible.cfg and also specify the private key to make a successful ssh connection.

No alt text provided for this image

Now let's create roles

First, let's understand what are roles in ansible.

  • Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules

Create a directory mkdir /etc/ansible/roles

  ansible-galaxy init webserver


  asnible-galaxy init load_balancer

The above command will create two roles "webserver" and "Load_balancer"

The main playbook will contain

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



- hosts: LoadBalancer
  roles:
  - roles: load_balancer
  • First, let's create a playbook for Web-Server in /etc/anisble/roles/webserver/tasks/main.yml
No alt text provided for this image
 - name:"Installing Httpd"
    package:
      name: "httpd"
      state: present
 - name: "copy the content"
   copy:
      src: "/root/ansible/webfiles/ansible.html"
      dest: /var/www/html/
 - name: "restart httpd"
   service:
      name: "httpd"
      state: started



  • Install the HaProxy in the controller node.
yum install haproxy

In the configuration file of HaProxy, we are going to use port no 8080 for the Load_Balancer and we use Jinja and extract the hostname of each ec2 instance

No alt text provided for this image
{% for host in group['web'] %}
     server app{{ loop.index }}  {{ host }}:80 check
{% endfor %}

  • Second, create a playbook for Load balancer inside /etc/anisble/roles/Load_Balancer/tasks/main.yml
No alt text provided for this image
- name: "Installing Haproxy"
  package:
     name: "haproxy"
     state: present
- name: "Copying the Haproxy file"
  template:
     src: "/root/ansible/haproxy/haproxy.cfg"
     dest: /etc/haproxy/
  notify: Load_balancer restart
- name: "restart haproxy"
  service:
     name: "haproxy"
     
     state: "started"

Now let's run the final playbook

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

Let's check if the Load_balancers are running or not.

No alt text provided for this image

First, I was connected to server 1 and then the server 2.

No alt text provided for this image

Reference - Amit Sharma

---------------------------------- ??Successfully completed??--------------------------------

Keep Learning !! Keep Sharing!!!??

Thank You !!!??





Adarsh Kumar

Cloud Technical Solutions Engineer @ Google

4 年

Well done Suyog Shinde

回复
Pritee Dharme

ARTH - School Of Technologies || Python || Amazon Web Service || Big Data || C || C ++ || DevOps (Kubernetes , Ansible , Docker , Git and GitHub , Jenkins )

4 年

Great Suyog Shinde

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

Suyog Shinde的更多文章

社区洞察

其他会员也浏览了