After we finished writing the post on VMware Cloud Foundry platform, it seemed natural to write a follow-up on Red Hat OpenShift. OpenShift is a Java-based Platform-as-a-Service (PaaS) offering from Red Hat, the ‘giant’ of Open Source Software with a well-deserved reputation that comes from a wide range of products including operating systems (Fedora, Red Hat Enterprise Linux), application servers / middleware (JBoss AS, JBoss ESB), frameworks (Hibernate, Seam) and tools (JBoss Tools, Arquillian).
As a PaaS offering, the ultimate goal of OpenShift is to reduce the effort needed to write and deploy highly scalable and highly available Java applications. Under your dedicated “application space” the platform components run to ensure your application is able to respond to user’s requests, but isolating your application code from the infrastructure and all the complexity usually associated with complex, distributed deployments.
Let’s jump into OpenShift!
2 Red Hat OpenShift
OpenShift was born after Red Hat’s acquisition of Makara in 2010. Makara is a state-of-the-art technology for building highly scalable solutions that was integrated with Red Hat flagships RHEL and JBoss to become the core of the OpenShift platform.
The first fact that really got our attention and positively increased our interest in the platform – above the usual PaaS stuff – is the statement used as the title for the post: Freedom of choice. No matter if you are a Java developer and your favorite framework is Spring, Seam or just the Java EE specification; no matter if you prefer Ruby, PHP, Perl or Python. OpenShift supports all these technologies.
OpenShift as an offering comes in three flavors: Express, Flex and Power. Express is a free to use platform for Java, Ruby, PHP, Perl or Python technologies, easy to setup and easy to use. Flex is a full cloud management solution that operates on top of Amazon EC2, dynamically managing clusters and applications, scaling up and down as needed. Power is the most flexible solution in which you can go down to the OS level and build up cloud services on any Linux-based solution.
For the rest of this post we will show how to start working with OpenShift Express, prepare an existing Java with Spring application and deploy it to the cloud. We will be using Eclipse IDE Java EE bundle on a Windows workstation to work on the application and test it locally, plus OpenShift command-line tools to configure and manage the cloud deployment.
3 Preparing the Tool Set
OpenShift client tools are a collection of Ruby-based scripts. With them you can interact with OpenShift and with your application.
According to Express setup instructions page here, you need to install Cygwin (http://www.cygwin.com/), some Cygwin packages (including Ruby and Git) plus RubyGems and then, finally, install Red Hat OpenShift gems.
But we are Windows geeks, so there is no point in going through Cygwin to interact with OpenShift. Actually, this is one of the many advantages of Ruby: The same Ruby script can be executed from a Linux shell, Windows shell or inside the Java Virtual Machine.
So, first step, if you do not have a Ruby runtime already installed, is to go to http://www.rubyinstaller.org/ and download the Ruby Zipped distribution. Then, decompress it to a folder that I will refer as %RUBY_HOME%. From a command prompt, move to %RUBY_HOME%/bin directory and issue the following commands:
Needed if you are behind a proxy, use the right one for your network or skip this command
>gem install rhc
Install the Red Hat OpenShift gem
You will also need a Git client to upload changes to your OpenShift application space. Git is a popular software configuration management tool and it is used by OpenShift to track changes on artifacts uploaded to the application space. Go to http://code.google.com/p/msysgit/ and download the Git Zipped distribution. Then, decompress it to a folder that I will refer as %GIT_HOME%. I recommend reading a bit about Git and basic commands so you are in good shape to interact with your remote repository at OpenShift.
I also recommend adding both %RUBY_HOME%/bin and %GIT_HOME%/bin to the path.
4 Creating a Domain and the Application Space
Once we have installed the needed tools, it’s time to create a domain and an application space. A domain is a unique identifier for all your applications. Inside the domain we will have one or many application spaces, and each one will have an associated Git repository to where we will push our updates to the deployed application. Each push will trigger the necessary actions for doing so automatically.
Both the domain and application identifiers are part of the application URL, so choose wisely. The format is:
To create the domain issue the following command:
>rhc-create-domain -n <domain-name> -l <login-email-address>
The login email address identifies you and you will be prompted to enter your password. Once introduced, the console will show your SSH key identification. It will be saved in your %HOME%/.ssh folder and used from now on to secure communications to your domain. The default login information is saved in %HOME%/.openshift/ express.conf so you do not need to provide your login email address each time you issue a command to control your domain.
Next step is to create the application space. For that, issue the following commands:
You will need to create a directory where the application will be locally stored
>rhc-create-app -a <app-name> -t jbossas-7.0
Provide the application name and the type of the application
This command will create the remote Git repository and add the application URL to the DNS so users may access it from the Internet. The remote Git repository URL is showed in the command output, in the case you need to supply it to another tool.
With this we have created a JBoss AS 7.0 Java application. OpenShift is taking care of provisioning the server runtime and needed resources. Other types include Ruby (rack-1.1) or PHP (php-5.3) applications. In all cases the platform provides everything needed to start running the application. You can check the list of available types in OpenShift documentation here.
5 Deployment to the Cloud
Now that our environment is ready, let’s discuss about what changes need to be done on an existing Java with Spring application to be deployed there.
The reference application is a very simple one: web interface using JSP and JSTL, plus Spring 3 and Hibernate. It is currently configured to use an embedded HSQLDB database for data persistent storage. The application is working locally deployed in a JBoss AS 7.0.2 runtime.
5.1 Adding Support for MySQL
The first adaptation that we are going to do is to add support for MySQL to our application.
Each application in OpenShift may have services associated to it. Each service is named a Cartridge. For our needs we are going to add a MySQL database cartridge to the application. To do that, remember the given application name and issue the following command:
>rhc-ctl-app -a <app-name> -e add-mysql-5.1
The console will show a message confirming the creation of the MySQL database, the root user credentials, the database name (should be the same as the application name) and the connection URL.
5.2 Updating Spring configuration
Our next step is to update the Spring configuration thus the application can make use of the MySQL database. To do that, you may replace the existing data source configuration with the URL and credentials received when the MySQL cartridge was created or rely on some environment variables that are created by OpenShift platform. Using environment variables is the recommended way to configure database access. This way it is easy to test the application locally just by providing values to a locally available MySQL database:
The environment variables used are self-explanatory:
OPENSHIFT_DB_HOST OPENSHIFT_DB_PORT OPENSHIFT_APP_NAME OPENSHIFT_DB_USERNAME OPENSHIFT_DB_PASSWORD
5.3 Running the Application
Once the changes have been made it is time to commit all the files and make our first push to OpenShift.
Assuming that you’ve placed all application files in the project home directory used when the application was created, just issue the following commands to commit all files and push them to the remote Git repository:
>git commit -a -m “initial version”
The contents will be uploaded and then a Maven build process will trigger. Sources will be compiled, packaged and deployed in a few seconds and after that, the application is ready to be used!
5.4 Useful Commands to Control your Applications
These are some useful command that will allow you to control your applications:
Show basic information about applications available in your domain:
names, types, Git URL, application URL and cartridges
>rhc-ctl-app -a <app-name> -c status
Show the application status, including a tail from its log
>rhc-ctl-app -a <app-name> -c start|stop|reload|restart
Starts, stops, reloads and restarts the application
>rhc-snapshot -a <app-name> -s <file-name>.tgz
Downloads the application files, including full logs, for troubleshooting
6 JBoss Tools Support for OpenShift
Recently Red Hat announced a new release of their popular JBoss Tools suite for Eclipse that included support for OpenShift. If you do not feel comfortable while working with command line tools or just prefer the improved experience of having everything at one click distance in your favorite IDE, then this section is for you.
Let’s assume that we already have an application deployed to OpenShift and a blank Eclipse workspace. First, let’s install the OpenShift support from JBoss Tools. For example, for Indigo, add the following Eclipse update site and add JBoss OpenShift Express Tools feature:
Once installed, we will have available a new wizard named OpenShift Express Application. Let’s create a new project of this type:
Next, enter your OpenShift credentials and select the domain and application to import it into Eclipse.
Then, review the application details and specify, if desired, the target folder for the project. You may as well create a new JBoss server runtime if you do not have already one in your workspace. Once done, the Git remote repository for the application will be cloned into your workspace:
Now, to verify that everything is working fine, let’s make a trivial change, commit it and push it to OpenShift for deployment:
Once the push is finished the updated application will be redeployed:
Red Hat OpenShift is a Cloud platform based on Red Hat Enterprise Linux and JBoss Application Server that allows Java, Ruby, PHP or Python web applications run on Cloud premises with high flexibility and scalability. Existing applications will need only minor configuration changes to be ready for OpenShift. Moreover, OpenShift can be easily integrated with the popular Eclipse IDE for developers productivity increase.
In the forthcoming months we will surely have confirmation on enterprise-ready commercial offerings sitting on this great platform, and hopefully it will make the jump to private infrastructures as well.