Monday 7 March 2016

Load balancing with Apache Karaf Cellar

Load balancing with Apache Karaf Cellar

Consider your application have different components like CXF services, Camel Routes, DOSGi services, Distributed Session.... etc. If you can deploy your applications on several Karaf nodes and with help of Cellar, you may need load balancing for CXF/HTTP endpoints, need is to load balance the HTTP requests on the Karaf nodes.


Apache httpd with mod_proxy_balancer

There are different ways to do that and here i am explaining Apache httpd with mod_proxy_balancer. It’s a very stable solution, easy to setup.

Consider you have three Karaf nodes, having the following services:
  • http://192.168.1.10:8181/services
  • http://192.168.1.11:8181/services
  • http://192.168.1.12:8181/services

install Apache httpd

# on Debian/Ubuntu system
aptitude install apache2

# enable network connect on httpd
/usr/sbin/setsebool -P httpd_can_network_connect 1

# on CentOS/RHEL/Fedora system
yum install httpd

Just check if mod_proxy, mod_proxy_http, and mod_proxy_balancer modules are there in the main httpd.conf.

Edit the main httpd.conf or create a conf file in etc/httpd/conf.d and add

<Proxy balancer://mycluster>
  BalancerMember http://192.168.1.10:8181
  BalancerMember http://192.168.1.11:8181
  BalancerMember http://192.168.1.12:8181
</Proxy>
ProxyPass /services balancer://cluster

This load balancer will easily proxy the /services requests to the different Karaf nodes.
The mod_proxy_balancer module is able to support session as well. Means, when a request is proxied to one node, then all following requests from the same user should be proxied to the same node.
For this, you can use the cookie in header to define the session.

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://mycluster>
  BalancerMember http://192.168.1.10:8181 route=1
  BalancerMember http://192.168.1.11:8182 route=2
ProxySet stickysession=ROUTEID
</Proxy>
ProxyPass /myapp balancer://mycluster

mod_proxy_balancer: web manager

This allows you to see if your Karaf nodes are up or not, the number of requests received by each node, and the current load balancing method in use and all.

<Location /balancer-manager>
  SetHandler balancer-manager
  Order allow,deny
  Allow from all
</Location>

Point your browser to http://host:port/balancer-manager and you will see the manager page.

Reference:

http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html.



6 comments: