četrtek, 5. november 2015

How to: set Wildfly 9 in standalone cluster.

In last month i wanted to set my Java EE web application  on cluster of Wildflys. It took me a lot of time since many components changes over time, description found on internet were outdated so i decided to write my own how to. Since I didn't have physical machines i decided to do it on my laptop with help of  virtual machines. I tried also with mod cluster 1.3.1 and httpd 2.4. but since it didn't work after trying to repair things for some time i will not discuss it here. (mod cluster 1.2.6. with httpd 2.2 took me much less time since it worked almost out of the box). I developed my application on one server and when i was sure it works well I started with this experiment. For people who are setting cluster for first time i recommend reading Wildfly official post on clustering.


IDEA: Idea was setting up an experimental cluster with 2 wildfly nodes and setting Apache httpd as load balancer for cluster with use of mod cluster, scaling application horizontally.

We will simulate such arhitecture.


Advantages of such deployment are load balancing, reliability and scalability. Since we will deploy server in standalone mode we will also gain reliability between software upgrades (It can be done also in domain mode, but i found it easier in standalone mode).

REQUIREMENTS: 
-Virtual box
-Centos 6.7
-Apache httpd 2.2. and mod cluster 1.2.6.
-Wildfly 9.0.1. Final

PREPARATIONS:
First we need to install Virtual box to host computer. After we set two virtual machines with centos 6.7. we should take for internet connection to them. In network settings i set "Briged Adapter" and in advance subsection i set different MACs. After installation of centos, download Wildfly 9.0.1. Extract Wildfly to same place, I will refer it as WILDFLY_HOME. We should  also disable selinux and firewall.

To disable selinux edit /etc/sysconfig/selinux change value SELINUX=disabled  and restart.

To disable firewall:
service ip6tables stop
service iptables stop 
We should ping VM between eachother to make sure communication will work.

Now we need to set up mod cluster and httpd. We will set it up only on one virtual machine. Just for easier communicating we will say it is node1. Now extract jboss folder in downloaded archive to your /opt/ directory.

Hint 1: Don't try to make your life miserable by installing httpd and mod_cluster via yum or any similar program. It may work but in my case it didn't and i lost a lot of time trying to set it right.

Hint 2: To make virtual machines and all download faster, set up only one virtual machine and then clone it in Virtual box. After cloning, set second MAC address different and it should work.

HTTPD  & MOD CLUSTER SETTINGS:
In mod clud cluster we can stick with basic settings that are already set. All settings in setup i proposed are made in configuration file httpd.conf. Only thing that i was changing were IPs inside that file. We will change them with IP of virtual box that has installed http. We will also add command AllowDisplay On that will allow us see details in mod cluster manager page. We will see that page in browser on http://IP_of_VM_with_httpd:port_specified/location_specified. In my example it is http:192.168.2.249:6666/mod_cluster_manager/

<IfModule manager_module>
  Listen 192.168.2.249:6666
  ManagerBalancerName mycluster 
<VirtualHost 192.168.2.249:6666>
    <Location />
     Order deny,allow
     Deny from all
     Allow from 192.168.2
    </Location>

    ServerName myapp.localhost

    KeepAliveTimeout 300
    MaxKeepAliveRequests 0
    ServerAdvertise on http://192.168.2.249:6666
    AdvertiseFrequency 5
    EnableMCPMReceive
    AllowDisplay On  
    <Location /mod_cluster_manager>
       SetHandler mod_cluster-manager
       Order deny,allow
       Deny from all
       Allow from 192.168.2
    </Location>
  </VirtualHost>
</IfModule>

We run httpd service with ./sbin/apacheclt.sh start and we can make sure everything is ok with watching log located in httpd/logs/error_log.

WILDFLY SETTINGS:
We will need to edit file standalone-ha.xml located in WILDFLY_HOME/standalone/configuration/. we will add name to our server by adding attribute name with value node1  or node2 to server tag(second line in XML file). It will look like:
<server name="node1" xmlns="urn:jboss:domain:3.0">
We can copy out database settings in <datasources>  tag and add drivers if needed.

We should also change our virtual host settings so we could have our application on URL specified in httpd. We set this with following:
            <server name="default-server">
                <ajp-listener name="ajp" socket-binding="ajp"/>
                <http-listener name="default" socket-binding="http" redirect-socket="https"/>
                <host name="myapp" alias="myapp.localhost">
                    <location name="/" handler="welcome-content"/>
                    <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                </host>
            </server>
The last thing we need to change are public and management interface IP. We set them to VM IP address.
    <interfaces>
        <interface name="management">
            <inet-address value="${jboss.bind.address.management:192.168.2.249}"/>
        </interface>
        <interface name="public">
            <inet-address value="${jboss.bind.address:192.168.2.249}"/>
        </interface>
        <interface name="unsecure">
            <inet-address value="${jboss.bind.address.unsecure:127.0.0.1}"/>
        </interface>
    </interfaces>
We set this on both VM everything else is  already configured, even most of this configurations are not needed for simple example. After saving those settings we can run server with ./WILDFLY_HOME/bin/standalone.sh 

We will see now in wildfly console that nodes found each other and started infinispan and jgroup which will transfer our session between them.

APPLICATION SETTINGS:
Only setting we need in our application is tag <distributable/>  in web.xml.

For deploying on root context we also need to put following in jboss-web.xml
<jboss-web> 
    <context-root>/</context-root>
    <virtual-host>myapp</virtual-host>
</jboss-web>

 TESTING:
We should test httpd first and see if it is set correctly. For this we should take a look on management page. We will see in detail print if everything is OK.
Mod cluster manager detail view

At this panel we should also see nodes online.

Simple app for testing can be found on  Wildfly cluster how to page. We deploy that application on both Wildflys and now we are ready to test it. On the host machine we add following line in hosts file so we will be redirected to right IP when we type URL http://my_url/ in browser.
192.168.2.249        myapp.localhost
We can see it in browser now and we should get connection with one of nodes. When we see with which we can shut down that node and refresh browser and see that session still lives on second node.

Hint 3: When i was testing my app I noticed that session wont be shared until i am using omnifaces 2.0, this is repaired in 2.1. version.
Hint 4: If we want to upgrade version of our application running on servers we can gracefuly shut down server(Server waits for all requests to be served before shuting down, meanwhile this node will not accept new requests), upgrade version on this server, put it back online, shuting down other node and upgrade. 

LITERATURE AND SOURCES: 

  • Ritchie, Christopher. Wildfly Configuration, Deployment, And Administration - Second Edition. Birmingham: Packt Publishing, Limited, 2014. Print.
  • Fugaro, Luigi. Wildfly Cookbook. Birmingham, England: Packt Publishing, 2015. Print.
  • Wildfly cluster how to page
  • Mod cluster documentation


Ni komentarjev:

Objavite komentar