Software load balancer configuration using the Apache Web Server

Now, both the Liferay Portal server nodes are ready. As a next step, we need to configure the Apache Web Server to connect with both the Liferay Portal nodes and also distribute the load on both the Liferay Portal nodes. The Apache Web Server provides many options to connect with the Liferay Portal Tomcat server. But there are three options which are more popular. Let's understand these options and the scenarios in which they are best suitable.

Load balancer configuration using mod_jk

This option allows us to configure the load balancer using the mod_jk module of the Apache Web server. Internally, the mod_jk module connects with the Liferay Portal Tomcat server using the AJP protocol. Using this option, the Apache Web Server distributes all requests on the AJP port of Liferay Portal Tomcat servers. Let's learn how to configure the software load balancer using this option.

  1. Download and copy the mod_jk module in the <APACHE_HOME>/modules directory.

    Note

    The mod_jk connector is available for download from the following URL:

    http://tomcat.apache.org/download-connectors.cgi

  2. Create a new file called mod_jk.conf in the <APACHE_HOME>/conf directory, and add the following configuration into it:
    LoadModule    jk_module  modules/mod_jk.so
    JkWorkersFile /etc/httpd/conf/workers.properties
    JkShmFile     /var/log/httpd/mod_jk.shm
    JkLogFile     /var/log/httpd/mod_jk.log
    JkLogLevel    info
    JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
    JkMount /* loadbalancer

    Note

    In the preceding configuration, please make the changes to the file paths according to your installation folder structure.

  3. Now edit the httpd.conf file located in the <APACHE_HOME>/conf directory, and add the following line at the bottom:
    Includemod_jk.conf
  4. Add a new file called worker.properties in the <APACHE_HOME>/conf directory, and add the following lines into it:
    #Name of the load balancer workers
    worker.list=loadbalancer
    #Worker configuration for liferay-node-01
    #AJP Connector port of node-01 on liferay-node-01 server 
    worker.node-01.port=8009
    worker.node-01.host=<IP of liferay-node-01 server>
    worker.node-01.type=ajp13
    #Factor which decides the load sharing by this worker in the cluster
    worker.node-01.lbfactor=1
    #Worker configuration for liferay-node-02
    worker.node-02.port=8009
    worker.node-02.host=<IP of liferay-node-02 server>
    worker.node-02.type=ajp13
    worker.node-02.lbfactor=1
    #load balancer configuration properties
    worker.loadbalancer.type=lb
    #list of worker nodes that are part of the cluster
    worker.loadbalancer.balance_workers=node-01,node-02
    worker.loadbalancer.sticky_session=1
    worker.loadbalancer.method=B
  5. Now edit the server.xml file of liferay-node-01 located in node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27conf, and add the jvmRoute attribute to the <Engine> tag as given here:
    <Engine defaultHost="localhost" name="Catalina" jvmRoute="node-01">
  6. Similarly, add the jvmRoute parameter in server.xml of liferay-node-02. Here the value of jvmRoute will be node-02.
  7. We are ready to test our configuration. Restart both the Liferay Portal nodes and the Apache Web Server to test the configuration. We can access Liferay Portal directly by using the http://<Apache Web Server IP> URL.

To connect the Apache Web Server with the Liferay Portal Tomcat server, we installed the mod_jk module in the Apache Web Server. We then configured the Apache Web Server to load the mod_jk module and provided the mod_jk configuration parameters. We defined the logfile and shared memory file locations and logfile formats for the mod_jk module. The most important configuration is to add worker nodes on which we want to distribute the load. We defined this by providing the worker configuration file. In the worker configuration file, we defined two Liferay Portal nodes. We then configured the load balancer and added Liferay Portal nodes to it. We configured the load balancer method to Busyness (B) which means, the load balancer will distribute the requests depending upon the load on Liferay Portal Tomcat servers. Other possible load balancer methods include By Requests (distributes the load based on the number of requests and load factor of the worker) and By Traffic (distributes load based on the traffic in bytes and load factor). Finally, we enabled session stickiness. Session stickiness is used to distribute all the requests for a specific session to a specific Liferay Portal Server node. Only in case of failure of the specific node, subsequent requests will be served by the other node. It is very important to use the sticky session feature to save resources. In order to make sure the sticky session functionality works fine, we configured jvmRoute in both the Tomcat nodes with unique values. The Apache Web Server appends jvmRoute in the session ID and based on the jvmRoute value, the Apache Web Server can ensure sending requests to the right Liferay Portal Tomcat node.

Load balancer configuration using mod_proxy_ajp

Another way to configure the software load balancer using Apache Web Server is through the mod_proxy_ajp and mod_proxy_balancer modules. This is a newer approach introduced in Apache Web Server 2.2. It uses the mod_proxy module and connects the Liferay Portal server using the AJP protocol. Let's understand how to configure the software load balancer using this option.

  1. Create a new file called mod_proxy_ajp.conf in the <APACHE_HOME>/conf directory, and add the following content:
    LoadModuleproxy_modulemodules/mod_proxy.so
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    LoadModule proxy_balancer_module /modules/mod_proxy_balancer.so

    Note

    If you are using an existing Apache Web Server, before adding these lines, make sure these modules are not enabled already in the Apache Web Server configuration (<APACHE_HOME>/conf/httpd.conf and all the included files).

  2. In the same file, add the following configuration settings:
    <VirtualHost *:80>
      ServerName localhost.localdomain
      ErrorLog /var/log/apache2/ajp.error.log
      CustomLog /var/log/apache2/ajp.log combined
      
      <Proxy *>
        AddDefaultCharSet Off
        Order deny,allow
        Allow from all
      </Proxy>
      ProxyPass / balancer://ajpCluster/ stickysession=JSESSIONID
      ProxyPassReverse / balancer://ajpCluster/ stickysession=JSESSIONID
      <Proxy balancer://ajpCluster>
        BalancerMember ajp://<IP of liferay-node-01>:8009 route=node-01
        BalancerMember ajp:// <IP of liferay-node-02>:8009 route=node-02
        ProxySet lbmethod=byrequests
      </Proxy>
    </VirtualHost>
  3. Now edit the httpd.conf file located in the <APACHE_HOME>/conf directory, and add the following lines at the bottom:
    Include mod_proxy_ajp.conf
  4. Now, edit the server.xml file of liferay-node-01 located in node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27conf, and add the jvmRoute attribute to the <Engine> tag as shown here:
    <Engine defaultHost="localhost" name="Catalina" jvmRoute="node-01">
  5. Similarly, add the jvmRoute parameter in server.xml of liferay-node-02. Here the value of jvmRoute will be node-02.
  6. Now restart both the Liferay Portal Servers and the Apache Web server, and test the configuration using the http://<Apache Web Server IP> URL.

This approach requires the mod_proxy, mod_proxy_ajp and mod_proxy_balancer modules. These modules, by default, ship with the Apache Web Server binary. We enabled them by using the LoadModule command. We then configured the virtual host for our local instance. In the virtual host configuration, we provided the locations of the logfiles. We added the load balancer using the <Proxy balancer> tag. Again in the load balancer configuration, we provided the hostname and port of both the Liferay Portal Servers. We also provided the jvmRoute value using the route parameter. This parameter must match with jvmRoute configured in steps 4 and 5. We also configured the load balancing method to By Requests (byrequests). This load balancing method distributes the load on both the Liferay Portal servers in a round robin manner. We also configured the virtual host to route all the requests to the load balancer using the mod_proxy configuration. We configured to use session stickiness through the mod_proxy configuration.

Load balancer configuration using mod_proxy_http

This method is very similar to mod_proxy_ajp. The only difference here is the load balancer configuration. Here the Apache Web Server and the Liferay Portal Tomcat server will connect using the HTTP or HTTPS protocol. So let's configure the load balancer using this option.

  1. Similar to previous options, we need to load the necessary modules. Create a new file called mod_proxy_http.conf in the <APACHE_HOME>/conf directory and add the following content:
    LoadModuleproxy_modulemodules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_balancer_module /modules/mod_proxy_balancer.so

    Note

    If you are using an existing Apache Web Server, before adding these lines, make sure these modules are not already enabled in the Apache Web Server configuration.

  2. In the same file, add the following configuration settings:
    <VirtualHost *:80>
      ServerName localhost.localdomain
      ErrorLog /var/log/apache2/http.error.log
      CustomLog /var/log/apache2/http.log combined
      
      <Proxy *>
        AddDefaultCharSet Off
        Order deny,allow
        Allow from all
      </Proxy>
      ProxyPass / balancer://httpCluster/ stickysession=JSESSIONID
      ProxyPassReverse / balancer://httpCluster/ stickysession=JSESSIONID
      <Proxy balancer://httpCluster>
        BalancerMember http:// <IP of liferay-node-01>:8080 route=node-01
        BalancerMember http:// <IP of liferay-node-02>:8080 route=node-02
        ProxySet lbmethod=byrequests
      </Proxy>
    </VirtualHost>
  3. Now edit the httpd.conf file located in the <APACHE_HOME>/conf directory and add the following line at the bottom:
    Include mod_proxy_http.conf
  4. Now edit the server.xml file of liferay-node-01 located in node-01liferay-portal-6.1.1-ce-ga2 tomcat-7.0.27conf, and add the jvmRoute attribute to the <Engine> tag as follows:
    <Engine defaultHost="localhost" name="Catalina" jvmRoute="node-01">
  5. Similarly, add the jvmRoute parameter in server.xml of liferay-node-02. Here the value of jvmRoute will be node-02.
  6. Now restart both the Liferay Portal Servers and the Apache Web server and test the configuration using the http://<IP of Apache Web Server> URL.

These steps are very similar to the previous option. The only difference is that we are using the HTTP protocol. This connector allows us to establish an encrypted connection between the web server and the Liferay Portal application server using the HTTPS protocol.

Load balancing best practices

We have learned three different methods of configuring the software load balancer using the Apache Web Server. Now let's learn some of the best practices associated with these options:

  • The software load balancer configuration using mod_jk is most recommended because mod_jk is a reliable and error free module compared to other options. From the performance point of view it gives the best performance. The mod_proxy_ajp module is similar to mod_jk but it is relatively new. If there is a need to use a secured connection between the Apache Web Server and Liferay Portal Tomcat server, we can consider using the mod_proxy_http module. It provides easy configuration to implement this scenario.
  • We learned that the Liferay Portal Tomcat server and the Apache Web Server either connect using the AJP connector or the HTTP connector. None of the connectors use both the connectors at the same time. The Liferay Portal Tomcat server, by default, enables both the connectors. It is a best practice to disable the connector which we are not using. This can save resources on the Liferay Portal application server. We can disable any of the connectors by commenting the respective <Connector> tag from the server.xml file of the Liferay Portal Tomcat server.
  • It is advisable to select the load balancer method carefully. Depending upon the nature of the application, the right load balancer method should be chosen. If we are using the mod_jk connector, it is recommended to use the Busyness load balancer method. This will help in distributing requests on Liferay Portal servers with respect to their current load.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.172.210