Creating LoadBalancer Setup In AWS Instance Using Ansible

Haproxy — Using the Ansible

Ansible

Hello ….!!

Here I am coming back with new article in which we are going to see how we can configure an Apache web-server on the top of the docker container using Ansible in AWS instance.In this article I am also lunch some instances that is doing provision using ansible. So if you have any issue with it then here you found the answer of your problem.

Now , before we start the practical we have to know some important concepts. Let’s see some thing related with our task ..

LoadBalancer :

A load balancer is a device that acts as a reverse proxy and distributes network or application traffic across a number of servers. Load balancers are used to increase capacity (concurrent users) and reliability of applications.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.

Configuring Loadbalancer on AWS using Ansible

  • Distributes client requests or network load efficiently across multiple servers.
  • Ensures high availability and reliability by sending requests only to servers that are online.
  • Provides the flexibility to add or subtract servers as demand dictates.
LoadBalancer Architecture

Haproxy :

HAProxy: Kick-Ass Load Balancing Software. HAProxy (High Availability Proxy) is used for TCP and HTTP-based applications. Over the years it has become the standard for open source load balancing. It’s also free and comes with most Linux distributions.HAProxy is a fast and widely used reverse proxy. HAProxy was first released in 2001 by Willy Tarreau. Since then, it has evolved to meet the most prevalent use cases for modern applications, while still maintaining its high performance and strict adherence to clean code.

High Availability Proxy

Background Information. HAProxy, or High Availability Proxy is used by RightScale for load balancing in the cloud. HAProxy is installed with RightScale load balancer ServerTemplates. Load-balancer servers are also known as front-end servers. Generally, their purpose is to direct users to available application servers.

Haproxy

Ansible :

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery.Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

Ansible is the simplest way to automate apps and IT infrastructure. … Provisioning is the first step in an application’s deployment process. In a cloud environment, software can be run from a Docker container, virtual machine or bare metal, and Ansible can be used for provisioning such systems.

Ansible

Hopefully, now you have a little bit of idea about above mentioned So let’s see what are the pre-requisites and how you can design the below-mentioned architecture of loadbalancer using ansible.

Pre-requisites :

  1. Configured Ansible Controller Node.
  2. IAM User
  3. At least 4 AWS ec2 Instances

Configure Ansible Controller Node :

Now you have to install ansible on your system that’s known as a controller node. So if your controller node isn’t configured then you can visit the below-mentioned article where you will find lots of information about the installation of Ansible.

IAM User :

Create an IAM Account with programmatic access to get the access and secret key so that we can access the AWS services. Let’s see how to create . I am showing you step by step..

First go to the IAM service of AWS and in that select add user..

Here give user name to your IAM user and select both option of access type and if you want to give auto-generated password then select option in console password. After all this click on next for permission.

In permissions if you create our own policies then you should do that but , here I am using existing policy that is “PowerUserAccess” . After that give tag to your user by clicking next.

Give tag that you want to your user. And click next for review and create user.

Here you found Access key and Secret key for your user. You should copy this or download the .csv file of the keys. It is used when we create playbook. That’s all this is an process for creating the IAM user . But if you are not comfortable with this then refer below blog..

Here our two pre-requisites are done..

Now, here we use a new concept for storing our access key and secret key in our OS for future in ansible playbook.

Create Vault :

Ansible Vault is a feature of ansible that allows you to keep sensitive data such as passwords or keys in encrypted files, rather than as plaintext in playbooks or roles. These vault files can then be distributed or placed in source control.To enable this feature, a command line tool, ansible-vault is used to edit files, and a command line flag — ask-vault-pass or — vault-password-file is used. You can also modify your ansible. cfg file to specify the location of a password file or configure Ansible to always prompt for the password.

So simply run the below command and put your access and secret key inside it which you downloaded from your IAM Account and then save it with your suitable password.

“ ansible-vault create (vault file name).yml ”

Here I am creating IAMkey.yml file for storing the access key and secret key of our IAM user.

Now For using AWS console by doing any program we have to install some extra software that is “ boto ” .

Install boto library :

Here we can use our localhost IP address to behave as a managed node and we will use the SDK to launch the ec2 instance on AWS as Ansible is built on python language so we will be using boto. Boto as it an API so it has the capability to contact AWS. So install boto in your controller node.

“ pip3 install boto3 ”

But some time if it not working then you install the previous version of boto3 that is “boto”.

Now we can create playbook for installing the ec2 instances using ansible..

Write a playbook for launching the Instances :

After installing the boto library, you can provision the EC2 instances through the Ansible playbook. So just create the playbook and write the below code which is given in my github account.

After creating the playbook, run it with your vault using the below command, and give your password which you gave during the vault creation.

“ ansible-playbook — ask-vault-password (playbook-name).yml ”

So after running the playbook, let’s see your EC2 Dashboard and check the instances are provisioned or not. and write your private IP somewhere which you will get after running a playbook.

Give user login permission for a managed node through Ansible. cfg to the controller node :

Now if you want to login to your EC2 instances dynamically, So write the below code in your ansible.cfg file. Because by default, login of the root has disabled. So you can’t log in with the root account in AWS. So go inside the below-mentioned location of the controller node and write the below syntax.

Create Inventory File :

Now your config file has been configured in the controller node so you can do anything on your instances (managed nodes) whatever you want to do so write the private IP and username which you got when you run the playbook for provisioning the Instances.

And check that your managed nodes are available or not using the below command.

ansible all --list-hosts

Check your EC2 Instances (Managed nodes) are pingable or not.

ansible all -m ping

After checking all these things, create the roles to deploy the web server and load balancer in the Managed nodes (EC2 Instances).

Create Roles for deploying the Webserver and load balancer :

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. Role directory structure. Storing and finding roles. So use the below command for creating the role.

ansible-galaxy init (name you want to give to role)

So after creating roles for load balancer and webserver, Install HAProxy software in your controller node using the below command.

yum install haproxy 

After installing HAProxy, go inside /etc/haproxy/haproxy.cfg and copy haproxy.cfg into directory where you want . Here you can use the cp command to copy the haproxy.cfg file.

Then open the haproxy.cfg file which is copied and binds the port 8080.

Also, write the below-mentioned jinja code to update the haproxy.cfg file to load balancer dynamically.

After doing all this setup we have to write playbook for roles. Open your lbrole/tasks/directory and write the below code inside main.yml to configure the load balancer.

Now for putting any html page we have to create this first . Use below code for creating the web page..

Now open your webrole/tasks/directory and write the below code inside main.yml to configure the load balancer.

So there are two tasks are created. One is for WebServer and another one is for Load Balancer.

Create a Playbook for Roles :

Now create a playbook to run your roles which contains only the information of your load balancer and web server instances and the location of roles path.

Now run the playbook using the below command.

ansible-playbook playbookname.yml

After running the playbook, let’s both webservers and load balancer are working properly. So take the IP of a load balancer and browse with (LoadBalancer_IP):8080.

Here we can see our Load-Balancer is working create . Because you can see that the IP address of this web-pages are different.

Here you have learned how we can launch the AWS instances as well as you saw the about roles and at the end of the article, your all setup is ready to update and configure the load balancer and webservers dynamically. Here you can add how many web servers you want, only write the information about the operating system in an inventory file, and run the playbook.

Yes..!! Here I am explain every bit of code . Hope you are learn something from it.

****If you like it then Clap and Share….!!!!

  • *If you like it then follow me in LinkedIn ………

And For any suggestion feel free to connect me in LinkedIn..

Thanks, Everyone for reading. That’s all…

--

--

Get the Medium app