Load Balancing with Apache

46,491 views
46,215 views

Published on

This is a presentation made at the Burlington, Vermont PHP Users Group about configuring load balancing using the Apache HTTP Server. Load balancing is a technique that can distribute work across multiple server nodes—here we will discuss load balancing HTTP (i.e. web) traffic. There are many software and hardware load balancing options available including HAProxy, Varnish, Pound, Perlbal, Squid, nginx, and Linux-HA (High-Availability Linux) on Linux Standard Base (LSB). However, many web developers are already familiar with Apache as a web server and it is relatively easy to also configure Apache as a load balancer.

Related concepts such as shared nothing architecture are discussed. We also take a look at some basic load balancing scenarios and features including sticky sessions and proxying requests based on HTTP method. Distributed load testing with Tsung is briefly discussed as well.

Published in: Technology
0 Comments
48 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
46,491
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
1,148
Comments
0
Likes
48
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Load Balancing with Apache

    1. 1. Load Balancing with Apache Bradley Holt (http://bradley-holt.com/)@BradleyHolt (http://twitter.com/BradleyHolt)
    2. 2. About Me
    3. 3. Co-Founder andTechnical Director
    4. 4. Contributor
    5. 5. Author http://oreilly.com/catalog/9781449303129/ http://oreilly.com/catalog/9781449303433/
    6. 6. Apache HTTP Server
    7. 7. About ApacheOpen sourceServes over 100 million websitesCan run in many different modes, depending on your needs,via MultiProcessing Modules (MPMs)The Apache Software Foundation (ASF) supports many otheropen source software projects
    8. 8. Alternatives
    9. 9. Software Load BalancersHAProxyVarnishPoundPerlbalSquidnginxLinux-HA (High-Availability Linux) on Linux Standard Base (LSB)
    10. 10. Hosted Load BalancersAmazon’s Elastic Load BalancingRackspace Cloud Load Balancers
    11. 11. Shared Nothing Architecture (SN)
    12. 12. Properties of a SN SystemEach node operates independentlyNo single point of contentionPHP is shared nothing by default…
    13. 13. Breaking SNSessions require sharing (or require the use of sticky sessions)Databases are the most common single point of contention(this is why eventual consistency is important)Storing variables in memory between requests breaks SN(possible in Java and .NET)
    14. 14. Practical SN TechniquesStore sessions in a memcached cluster—this makes web nodes SNFor database applications: • send writes to a write-only master • replicate to multiple read-only nodes • read-only nodes are now effectively SNClustering/partitioning/sharding can help, too (but painful).Alternatively, use a “NoSQL” database (e.g. CouchDB is SN).
    15. 15. Load Balancing Examples
    16. 16. Required Modulesmod_proxymod_proxy_http (assuming you’re load balancing HTTP requests)mod_proxy_balancermod_headers (for sticky sessions)mod_rewrite (for advanced con gurations)
    17. 17. Basic Load Balancing
    18. 18. # Create a load balancer named "web-nodes"<Proxy balancer://web-nodes> # Add three load balancer members BalancerMember http://www1.example.com BalancerMember http://www2.example.com BalancerMember http://www3.example.com</Proxy># Send all requests to the "web-nodes" balancerProxyPass / balancer://web-nodes
    19. 19. Apache allows traffic to bebalanced by number of requests(lbmethod=byrequests), bytestransferred (lbmethod=bytraffic),or by the number of currently pendingrequests (lbmethod=bybusyness).
    20. 20. Sticky Sessions in PHP(your mileage may vary)
    21. 21. # Create a load balancer named "web-nodes"<Proxy balancer://web-nodes> # Add three load balancer members BalancerMember http://www1.example.com BalancerMember http://www2.example.com BalancerMember http://www3.example.com # Use the PHPSESSID for sticky sessions ProxySet stickysession=PHPSESSID</Proxy># Send all requests to the "web-nodes" balancerProxyPass / balancer://web-nodes
    22. 22. Create Your Own Sticky Sessions
    23. 23. # Set a cookieHeader add Set-Cookie "NODE=%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED# Create a load balancer named "web-nodes"<Proxy balancer://web-nodes> # Add three load balancer members BalancerMember http://www1.example.com route=1 BalancerMember http://www2.example.com route=2 BalancerMember http://www3.example.com route=3 # Use the NODE cookie for sticky sessions ProxySet stickysession=NODE</Proxy># Send all requests to the "web-nodes" balancerProxyPass / balancer://web-nodes
    24. 24. Use a private network to connect yourload balancer to your balancermembers.This allows for dedicated bandwidthand opens up the possibility ofoffloading SSL handling to the loadbalancer.
    25. 25. Route Based on HTTP Method
    26. 26. # Enable mod_rewriteRewriteEngine On# Send POST, PUT, and DELETEs to "write" balancerRewriteCond %{REQUEST_METHOD} ^(POST|PUT|DELETE)$RewriteRule ^/(.*)$ balancer://write$1 [P]# Send GET, HEAD, and OPTIONS to "read" balancerRewriteCond %{REQUEST_METHOD} ^(GET|HEAD|OPTIONS)$RewriteRule ^/(.*)$ balancer://read$1 [P]# Modify HTTP response headers (e.g. Location)ProxyPassReverse / balancer://writeProxyPassReverse / balancer://read
    27. 27. Consider con guring multiple loadbalancers, removing the load balanceras a single point of failure.This typically involves having two ormore load balancers sharing the sameIP address, with one con gured as afailover.
    28. 28. Distributed Load Testing with Tsung
    29. 29. TsungDistributes load testing across multiple testing clientsCan generate huge numbers of concurrent usersMonitors CPU, memory, load, and network trafficSimulates dynamic sessions, as described in a con guration leRandomizes traffic patterns based on de ned probabilitiesRecording and playback of sessionsHTML reports and graphs
    30. 30. XML Con guration File
    31. 31. <?xml version="1.0"?><!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd"><tsung loglevel="notice" version="1.0"> <!-- … --></tsung>
    32. 32. Client Side Setup
    33. 33. <!-- Client side setup --><clients> <client host="test-a" weight="1" maxusers="10000" cpu="4" /> <client host="test-b" weight="1" maxusers="10000" cpu="4" /></clients>
    34. 34. Server Side Setup
    35. 35. <!-- Server side setup --><servers> <server host="www" port="80" type="tcp" /></servers>
    36. 36. Load Setup
    37. 37. <!-- Load setup --><load> <arrivalphase phase="1" duration="5" unit="minute" > <users arrivalrate="200" unit="second" /> </arrivalphase></load>
    38. 38. Session Setup
    39. 39. <!-- Session setup --><session name="default" probability="100" type="ts_http"> <thinktime value="1" random="true" /> <request> <http method="GET" url="/" /> </request></session>
    40. 40. Monitoring Setup
    41. 41. Tsung allows for monitoringusing Erlang, SNMP, or Munin
    42. 42. <!-- Monitoring setup --><monitoring> <monitor host="www" type="munin" /> <monitor host="www1" type="munin" /> <monitor host="www2" type="munin" /> <monitor host="www3" type="munin" /></monitoring>
    43. 43. Reports
    44. 44. From Scaling CouchDB by Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
    45. 45. Graphs
    46. 46. From Scaling CouchDB by Bradley Holt (O’Reilly). Copyright 2011 Bradley Holt, 978-1-449-30343-3
    47. 47. Questions?
    48. 48. Thank You Bradley Holt (http://bradley-holt.com/) @BradleyHolt (http://twitter.com/BradleyHolt)Copyright © 2011 Bradley Holt. All rights reserved.

    ×