Load balance with NGINX

 Load balance with NGINX

In this blog I will explain how to do a load balance using Nginx and how to work with Ansible to connect several nodes and do some operations with it. I did it using the Cent OS which installed into my Windows machine.


Creating Virtual Machines

First of all you need to install some virtual machine into your pc. In here I suggest the VMWare Workstation. You can install it from https://www.vmware.com/latam/products/workstation-pro/workstation-pro-evaluation.html. After installing the VMWare Workstation you need to download CentOS 7. You can view the installation guide from https://phoenixnap.com/kb/how-to-install-centos-7. After successfully installation the CentOS into your VM you can view as below.


Consider this as your main server. Then create another two clones with it and name those as "Webserver1" and "Webserver2".



Setting static IP adresses

Now you can set static IP addresses for the three servers. This is not mandatory but it will be helpful in the future. We do this because if your connection is change then the IP addresses will change. Then whole thing will not work. So it is better to set static IP addresses.

To do that go to settings in the CentOS and select the network tab. Then go to IPv4 and set IPv4 Method to manual. Then you can set the addresses as below,


Do this to all three servers. Make sure you set the same subnet mask and gateway for the all three servers. I set it as below,

Main VM - 192.168.43.52
Webserver1 - 192.168.43.237
Webserver2 - 192.168.43.50


Installing Apache

Then you have to create the web pages. To do that first of all you need to install the Apache server to Webserver1 and Webserver2. To do that run "yum install httpd". If this doesn't work properly it because we set the static IP addresses. Set it to the default and run it again.


After installing apache you can go to the installation path "cd /etc/httpd" and list down files by "ls". You can see the status of the apache server by running "systemctl status httpd". If you need to run the apache server "systemctl start httpd" and to stop it run "systemctl stop httpd"



Creating web pages

Now we need to create our web pages. Go to www by "cd /var/www" and create a html page. Run "vi index.html". We just need a single page to demonstrate this so we only create the index page. When the file is open in vi editor just type some sample text like "HELLO THIS IS SERVER 1".

Do the same thing for the Webserver2 and set text to the index.html as "HELLO THIS IS SERVER 2". Now we have two web pages in two servers. Start the apache server and browse the IP address and it should run the html files.

Additionally we can set DNS to this servers. Go to /etc/hosts and it will open a file. Do the changes as below image to it,


192.168.43.237 is your instance's IP address (you can get it by running "ifconfig"), and "webserver1.local" is your domain name. After setting this up, you can run the page by this name in the browser.




What is Nginx,

Nginx is one of the most efficient and reliable web servers available in 2017. Nginx works really well for serving static files. You can let requests for static files and assets pass directly through to the file system and Nginx will serve them really fast, without touching your web app processes.

Advantages of Nginx

● Installations and configurations are simple and easy
● Fastest and the best for serving static files
● Dynamic content transformed into static content
● When compared to Apache, 4 times more concurrent connections are handled.
● Load Balancing Support
● Compatibility with commonly-used web apps
● No risk in switching over to Nginx


Installing Nginx

To install Nginx run "sudo yum install nginx". Do this in the main server because we are balancing the requests from the main server. After installation config the /etc/nginx/conf.d/app.conf file as below (set the two IP addresses),



Demonstration of load balance

Now run the Nginx server "systemctl start nginx.service". Load the browser with main server's IP address and refresh the page multiple times. You will see the response will change time to time. This because the Nginx server will balance the request by sending the response from two servers.



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 Features

1. Configuration Management
2. Application Deployment
3. Orchestration
4. Security and Compliance
5. Cloud Provisioning

Advantages of Ansible

1. Very simple to set up and use - No special coding skills are necessary to use Ansible playbooks
2. Powerful - Ansible lets you model even highly complex IT workflows
3. Flexible - can orchestrate the entire application environment no matter where it's deployed

 

Install and configure Ansible

First of all install Ansible into all three servers. Run "sudo yum install ansible". Then config the /etc/ansible/hosts file. You have to create a group including all your machines in this file. I created "webservergroup" and included the IP addresses of my Webserver1 and Webserver2.



Setting up the RSA key

Then you need to create the RSA key pair to connect the servers. We create this because of the security purposes. Run "ssh-keygen -t rsa" in the main server which Ansible is installed.


Then copy the RSA key to the two servers by running "ssh 'root@<IP address>'".



Run commands through Ansible

After setting up the key you can run "ansible -m ping all" or "ansible -m ping <your group - in my case it is webservergroup>" to check all the connections are set.

Now you can do any command using ansible and it applicable to all the servers in the created group at once. Or you can run the command to individual or only selected machines using Ansible.

Thank you!

Comments

Post a Comment

Popular posts from this blog

First Step to Laravel

Send Email through Laravel