Apache Webserver and Haproxy server configuration on AWS with Ansible
Hello connections, in this blog i am going to configure Apache webserver and Haproxy server on AWS EC2 instance using Dynamic Inventory of Ansible playbook, i am going to write a ansible playbook for this whole setup. First of all, before configuration we should have basic knowledge about Ansible Dynamic Inventory, Ansible playbook, Apache web server, Haproxy server and about AWS EC2 Instance.
Requirements to Implement this setup:
- You should have a AWS Acount.
- you should have a IAM User.
- you should have Ansible with python interpreter in your Control Node
In this setup Controller Node of Ansible will be my local VM with OS RHL8, while Target Node will be AWS EC2 Instance.
Lets Implement the setup
Step 1
First of all i am going to launch three AWS EC2 Instance using ansible playbook, I use one of instance to run haproxy server and two instance to run webserver.
Above playbook launch two AWS EC2 instance to configure as web server.
Above playbook launch one AWS EC2 instance to configure as haproxy server.
Three AWS EC2 Instance are created as above we can see.
Step 2
In this step i am gonna setup Dynamic Inventory on my Controller Node. Dynamic Inventory helps us to fetch the ip address of our Target Node, it means at runtime of playbook ip of Target Nodes are fetched. To set Dynamic Inventory we need two files ec2.py and ec2.ini. To download these file use the link with wget command on terminal
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
Step 3
After downloading these files we need to make both files executable, we can do so with the command chmod as
chmod +x ec2.py ec2.ini
To run these files we need boto and boto3 SDK, we need to install these SDK in our system i install these libraries in my system with command pip3 install boto boto3.
Step 4
we need to do some modification on these files. In ec2.py, we need to update the path of python interpreter.
And in file ec2.ini we need to set our AWS access key along with secret key of IAM user.
Step 5
Along with above setup we need to export AWS Access Key, AWS Secret Key and AWS Region as Shell variable by running command on terminal:-
export AWS_ACCESS_KEY= 'put your access key'
export AWS_SECRET_KEY= 'put your secret key'
export AWS_REGION='put your region'
Now we need to update our ansible config file with these executable files. To do so i create a folder with name mydb with command mkdir /mydb, and move both files on mydb, and update the ansible config file as shown.
After this setup we need private key of AWS in Controller Node, by which we create EC2 Instance. so i put the private key on Controller Node.
And change the file permission of my private key file by using command: chmod 400 master.pem
Step 6
Run command ansible all --list-hosts to show list of ips of hosts(Target node)
Step 7
To check connectivity with Target Node from Control Node we can run the command:- ansible all -m ping, here in my setup i can able to ping my Target Node from Control Node as shown.
Step 8
Run ec2.py file on terminal to get tag name of web server and haproxy server. we use these tag name in our playbook.
Step 9
Now to configuring Haproxy Remotely first we need to install the Load Balancer locally and need to do some configuration in the haproxy conf file, to install haproxy use command :- yum install haproxy -y
Now go inside the configuration file of haproxy Server /etc/haproxy/haproxy.cfg and make the changes as below. In this config file, i set the port no. 8000 to serve the service.
Step 10
Now, write ansible playbook to configure web server and haproxy server on EC2 Instance.
Step 11
Run playbook using command: ansible-playbook <playbook_name.yml>, my playbook ran successfully.
Step12 :- Testing the setup
haproxy server running successfully.
Apache httpd web server also running successfully. To access the web page we need to use Ip of haproxy server, that serve our web page on port 8000, as we set port no. of haproxy server 8000.
Finally, got result the whole setup done successfully.
Happy Reading !!
Thank You !