Bookstore PaaS Application

About BookStore application

The sample Bookstore application is a simple web application that provides the ability to view the books in the bookstore and register information about new books. This application requires a database for storing the book details. The database has 2 tables DEMO_TABLE and BOOKS_TABLE. The DEMO_TABLE is used to update and get the last accessed time of the database, and the BOOKS_TABLE is used to add more books into the database table. A Servlet is used to display the list of books along with the version of the database being used and when the database was last accessed.

If we need to make this application available to end-users in a traditional non-PaaS environment, the administrator would manually create the environment required for running this application. Creation of the environment would include setting up a database server, setting up a GlassFish cluster, and possibly a load balancer that front-ends the GlassFish cluster. Once the environment is manually setup, the deployer would then deploy the application using 'asadmin deploy'.

This sample demonstrates one of the use-cases of making a Java EE application available in a PaaS environment, wherein the Application Server , the database and the optionally the loadbalancer are provisioned and made available as a service , and associated with this application, when it is deployed.

In a PaaS environment, deployment of applications is done without the cost and complexity of managing the underlying hardware and software. The environment should be more elastic and should automatically scale up/down based on the application's usage. In this sample, the application requests for two Java EE Application Server instances via meta-data specified in the glassfish-services.xml, and the Database service dependency is discovered during deployment.

As part of the Single Click Application Deployment on PaaS ( a.k.a. Single Click Service Orchestration) here, we are demonstrating how the required services (namely GF cluster, database server, load balancer) are automatically provisioned and associated behind the scenes, the application is deployed to the provisioned services and made available to the end user. For more details, refer to this page.

Native mode and Virtualization Mode

You can run this sample in one of two modes:  Native or Virtualized.

Native ( a.k.a Laptop mode)

This mode does not require any virtualization software and is targeted at developers who want to run the sample on their laptops or local machines. The services are installed in the local hard disk as local instances of a database or GlassFish cluster.

Virtualization Mode

This mode requires a virtualization solution and templates (VM disks pre-loaded with OS and the Service's software bits) for the Services that the application needs. For the purpose of this sample we will use KVM as the virtualization support. The user would need to create templates of  GlassFish, the database ( Derby) on their own. The details are mentioned below in the KVM section.

Instructions for setting up PaaS environment and running bookstore PaaS sample

The process was tested on Ubuntu 11.04. Please refer to https://wikis.oracle.com/display/GlassFish/BasicConfiguration to check if your setup can meet all the requirements.

In case you are short of time , please try out the sample in the Native mode.

Please note that the following steps are for running the bookstore application using the Derby database.

1. Download and unzip latest GlassFish binary specific to your OS to a location which has enough space. The GlassFish 4.0 latest build (on which this test was run) can be found here. In my case I unzip it under /space that has around 30 GB of free space. You could alternatively opt to build glassfish.zip from your own updated trunk workspace.

2. Set an environment variable named S1AS_HOME to point to GlassFish installation.

$export S1AS_HOME=/space/glassfish3/glassfish

3. Check if the /etc/hosts file is properly configured with your IP address corresponding to the "eth0" interface.

$ sudo cat /etc/hosts
127.0.0.1    localhost
xxx.xxx.xxx.xxx mymachine mymachine.domain mymachine.domain.com

4. Start the default GlassFish domain, this is your CPAS.

$S1AS_HOME/bin/asadmin start-domain

5. Choose if you want to execute the sample in the Native mode or in the Virtualized (KVM) mode. If you want to execute the sample in Virtualization(KVM) mode, go to Step 5.1, else go to Step 5.2.


5.1 KVM Setup

Setup virtualization environment for your GlassFish installation following instructions below :

a) Create the required templates for the bookstore application. More details can be found at the end of the page in section "KVM templating.html".

b) Setup KVM on your machine by referring to https://wikis.oracle.com/display/GlassFish/BasicConfiguration.

c) Ubuntu users can refer to the instructions given at https://help.ubuntu.com/community/KVM/Installation.

