Tuesday, December 23, 2014

Load Balancing and failover with Jboss Applicaiton Server 7 and Apache Web Server

In my previous two blogs I demonstrated how to create Jboss cluster in standalone and domain mode respectively. But there I had to manually hop from one server url to another server url to demonstrate the clustering. But the real flavour of clustering is when we use it with any load balancer so that the user does not and should not manually hop from one server to another server. In fact the application gets connected to the load balancer which internally load balances the request and failovers the request from one server to another without the user even knowing it.

In this blog I be will using apache web server as my load balancer and will use jobss7 as my application server which will have a standalone mode of clusters of two nodes. The first step is to download and install apache web server from apache site. Use this link to download 2.2.25 installer package, download the version with openssl installer. http://olex.openlogic.com/packages/apache/2.2.25#package_detail_tabs

Once downloaded run the installer and it will install apache web server on the machine. By default apache listens to port 80. So, to check if its working properly after installation or not hit the url http://localhost:80 and you should see the output like below in the browser:-
















This means that apache is installed and working properly. It will also be installed as windows service so that we can use this service when we need to restart the apache web server.

The next step would be to download mod_cluster. User the below link to download mod_cluser1.2 final distribution.http://mod-cluster.jboss.org/downloads/1-2-0-Final. Use the windows version as per your system configuration. In my case i have used binaries windows-x-86. Once downloaded unzip the folder.

To configure apache load balancer we need to do configuration changes at both apache side as wll as jboss side. First lets do the apache configuration.

Go to the modules directory of the mod_cluster downloaded in the previous step. Inside the modules directory we can see lots of .so files. We need few of them to configure mod_cluster with apache. We will need the following files so copy the below files from the modules directory to the apachehome/modules directory.
1.mod_slotmem.so
2.mod_manager.so
3.mod_proxy_cluster.so
4.mod_advertise.so
5.mod_proxy.so
6.mod_proxy_ajp.so

Once the files are copied to the apachehome/modules folder we need to tell apache to load the modules. For that we need to edit httpd.conf file that can be found inside apachehome/conf directory. Open httpd.conf file and add the below entry:-

LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Then we need to configure the virtual host. To configure the virtual host add the below entry to the same file:-

<VirtualHost 127.0.0.1:80>
<Directory />
Order deny,allow
Allow from all
</Directory>
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order deny,allow
Allow from all
</Location>
EnableMCPMReceive
KeepAliveTimeout 60
ManagerBalancerName mycluster
AdvertiseFrequency 5
ServerAdvertise On
</VirtualHost>

We are now done with the configuration changes at apache end. Now its time to do some configuration changes at Jboss side. The first step would be create two nodes of jboss applicaton server as demonstrated in the previous blog. For my demo I will be having two nodes running in standalone mode, so make two copies of jboss7.1.0-Final and name them node-1 and node-2. The changes that I am going to explain has to be done at both the nodes of the jboss application server. Open standalone-ha.xml file. The first entry would be to <server> tag by adding the name attribute.

<server name="standalone-node1" xmlns="urn:jboss:domain:1.2">

Next is to add the below entry if its not already there:-

<subsystem xmlns="urn:jboss:domain:modcluster:1.0">
            <mod-cluster-config advertise-socket="modcluster" proxy-list="127.0.0.1:80" sticky-session="false">
                <dynamic-load-provider>
                    <load-metric type="busyness"/>
                </dynamic-load-provider>
            </mod-cluster-config>
        </subsystem>

And finally-

<subsystem xmlns="urn:jboss:domain:web:1.1" default-virtual-server="default-host" instance-id="${jboss.node.name}" native="false">
            <connector name="http" protocol="HTTP/1.1" scheme="http" socket-binding="http"/>
            <connector name="ajp" protocol="AJP/1.3" scheme="http" socket-binding="ajp"/>
            <virtual-server name="default-host" enable-welcome-root="true">
                <alias name="localhost"/>
                <alias name="example.com"/>
            </virtual-server>
        </subsystem>

Add any missing entry from the two configuration if its not already there in your file. The proxy-list attribute should be the host:port where the apache is running. By default sticky-session is true. I am setting it to false sot that the request from one user does not go to the same server everytime.

Thats all we are done with all the configuration changes for both the ends. Remember to add user to both jobss standalone instances as done in the previous blogs. Just run the adduser.bat file and follow the steps.

Start both the jboss nodes and the apache web server after the configuration changes have been made.
If all the configuration is fine you can see something like below screen if you hit the url:- as you can see both the nodes are visible to apache mod_cluster.

















The next step would be to login to the admin console of both the nodes one by one and deploy the same default application as in the previous blogs. Once the deployment is done we can access the applicatoin by hitting the url:-http://localhost/JbossClusterWebApp/













Try refreshing the page multiple times. You can see that the node id changes on each refresh,i.e the request is load balanced on round robin basis to both the servers. Then try to bring the server down on which the request is currently running and refresh the page again. You will see that the node id has changed again. This is the example of failover. All this is being done without the user manually hopping from one server to another.

That is all for load balancing and failover with apache and jboss.

Thanks for reading.




No comments:

Post a Comment