This artical marked the way we scale up Fabflix to handle massive amounts of traffic by using a cluster of MySQL and Tomcat. Since the user would have only one entry point to Fabflix, then we need to use a load balancer to balance the traffic from one entry point to multiple Fabflix instances.
Step 1 (master/slave):
Create a dummy user for the two example Tomcat applications below:
1 | shell> mysql -u root -p |
Step 2 (master/slave):
Setup Tomcat on each master/slave instance.
Step 3 (master/slave):
On each master/slave instance, deploy TomcatTest.war. Make the URL http://PUBLIC_IP:8080/TomcatTest/servlet/TomcatTest work. You may need to modify the username/password and IP address (use the internal AWS instance address). Also make sure to modify the AWS security group setting for these two instances to allow remote access to their 8080 port.
Step 4 (master/slave):
On each master/slave instance, deploy Session.war. Make the URL http://PUBLIC_IP:8080/Session/servlet/ShowSession?myname=Mighael work.
Step 5 (instance 1):
On the instance that runs the original Fablix instance (called “instance 1”), set up Apache and its proxy by doing the following:
- Install Apache2 and related modules:
1
2
3instance1-shell> sudo apt-get install apache2
instance1-shell> sudo a2enmod proxy proxy_balancer proxy_http rewrite headers lbmethod_byrequests
instance1-shell> sudo service apache2 restart
Modify the security group to open port 80 of instance 1 to your IP address. Use your browser to open a URL using the public IP address of instance 1, and you should be able to see an “Apache2 Ubuntu Default Page”. It means you have successfully set up the Apache2 on this machine.
Configure the Apache2 webserver to use its proxy_balancer module for sharing (i.e., redirecting) requests to the backend instances. To do it, edit the following configuration file:
1
instance1-shell> sudo vim /etc/apache2/sites-enabled/000-default.conf
Create a load balancer proxy, whose members are the backend instances. In particular, define a proxy on top of the file, before the <VirtualHost *:80> tag.
1
2
3
4<Proxy "balancer://TomcatTest_balancer">
BalancerMember "http://172.2.2.2:8080/TomcatTest/"
BalancerMember "http://172.3.3.3:8080/TomcatTest/"
</Proxy>
Here we assume ‘172.2.2.2’ and ‘172.3.3.3’ are the private IP address of the master and slave instances, respectively.
Add two new rules in the body of the VirtualHost tag.
1
2ProxyPass /TomcatTest balancer://TomcatTest_balancer
ProxyPassReverse /TomcatTest balancer://TomcatTest_balancerRestart Apache:
1
instance1-shell> sudo service apache2 restart
Modify the security group of the two backend instances to allow instance 1 to access their 8080 port.
These settings will redirect HTTP requests to “instance1_IP/TomcatTest” to one of the two backend instances. To test it, use a browser to point to http://instance1_IP/TomcatTest/servlet/TomcatTest. Be sure to open port 80 of instance 1 to your IP address. Check the Tomcat access log of the two backend instances.
1 | instance2-shell> tail -f /home/ubuntu/tomcat/logs/* |
One of them should receive that request. Keep refreshing the page to send multiple requests, and check if the two backends are receiving the requests evenly.
Step 6 (instance 1):
Configure the proxy on instance 1 to handle sessions properly. Since the current setting will send requests randomly to the backend, it will not pass cookies properly, causing sessions to fail. We want to make the session persist over several requests of the same client, i.e., to have a sticky session. To do it, read the instructions, especially those under “Examples of a balancer configuration”.
Here’s a sample setting for the /etc/apache2/sites-enabled/000-default.conf file for the “Session.war” application:
1 | Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED |
Also, do the following:
Add two new rules in the body of the VirtualHost tag.
1
2ProxyPass /Session balancer://Session_balancer
ProxyPassReverse /Session balancer://Session_balancerRestart Apache:
1
instance1-shell> sudo service apache2 restart
Test if it works by pointing to the URL http://instance1_IP/Session/servlet/ShowSession?myname=Michael of instance 1. It should access one of the backend instances only.