d) Get kvm_setup.sh provided at https://svn.java.net/svn/glassfish~svn/trunk/main/appserver/tests/paas/kvm_setup.sh. This script enables the KVM virtualization and prepares the environment for deploying the app.

e) Delete all folders from the "virt" directory in your home. Make sure before deploying the application that the "virt" folder is empty and that no virtual machines for the bookstore app are running in the virtual machine manager.

f) In the kvm_setup.sh, set the variable TEMPLATES_DIR to point to the location on the local machine where the templates are stored.

g) In the kvm_setup.sh, modify the "create-machine-user" command to pass the host user name as the new machine user.

h) Execute kvm_setup.sh.

The kvm_setup.sh script configures the CPAS to enable virtualization (using KVM in this case) and then creates a "ServerPool" out of which virtual machines are created. The "create-template" command copies the ".img" and the ".xml" files for the specified template from the $TEMPLATES_DIR to the template repository (~/virt/templates). The template information is also updated in the CPAS. It then creates users that the IMS would use to access the server pool while creating VMs.

i) After executing kvm_setup.sh, ensure that the template images have been completely copied to the $HOME/virt/templates location. Only after the copying is complete, proceed to the next step.


5.2 Native Setup

a) Execute native_setup.sh provided at https://svn.java.net/svn/glassfish~svn/trunk/main/appserver/tests/paas/native_setup.sh.

b) If you are using LoadBalancer execute native_lb_setup.sh at https://svn.java.net/svn/glassfish~svn/trunk/main/appserver/tests/paas/native_lb_setup.sh instead. In this case you would not need to.

c) Restart CPAS (the GlassFish instance you started in the earlier step).


6. Compile Bookstore PaaS sample. The application can be found here https://svn.java.net/svn/glassfish~svn/trunk/main/appserver/tests/paas/bookstore.

cd main/appserver/tests/paas/bookstore
mvn clean compile war:war
cp target/bookstore.war /tmp/

7. Deploy the Bookstore PaaS sample:

$S1AS_HOME/bin/asadmin deploy /tmp/bookstore.war

8. To see the services as they are provisioned, tail the server.log file ($S1AS_HOME/domains/domain1/logs/server.log).

9. In case you are using the Virtualization, using the KVM Virtual Machine Manager, you can log in to the virtual machines and check the progress of the deployment. You can log in to the machines using the user name which was specified in the kvm_setup.sh. The password is the same as the user name. Once this step completes all the provisioning would have been finished and the application is ready to access.

10. See the details of the services provisioned for your Bookstore PaaS sample:

$S1AS_HOME/bin/asadmin list-services

11. Access the deployed application, using the IP address of the GlassFish node obtained from output of the list-services command. For example, you can access the application like this:

http://<ip-address-of-vm-node-1:28080>/bookstore

12. Finally, undeploy the Bookstore PaaS application:

$S1AS_HOME/bin/asadmin undeploy bookstore

KVM Templating

While running the application in Native mode, no templates are required to be created.

While running in KVM mode, templates are required to create the virtual machines on which the application is deployed.

In the Bookstore application, two Java EE instances and one database instance are created in the virtualization environment. Hence a GlassFish template needs to be created. The GlassFish template created is utilized for both JavaEE as well as Database instance creation. The LB template is optional.

After creating the ".img" and ".xml" files for the templates, copy them to a location on the local machine and set the "TEMPLATES_DIR" variable in the kvm_setup.sh to point to that location.

GlassFish template

Template used by IMS to create JavaEE instances. To create a GlassFish template, refer https://wikis.oracle.com/display/GlassFish/templating.

JavaDB template

Template used by IMS to create JavaDB database instance. As the GlassFish template is bundled with a javadb database, the same template can be used for both GlassFish and Derby database instances.

LB template

Template to set up a Load Balancer instance for the application. This template is to be created only when one wants to have a load balancer for the application. To create an LB template, refer https://wikis.oracle.com/display/GlassFish/LBTemplating. For simplicity sake, we will not use a LoadBalancer in this application.

Troubleshooting

For troubleshooting setup refer to https://wikis.oracle.com/display/GlassFish/Troubleshooting.