VIEWS: 4 PAGES: 4 POSTED ON: 2/14/2013
Apache Load Balancing Today’s web performance and availability requirements make load balancing indispensable. In this article, we show you how to set up an effective load balancing system using features built into the Apache web server. BY ERIK ABELE multitude of technologies support load balancing for web servers. Load balancers come in all shapes and sizes, from simple DNS- based techniques through vast and versatile proprietary systems. In some cases, however, the load balancing features you need might be available already through the Apache web server. In this article, I describe some strategies for load balancing with Apache. The schematic in Figure 1 shows the underlying structure of a software-based balancing system. In this scenario, several front-end load balancers accept incoming user requests and distribute them to a pool of back-end servers on the basis of a predefined scheme. Multiple individual systems can run in parallel to provide a fail-safe (shown in the background in Figure 1). Apache includes a number of modules for supporting load balancing (Table 1), and you’ll need to make sure any modules you intend to use are loaded: 34 ISSUE 94 SEPTEMBER 2008 Apache Load Balancing LoadModule xyz_module a reverse proxy, or modules/mod_xyz.so gateway, to be more precise. Disabling Via As you can see in Table 1, Apache’s headers (ProxyVia) basic load balancing capabilities include makes the gateway features such as caching, compression, invisible. URL rewriting, and header processing. The ProxyPreserve- Some of the modules in Table 1 are Host and ProxyError- loaded by default. Override commands Consult your own Apache configura- ensure that the host tion for more on which modules might headers included in already be present on your system. the request are If JServ-capable application servers, passed on to the back such as Apache Tomcat or Jetty, are ends and that any used, the gateway can also use the error messages gener- Apache JServ Protocol (ajp). All you ated by the back ends need to do is load the mod_proxy_ajp are replaced by the module instead of mod_proxy_http and load balancer and thus standardized. ancer parameters cannot be modified by change the URLs from http:// to ajp://. The output of a suitable timeout, with rules: The use of this binary-format protocol ProxyTimeout, rounds off the basic offers a couple of advantages with re- configuration. ProxySet balancer://pool1 gard to back-end connection perfor- The core definition, that of a back-end lbmethod=bytraffic mance and lower resource overheads, pool and its members, is handled by a ... but this functionality is bought at the Proxy container and the specification RewriteEngine On price of more permanent connections of a special balancer:// schema followed RewriteRule ^/+(.*)$ to the back ends. by the pool name. The BalancerMember balancer://pool1/$1 [P,L] Incidentally, you can run ajp and http instructions and parameters in the con- back ends at the same time as members tainer specify the individual members Listing 1 thus defines two back-end serv- of the same pool. For the sake of com- along with their properties. ers for pool1. Requests are distributed on pleteness, keep in mind also that Apache At the end of the configuration, the the basis of the number of requests (see supports ftp proxy with the mod_proxy_ back-end pool defined previously is the lbmethod parameter). The load ftp module. assigned a separate URL space; more factor setting assigns twice as many For additional details, check out the parameters define the load balancer’s requests to server1 compared with Apache http server documentation . generic approach. To enable regular ex- server2. Connections are reused but also pressions, you could use the advanced restricted to a maximum value. The URL ProxyPassMatch command instead of space is defined as the complete URL The sample configuration shown in List- ProxyPass. space below /shop. ing 1 includes the basic front-end server As an alternative, the rewrite module Table 2 provides a summary of the settings for load balancing in Apache. (mod_rewrite) and custom rules would most common ProxyPass and Balancer- This configuration starts with Proxy- unleash the full power of regular expres- Member commands. For more informa- Requests to disable the normal proxy sions. However, in this case, you will tion, see the Apache http server docu- mode and setting up what is known as need to use ProxySet because load bal- mentation . Listing 1: Sample Configuration 01 ProxyRequests Off 12 The Apache http server’s proxy module (mod_proxy) provides an unbelievable 02 ProxyVia Off 13 BalancerMember http:// range of special settings. Tools are avail- 03 server2:8080 \ able for many different scenarios. For 04 ProxyPreserveHost On 14 min=5 max=25 loadfactor=1 05 ProxyErrorOverride On 15 </Proxy> Listing 2: mod_cache 06 16 Sample Configuration 07 ProxyTimeout 30 17 ProxyPass /shop balancer:// 01 CacheEnable disk / pool1 \ 02 CacheDisable /users 08 18 lbmethod=byrequests \ 09 <Proxy balancer://pool> 03 CacheRoot /var/cache/httpd 19 nofailover=Off maxattempts=3 10 BalancerMember http:// 04 ... \ server1:8080 \ 05 AddOutputFilterByType DEFLATE 20 stickysession=PHPSESSIONID 11 min=10 max=50 loadfactor=2 text/html SEPTEMBER 2008 ISSUE 94 35 Apache Load Balancing ward requests to a RequestHeader substitute system. set Front-End-Https "On" A number of standard variables and headers, such as proxy-nokeepalive, Another typical proxy-sendcl, X-Forwarded-For, or X- configuration is Forwarded-Server, saves typing and used to support makes life easier for administrators. sticky sessions: Other modules support caching or fil- tering of content generated by the back BalancerMember ends. In addition to improving perfor- http://server6: mance, caching also reduces the overall 1080... traffic volume and generally offloads stickysession= some of the work from the back-end JSESSIONID servers. Listing 2 enables a simple, file- based cache, including compression, for The stickysession the whole URL space /. (Just to demon- parameter, com- strate how the exclusion feature works, bined with the the whole URL space below /users has name of a cookie been excluded.) supported by the back ends, means that requests origi- If you loaded the status module (mod_ nating with individ- status) when you launched the server, ual users are always the proxy module also provides a simple sent to the same but practical web interface (Figure 2). back-end server. The simple configuration involves as- This kind of signing a handler: limited distribution ensures the persis- <Location "/.balancer-manager"> tence of the re- SetHandler balancer-manager quests, but it does </Location> interfere with the ... example, you can use the status parame- actual task of load balancing. ProxyPass /.balancer-manager ! ter to operate a hot standby server: To forward information to the back- end servers in a targeted way, or to influ- However, it is important to take access BalancerMember ence communications with the the back control into consideration and to exclude http://server4:1080... status=+H end, the proxy module also supports custom environmental variables and INFO This command specifies that server4 is http headers, which you can use to re-  Hypertext transfer protocol 1.1: only enabled if all the remaining pool strict the connections to the back end http://www.w3.org/Protocols/ members fail. or to advertise the use of SSL: rfc2616/rfc2616.html This server is the last line of defense  Apache documentation, httpd 2.2: and can be used to serve up a restricted SetEnv proxy-nokeepalive 1 http://httpd.apache.org/docs/2.2/en/ version of a web application or to for- ...  Apache Software Foundation: http://www.apache.org/ Table 1: Required Modules Module Function Erik Abele has worked for many mod_proxy Generic proxy module years as a freelance IT consultant. mod_proxy_balancer Balancer functions for the proxy module His international projects cover a full THE AUTHOR range of architectures and large mod_proxy_http Http support for the proxy module web farm operations. Erik is a long- mod_cache Generic caching module standing member of the Apache Software Foundation, where he mod_disk_cache File-based cache for the caching module takes an active part in the http mod_deflate Content compression module Server and HttpComponents proj- mod_rewrite Module for parsing and processing URLs ects. You can contact Erik via his websites: http://www.eatc.de/ or mod_headers Module for parsing and processing http headers http://www.codefaktor.de/. 36 ISSUE 94 SEPTEMBER 2008 with the Apress SUMMER ’08 HOTLIST processing individual URLs within the load balancer, which is achieved by means of a negative ProxyPass command. If you use the Apache rewrite module (mod_rewrite), you can define a separate rule to handle this case. Management is restricted to viewing the status of all config- ured balancers, disabling individual pool members, or modi- fying some basic settings, but it is extremely useful if you en- counter a problem, or if you wish to monitor multiple load balancers. Version 2.2 of the Apache http server offers a trouble-free, efficient, elegant, and scalable approach to load balancing in an http environment. Availability, a short learning curve, and nearly infinite flexibility all speak in favor of Apache. All told, the Apache load balancing system is a very sensible alterna- tive to popular commercial or open source alternatives. Check Table 2: Common Balancer Parameters www.apress.com/promo/hotlist Name status Explanation Balancer member status regularly for special sales loadfactor Normalized balancer member weighting and promotions! lbset The cluster set assigned to the balancer member lbmethod The request distribution method used by the balancer on the basis of either the For more information about Apress titles, number of requests (byrequests) or the please visit www.apress.com traffic volume (bytraffic) min Minimum number of permanent Don’t want to wait for the printed book? back-end connections Order the eBook now at max Maximum number of permanent http://eBookshop.apress.com! back-end connections maxattempts Maximum number of retries before denying a request stickysession Name of a persistent cookie used by the back-end server
"Load balancing with the Apache http server - Linux Magazine"