Using Apache for Load Balancing
Using Apache for Load Balancing
This article uses Apache as the load balancer in conjunction with the Apache module mod_proxy and mod_proxy_balancer. Both are available through CentOS, which this article is written for. You may wish to use the same configuration on Red Hat Enterprise Linux or Ubuntu, however, some steps may vary.
The goal of this article is to put into practice the ability to use dedicated or cloud servers to be able to scale horizontally. When you need to add more resources, adding web-heads (or drones) behind the smart host will be a quick and simple task.
You need to have the following hardware and software in place before you begin.
You will require three servers to start, however, you can use this model to scale horizontally.
-One server for the load balancer
-Two servers minimum for the web heads
The software for the three servers will need to be the same; they will run the same packages. To do this, you will need to add two groups to the default CentOS install.
1) Update your system
# yum update
2) Install Apache by using the CentOS ‘groupinstall’ command
# yum groupinstall "Web Server"
3) You may also wish to install a text-based web browser in the event that you need to check that a particular server is displaying the page it is meant to, behind the load balancer.
# yum groupinstall "Text-based Internet"
The web servers (web heads) do not require special configurations. One recommendation is to place a file called index.html in /var/www/html/index.html. You can then simply put a short message and the web head device number in the body (ex: “Hello World! www-1”), and during testing, the index page will indicate what web head you are looking at.
Here is the key to the entire effort. Place all configurations that are enabled in the file /etc/httpd/conf/httpd.conf in a standard virtual host.
Turn off ProxyRequests to avoid superfluous traffic.
Balance web heads
In this part of the virtual host, name the web heads and declare how you will be balancing. The ‘BalanceMember’ directive is how you declare the web heads. You can add as many as you like, using these as templates. The ‘ProxySet’ directive declares how you want to balance. This example uses a “byrequest” balancing algorithm, which is the same as a round robin, so for each new request you get a new web head. The order is sequential. (Although better and smarter algorithms exist, this one is easy to configure and you don’t need to know networking theory.) All of this is wrapped in ‘<Proxy>’ tags, which is how Apache knows to send it to ‘mod_proxy’. The ‘balancer://mycluster’ identifier is only an identifier; you could call it what you want as long as you use the ‘balancer://’ prefix.
NOTE: You will want to use the internal IP addresses on the ‘BalancerMember’ configuration on the load balancer to ensure that the fastest route is always taken (internal vs a possible external request)
The balance-manager is a tool packaged with the mod_proxy_balancer tool, and it enables you to make configurations from a GUI tool through the web browser. You can view it at http://domain.tld/balancer-manager (substituting your own domain or IP). Consider that any changes made by this tool end after you restart Apache.
This is the last part of the configuration, and adds the situations that will need to be proxied. You don’t want to proxy the balancer-manager, but you do want to proxy everything else.
ProxyPass /balancer-manager !
ProxyPass / balancer://mycluster/
If you have all this configured in your httpd.conf file on your load balancer cloud server, and you start Apache, you should be able to view your domain name that is properly pointed to your load balancer. When you refresh, it should hop between your two web heads, saying “Hello World www-1” or “Hello World www-2”. You are now balancing.