OpenShift is RedHats PaaSÂ solution and is perfectly suited for running Java EE applications. In this article we will be deploying an existing web application to OpenShift Online. OpenShift Online automates the provisioning, management and scaling of applications.
Lets assume we have a web application we normally deploy to Tomcat and makes use of a PostgreSQL database. A pretty common setup. So, what do we need to do to get this web application deployed to OpenShift?
If you have taken a look at OpenShift, you might have seen OpenShift talks about applications as a configuration of gears, cartridges and application code. It seems obvious we need to create an application and specify the configuration we want to use. We will be making use of the client tools for OpenShift as provided by RedHat. Information on how to install and configure them can be found here.
Application Creation
Our application will get the name demo. Since we are keeping things simple we will stick to using one small gear with Tomcat and PostgreSQL installed. Default when creating an application, OpenShift links it to a git repository and links deployment to git pushes. As we will be deploying an existing application we don’t need it. The command below will create our application and provision our gear.
$ rhc app-create demo jbossews-2.0 postgresql-9.2 --gear-size small --no-git
If all went ok, you should get roughly the following output.
Application Options ------------------- Domain: somedomain Cartridges: jbossews-2.0, postgresql-9.2 Gear Size: small Scaling: no Creating application 'demo' ... done PostgreSQL 9.2 database added. Please make note of these credentials: Root User: ********* Root Password: ******** Database Name: demo Connection URL: postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT Waiting for your DNS name to be available ... done Your application 'demo' is now available. URL: http://demo-somedomain.rhcloud.com/ SSH to: *********************@demo-somedomain.rhcloud.com Git remote: ssh://*********************@demo-somedomain.rhcloud.com/~/git/demo.git/ Run 'rhc show-app demo' for more details about your app.
Next we need to change some settings to have more control over our deployments. We will disable automatic deployments, set the deployment type to binary and make sure we keep upto five deployments so we can restore a previous version of our application.
$ rhc app configure demo --no-auto-deploy --deployment-type binary --keep-deployments 5
If all went ok, you should see roughly the following output.
Configuring application 'demo' ... done demo @ http://demo-somedomain.rhcloud.com/ (uuid: *********************) ------------------------------------------------------------------------- Deployment: manual (use 'rhc deploy') Keep Deployments: 5 Deployment Type: binary Deployment Branch: master Your application 'demo' is now configured as listed above. Use 'rhc show-app demo --configuration' to check your configuration values any time.
Deployment Structure
Deployments to OpenShift can be performed using the client tools using the following command:
$ rhc deploy --app demo
This will kick off an script on the gear that deploys the specified deployment archive. It would have been nice if you could just specify your WAR archive here, but that would be too easy. OpenShift expects a very specific deployment archive structure. The structure is as follows.
deployment/ +-buid_dependencies/ +-dependencies/ +-jbossews/ +-webapps +-repo/ +-.openshift/ +-action_hooks/ +-config/ +-cron/ +-markers/ +-webapps/
To deploy our application we need to add some files to this structure.
deployment/ +-buid_dependencies/ +-dependencies/ +-jbossews/ +-webapps +-repo/ +-.openshift/ +-action_hooks/ +-config/ +-cron/ +-markers/ +-java7 -- Tell OpenShift to use Java7 +-webapps/ +-ROOT.war -- Our application WAR
To package deployment as an archive, you can use the following command:
$ tar cvfz deployment.tar.gz -C deployment .
This will create the archive including everything starting inside the deployment directory.
Deployment
Now we have created the binary deployment package, we are ready to deploy it to our provisioned gear. This is achieved using the following command:
$ rhc deploy path/to/deployment.tar.gz --app demo
If all goes well, you should see something like:
Deployment of file 'path/to/deployment.tar.gz' in progress for application demo ... Starting deploy for binary artifact Stopping gear Stopping jbossews cartridge Stopping Postgres cartridge Creating new deployment directory Preparing deployment Preparing build for deployment Deployment id is 2cfd13ca Distributing deployment Distribution status: success Activating deployment Starting Postgres cartridge Postgres started Starting jbossews cartridge Found 127.7.139.129:8080 listening port Activation status: success Deployment status: success Success
Your application should now be up and running at the following location:
http://demo-somedomain.rhcloud.com.
Bibliography
This article is stitched together using information form the following publicly available sites, which you should explore further if you want to know more.