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?
- 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----------------------------------------
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.
In the ansible main, ANSIBLE is installed.
- 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
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.
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
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
- 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
{% 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
- 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
Let's check if the Load_balancers are running or not.
First, I was connected to server 1 and then the server 2.
Reference - Amit Sharma
---------------------------------- ??Successfully completed??--------------------------------
Keep Learning !! Keep Sharing!!!??
Thank You !!!??
Cloud Technical Solutions Engineer @ Google
4 年Well done Suyog Shinde
ARTH - School Of Technologies || Python || Amazon Web Service || Big Data || C || C ++ || DevOps (Kubernetes , Ansible , Docker , Git and GitHub , Jenkins )
4 年Great Suyog Shinde
Software Engineer
4 年Good job Suyog Shinde ?????