Contribute  :  Calendar  :  Advanced Search  :  Site Statistics  :  Directory  :  Web Resources  :  Polls  
    Nat's Geeklog Just another brick in the Geeklog wall    
 Welcome to Nat's Geeklog
 Friday, September 03 2010 @ 12:14 PM CDT

Apache Load Balancer Persistence

   
Computer Tech StuffApache 2.2's load balancer is pretty neat. However, to get persistence to work properly, you have to be careful. Here we are setting the balancer manager to watch a client cookie called BALANCEID, and each member has a particular route string tied to it that is set in the cookie. It's important to note that the cookie format must be: something.routestring, ie, nat.server1. If it is just server1, it will not work.

  ProxyPass / balancer://mycluster/ stickysession=BALANCEID
  ProxyPassReverse / http://localhost:71/
  ProxyPassReverse / http://localhost:72/
  <Proxy balancer://mycluster>
        BalancerMember http://localhost:71 route=server1
        BalancerMember http://localhost:72 route=server2
  </Proxy>
Now there's the issue of the client cookie being set. What if you are load balancing a third party app or webserver and can't easily get the cookie set on the client? No problem! While it didn't work in 2.2.3 (in particular, the BALANCER_ROUTE_CHANGED var being set), when I tried in 2.2.11, I was able to set the cookie myself based on which balancer member was selected:

  # Set session cookie if BALANCER_ROUTE_CHANGED, containing BALANCER_WORKER_ROUTE env variable, which is set to the route above
  # Note that cookie value should be a session id, followed by a period, followed by the route.
  # Since session id cookie usually not advised to be mutable, best create own cookie with anything you want
  # for the session part, just make sure to have a period and route part last

  ### Used for setting cookie
  LoadModule headers_module modules/mod_headers.so
  Header add Set-Cookie "BALANCEID=balancer.%{BALANCER_WORKER_ROUTE}e; path=/;" env=BALANCER_ROUTE_CHANGED

  # Just give some debug info in the header, don't use once you have it working
  Header add X-Var "BALANCER_ROUTE_CHANGED=%{BALANCER_ROUTE_CHANGED}e" env=BALANCER_ROUTE_CHANGED
  Header add X-Var "BALANCER_WORKER_ROUTE=%{BALANCER_WORKER_ROUTE}e" env=BALANCER_WORKER_ROUTE
  Header add X-Var "BALANCER_SESSION_ROUTE=%{BALANCER_SESSION_ROUTE}e" env=BALANCER_SESSION_ROUTE
Presto! Session persistence, all handled at the reverse proxy load balancer level.
 

What's Related

Story Options

Apache Load Balancer Persistence | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
 Copyright © 2010 Nat's Geeklog
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.04 seconds