Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Cloud Zone is brought to you in partnership with:

Bradley Holt is a web developer, entrepreneur, community facilitator, speaker, and an author. He is the co-founder of Found Line, a creative studio with capabilities in web development, web design, and print design. He is a Board Member at Vermont Community Access Media, a non-profit community media and technology center. He is a minor contributor of source code and bug reports to Zend Framework and an active member of the PHP community. He organizes the Burlington, Vermont PHP Users Group where he is a regular speaker and is involved with helping to organize other technology community events such as Vermont Code Camp. He has spoken at (or will be speaking at) SXSW Interactive, OSCON, OSCON Data, the jQuery Conference, and ZendCon. He is the author of Writing and Querying MapReduce Views in CouchDB and Scaling CouchDB, both published by O’Reilly Media. He blogs at bradley-holt.com and can be found on Twitter as @BradleyHolt. Bradley is a DZone MVB and is not an employee of DZone and has posted 15 posts at DZone. You can read more from them at their website. View Full User Profile

Load Balancing with Apache

01.02.2012
Email
Views: 3366
  • submit to reddit
The Cloud Zone is presented by DZone and Microsoft. Let our tutorials, design patterns, and news guide you through the maze of constantly increasing cloud solutions.  Microsoft has a host of tools to let you deploy Node.js, PHP, and Java apps on their Windows Azure platform.

Recently at a Burlington, Vermont PHP Users Group meeting I gave a presentation on Load Balancing with Apache:

Load Balancing with Apache

 

View more presentations from Bradley Holt

 

I’ve posted the example configuration files for reference.

 

Basic load balancing:

# 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" balancer
ProxyPass / balancer://web-nodes

Sticky sessions in PHP:

# 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" balancer
ProxyPass / balancer://web-nodes

Create your own sticky sessions:

# Set a cookie
Header 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" balancer
ProxyPass / balancer://web-nodes

Route based on HTTP method:

# Enable mod_rewrite
RewriteEngine On

# Send POST, PUT, and DELETEs to "write" balancer
RewriteCond %{REQUEST_METHOD} ^(POST|PUT|DELETE)$
RewriteRule ^/(.*)$ balancer://write$1 [P]

# Send GET, HEAD, and OPTIONS to "read" balancer
RewriteCond %{REQUEST_METHOD} ^(GET|HEAD|OPTIONS)$
RewriteRule ^/(.*)$ balancer://read$1 [P]

# Modify HTTP response headers (e.g. Location)
ProxyPassReverse / balancer://write
ProxyPassReverse / balancer://read

Distributed load testing with Tsung:

<?xml version="1.0"?>
<!DOCTYPE tsung SYSTEM "/usr/share/tsung/tsung-1.0.dtd">
<tsung loglevel="notice" version="1.0">

  <!-- 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>

  <!-- Server side setup -->
  <servers>
    <server host="www" port="80" type="tcp" />
  </servers>

  <!-- Load setup -->
  <load>
    <arrivalphase phase="1" duration="5" unit="minute">
      <users arrivalrate="200" unit="second" />
    </arrivalphase>
  </load>

  <!-- Session setup -->
  <session  name="default" probability="100" type="ts_http">
    <thinktime  value="1" random="true" />
    <request>
      <http method="GET" url="/" />
    </request>
  </session>

  <!-- Monitoring setup -->
  <monitoring>
    <monitor host="www" type="munin" />
    <monitor host="www1" type="munin" />
    <monitor host="www2" type="munin" />
    <monitor host="www3" type="munin" />
  </monitoring>

</tsung>

I cover load balancing and distributed load testing in more detail in my book, Scaling CouchDB.

 

Source: http://bradley-holt.com/2011/03/load-balancing-with-apache/

Tags:
Published at DZone with permission of Bradley Holt, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Whether it's IaaS or PaaS, there are many options and features for developers to consider when deploying applications to cloud environments.  Cloud Zone is your trusted guide through the jungle of diverse cloud solutions. Get clear cut information on solutions like Windows Azure, open and flexible cloud platform to develop, deploy and manage applications on Microsoft's datacenters.  You can see how well your apps run on Azure with their free 3 month trial.