Having a proper set up of load balancer allows your web server to handle high traffic smoothly instead of crashing down.
Bind.:3389 ssl crt. Means HAProxy will insist on negotiating SSL with the client itself, and handling the payload in the clear, internally. Ssl is used on the backend, HAProxy will negotiate a new SSL session with the back-end server and re-encrypt the payload, otherwise it will pass it to the server in the clear. $ cd haproxy-rest $ haproxy -f resources/haproxyinit.cfg -p resources/haproxy-private.pid -sf $(cat resources/haproxy-private.pid) Build the program and run it. $ go build $./haproxy-rest If you're on Mac OSX or Windows and want to compile for Linux (which is probably the OS you're using to run HAproxy), you need to cross compile.
What is load balancing?
Load balancing is the process of distributing workloads to multiple servers. It is like distributing workloads between day shift and night shift workers in a company. Load balancing improves the server’s reliability as it overcomes single point failure.
An example of How a server without load balancing looks like is shown below.
In this example, if the web server goes down, the user’s web request cannot be accessed in real time. Also if numbers of users request the same web page simultaneously, then serving the user’s web request by a single web server can be a slow process. Hence load balancers are used to enhance the server’s performance, provide backup and prevent failures.
In this tutorial, we are going to set up a load balancer for web server using Nginx, HAProxy and Keepalived.
An example of how servers with load balancers look like is shown below.
So, what are Nginx, Haproxy and Keepalived?
Nginx
Nginx, pronounced as Engine-x is an open-source Web server. More than just a Web server, it can operate as a reverse proxy server, mail proxy server, load balancer, lightweight file server and HTTP cache. Nginx has been used in many popular sites like BitBucket, WordPress, Pinterest, Quora and GoDaddy.
HAProxy
HAProxy stands for High Availability Proxy. It is an open source load balancer that provides load balancing, high availability and proxy solutions for TCP and HTTP based applications. It is best suited for distributing the workload across multiple servers for performance improvement and reliability of servers.
The function of Haproxy is to forwards the web request from end-user to one of the available web servers. It can use various load balancing algorithms like Round Robin, Least Connections etc.
Keepalived
What if HAProxy load balancer goes down?
Keepalived is an open-source program that supports both load balancing and high availability. It is basically a routing software and provides two types of load balancing:
- Layer 4 ( transport layer)
- Layer 7 ( application layer)
Keepalived can perform the following functions:
- Health checking ( whether the servers are up or not)
- Implements VRRP (virtual redundancy routing protocol) to handle load-balance failover
Keepalived uses VIP (Virtual IP Address) as a floating IP that floats between a Master load balancer and Backup load balancer and is used to switch between them. If Master load balancer goes down, then backup load balancer is used to forward web request.
Let’s move towards simulation of how high availability and load-balancing is maintained for web servers.
Setting up a load balancer in Linux with Nginx, HAProxy and Keepalived
This is a test lab experiment meaning it’s just a test setup to get you started. You may have to do some tweaking if you are implementing it on real servers. Use this tutorial as a learning material instead of blindly following it for your own setup.
I have used CentOS Linux distribution in this tutorial. You can use other Linux distributions but I cannot guarantee if all the commands (especially the installation ones) will work in other distributions.
Requirements for load balancer setup
4 CentOS installed systems (minimal installation is enough for this tutorial)
- 2 CentOS to be set up with nginx
- 2 CentOS to be set up with HAProxy and Keepalived
In this tutorial, we have worked on the following IP addresses as an example. These can be changed as per your system. Don’t think these are the static IPs.
Web servers:
- 10.13.211.169
- 10.13.211.158
LoadBalancer:
- 10.13.211.194
- 10.13.211.120
Virtual IP:
- 10.13.211.10
You can easily get IP address in Linux command line.
Step 1: Setup the web servers with Nginx
In this part, we’ll use two CentOS systems as the web server. We need to install Nginx on them first.
For that, add a repository containing nginx and then install it from there:
After installing nginx, start the Nginx service:
Make nginx service to be enabled even after every boot:
Check the status of nginx service:
Allow the web traffics in nginx that is by default block by CentOS firewall.
Repeat the above steps on the second CentOS web server as well.
Now pay attention to the next steps.
Haproxy For Windows Download Windows 7
The web files for nginx is located in
/usr/share/nginx/html
Change the content of index.html file just to identify the webservers.For the first web server:
For the second web server:
NOTE: If you are on a virtual machine, it is better to install and configure Nginx on one system and then clone the system. Afterward, you can reconfigure on the second system. Saves time and errors.
Now confirm the web server status by going to the following URL in your browser: http://SERVER_DOMAIN_NAME or Local_IP_Address. Example here:
Or in the terminal, curl Local_IP_Address. Example here:
You will get the output like:
Step 2: Setup load balancers with HAProxy
On the other two systems, use the following commands to install HAProxy:
HAProxy configuration file is located at /etc/haproxy. Use the cd command to go to the directory and backup the file before edit.
Haproxy For Windows Download Windows 10
Create a new haproxy.cfg file and open the file with any editor you like.
Now, paste the following lines into the file:
Now, enable and start HAProxy service.
Check the status of HAProxy:
Go to url in your browser to confirm the service of haproxy: http://load balancer’s IP Address/haproxy?stats. Example used here:
or in the terminal, use command $ curl LoadBalancer_IP_Address
curl two times and you will see different outputs for the curl command. It is because of the response is coming from different web servers (one at a time), for your request at the load balancer.
The Output would look like this:
![Haproxy Haproxy](/uploads/1/1/9/3/119320052/360079535.jpg)
Step 3: Set up high availability with Keepalived
Keepalived must be installed to both HAProxy load balancer CentOS systems (which we have just configured above). One acts a master (main load-balancer) and another acts as the backup load-balancer.
On both system, run the following command:
The configuration file of Keepalived is located at
/etc/keepalived/keepalived.conf
. Backup the original keepalived.conf file and use the following configuration at new keepalived .conf file.Paste the following lines to the configuration file (don’t forget to change the email addresses):
Note: Virtual IPs can be any live IP inside your network. Near about the range of Loadbalancer’s IP Address. Here, the load balancer’s IP are: 10.13.211.194 & 10.13.211.120, and VIP is 10.13.211.10
Edit the configuration file as per the system assumption. Take care on master and backup configuration. Save the file and start and enable the Keepalived process:
To view the status of Keepalived:
Note: If you are on a virtual machine, it is better to install and configure Haproxy and Keepalived on one system and then clone the system. Afterward, you can reconfigure on the second system. Saves time and errors.
Now to check the status of your high-availability load-balancer, go to terminal and hit:
Windows Download Iso
Hit
ctrl+c
to stop the terminal run.The output will look like this:
If you feel uncomfortable in installing and configuring the files, download the scripts form my GitHub repository and simply run them.
I hope this tutorial helped you to set up a load balancer in Linux with high availability. Of course, it was a simple setup but it definitely gives an idea about load balancing and handling high availability.
Haproxy Install
If you have questions or suggestions please leave a comment below.
Become a Member for FREE
Windows Download Tool
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only content
![Rpm Rpm](/uploads/1/1/9/3/119320052/542775286.png)
Join the conversation.