Tutorial: Getting to grips with OpenShift
Tired of requesting a new development machine for your application? Sick of setting up a new test environment? Have no fear OpenShift is here!
This article will walk you through the simple steps it takes to set up not one, not two, not three, but up to five new machines in the Cloud with OpenShift. You will have your applications deployed for development, testing or to present them to the world at large in minutes. No more messing around.
We start with an overview of what OpenShift is, where it comes from and how you can get the client tooling setup on your workstation. You will then be taken on a tour of the client tooling as it applies to OpenShift. In minutes you will be off and back to focusing on your application development, deploying to test it in OpenShift. When finished you will just discard your test machine and move on. After this you will be fully capable of ascending into the OpenShift Cloud when you chose, where you need it and at a moments notice. This is how development is supposed to be, development without stack distractions.
There is a great amount of hype in the IT world right now about Cloud. There is no shortage of acronyms for the various areas that have been carved out, like IaaS, PaaS and SaaS. OpenShift is a Platform as a Service (PaaS) from Red Hat which provides you with a platform to run your applications. For you as a developer, you want to look at the environment where you put your applications as just a service that is being provided. You don’t want to bother with how that service is constructed of a set of components, how they are configured or where they are running. You just want to make use of this service that they offer to deploy, develop, test and run your application. At this basic level, OpenShift provides a platform for your Java applications.
First let’s take a quick look at where OpenShift comes from. It started at a company called Makara that was based in Redwood City, California, providing solutions to enable organizations to deploy, manage, monitor and scale their applications on both private or public clouds. Red Hat acquired Makara in November of 2010, and in the following year they merged Red Hat technologies into a new project called OpenShift.
Just recently the OpenShift team has announced the launch of their Open Source project called OpenShift Origin, making the entire code base available online. You can even take it for a spin with the liveCD they offer, but this is outside the scope of this article. What makes this merging of technologies interesting for a Java developer is that Red Hat has included the next generation application platform based on JBoss AS 7 in OpenShift. This brings a lightning fast application platform for all your development needs.
The OpenShift website states, “OpenShift is a free, cloud-based application platform for Java, Perl, PHP, Python, and Ruby applications. It’s super-simple—your development environment is also your deployment environment: and you’re in the cloud.” This piques the interest so lets give it a try and see if we can raise our web application into the clouds. For this we have our jBPM Migration web application which we will use as a running example for the rest of this exercise.
Getting started in OpenShift is well documented on the website as a quick start, which you can get to once you have signed up for a Red Hat Cloud (rhcloud) account. This quick start provides us with the four steps you need to get our application online and starts with the installation of the necessary client tools. This is outlined for Red Hat Enterprise Linux (RHEL), Fedora Linux, generic Linux distributions, Mac OS X and Windows. For RHEL and Fedora it is a simple package installation, for the rest it is a Ruby based gem installation which we will leave for the reader to apply to their system.
Once the client tooling is installed, there are several commands based on the form rhc-<command>. There is an online interface available but most developers prefer the control offered by the command line client tools so we will be making use of these. Here is an overview of what is available with a brief description of each.
- rhc-create-domain – used to bind a registered rhcloud user to a domain in rhcloud. You can have maximum of one domain per registered rhcloud user.
- rhc-create-app – used to create an application for a given rhcloud user, a given development environment (Java, Ruby, Python, Perl, PHP) and for a given rhcloud domain. You can create up to five applications for a given domain. This will generate the full URI for your rhcloud instance, setup your rhcloud instance based on the environment you chose and by default will create a local git project for your chosen development environment.
- rhc-snapshot – used to create a local backup of a given rhcloud instance.
- rhc-ctl-app – used to control a given rhcloud application. Here you can add a database, check the status of the instance, start, stop, etc.
- rhc-tail-files – used to connect to a rhcloud applications log files and dump them into your command shell.
- rhc-user-info – used to look at a given rhcloud user, the defined domains and created applications.
- rhc-chk – used to run a simple configuration check on your setup.
Create your domain
To get started with our demo application we need to do a few simple things to get an OpenShift instance setup for hosting our Java application, beginning with a domain.
# We need to create the domain for OpenShift to start setting up # our URL with the client tooling using # rhc-create-domain -n domainname -l rhlogin # $ rhc-create-domain –help
Usage: /usr/bin/rhc-create-domain Bind a registered rhcloud user to a domain in rhcloud.
NOTE: to change ssh key, please alter your ~/.ssh/libra_id_rsa and
~/.ssh/libra_id_rsa.pub key, then re-run with –alter
-n|–namespace namespace Namespace for your application(s) (alphanumeric – max 16 chars) (required) -l|–rhlogin rhlogin Red Hat login (RHN or OpenShift login with OpenShift access) (required) -p|–password password RHLogin password (optional, will prompt) -a|–alter Alter namespace (will change urls) and/or ssh key -d|–debug Print Debug info -h|–help Show Usage info
# So we setup one for our Java application. Note that we already have # setup my ssh keys for OpenShift, if you have not yet done that, # then it will walk you through it. #
$ rhc-create-domain -n inthe -l [rhcloud-user] -p [mypassword]
OpenShift key found at /home/[homedir]/.ssh/libra_id_rsa. Reusing…
You may now create an application. Please make note of your local config file in /home/[homedir]/.openshift which has been created and populated for you.
Create your application
Next we want to create our application, which means we want to tell OpenShift which stack we need. This is done with the rhc-create-app client tool.
# Let’s take a look at the options available before we setup a Java # instance for our application. #
$ rhc-create-app –help
Contacting https://openshift.redhat.com to obtain list of cartridges… (please excuse the delay)
Usage: /usr/bin/rhc-create-app Create an OpenShift app.
-a|–app application Application name (alphanumeric – max 16 chars) (required) -t|–type type Type of app to create (perl-5.10, jbossas-7.0, wsgi-3.2, rack-1.1, php-5.3) (require) -l|–rhlogin rhlogin Red Hat login (RHN or OpenShift login) (Default: xxxxxxxxx) -p|–password password RHLogin password (optional, will prompt) -r|–repo path Git Repo path (defaults to ./$app_name) -n|–nogit Only create remote space, don’t pull it locally -d|–debug Print Debug info -h|–help Show Usage info
# It seems we can choose between several but we want the jboss-as7.0 # stack (called a cartridge). Provide a user, password and location # for the git repo to be created called ‘jbpmmigration’, see the # documentation for the defaults. Let’s watch the magic happen! #
$ rhc-create-app -a jbpmmigration -t jbossas-7.0 -l [rhcloud-user] -p [mypassword] -r /home/[homedir]/git-projects/jbpmmigration
Found a bug? Post to the forum and we’ll get right on it.
IRC: #openshift on freenode
Attempting to create remote application space: jbpmmigration Contacting https://openshift.redhat.com API version: 1.1.1 Broker version: 1.1.1
RESULT: Successfully created application: jbpmmigration
Checking ~/.ssh/config Contacting https://openshift.redhat.com Found rhcloud.com in ~/.ssh/config… No need to adjust Now your new domain name is being propagated worldwide (this might take a minute)… Pulling new repo down Warning: Permanently added ‘jbpmmigration-inthe.rhcloud.com,18.104.22.168′ (RSA) to the list of known hosts. Confirming application jbpmmigration is available
Attempt # 1 Success! Your application is now published here:
The remote repository is located here: ssh://firstname.lastname@example.org/~/git/jbpmmigration.git/
To make changes to your application, commit to jbpmmigration/. Then run ‘git push’ to update your OpenShift space.
If we take a look at my given path to the repo we find a git-projects/jbpmmigration git repository. Note that if you decide to alter your domain name you will have to adjust the git repository config file to reflect where the remote repository is, see above the line with ‘ssh:…..’. Also the page is already live http://jbpmmigration-ishereon.rhcloud.com/ . It is just a splash screen to get you started, so now we move on to deploying our existing jBPM Migration project.
First lets look at the provided README in our git project which gives some insight to the repository layout.
deployments/ – location for built wars (Details below) src/ – maven src structure pom.xml – maven build file .openshift/ – location for openshift specific files .openshift/config/ – location for configuration files such as standalone.xml (used to modify jboss config such as datasources) ../data – For persistent data (also in env var OPENSHIFT_DATA_DIR) .openshift/action_hooks/build – Script that gets run every push, just prior to starting your app
For this article we only will examine the deployments and src directories. You can just drop in your WAR files, remove the pom.xml file in the root of the project and they will be automatically deployed. If you want to deploy exploded WAR files then you just add a file called ‘.dodeploy’ as outlined in the README file. For real project development we want to push our code through the normal src directory structure and this is also possible by working with the provided pom.xml file. The README file provided gives all the details needed to get your started.
Our demo application, jbpmmigration also comes with a README file that provides the instructions to add the project contents to our new git repository, so we will run these commands to pull the files into our local project.
# placing our application into our Openshift git repo. # $ cd jbpmmigration $ git remote add upstream -m master git://github.com/eschabell/openshift-jbpmmigration.git $ git pull -s recursive -X theirs upstream master # now we need to push the content. # $ git push origin
[jbpmmigration maven build log output removed]
remote: [INFO] ———————————————————————— remote: [INFO] BUILD SUCCESS remote: [INFO] ———————————————————————— remote: [INFO] Total time: 3.114s remote: [INFO] Finished at: Mon Nov 14 10:26:57 EST 2011 remote: [INFO] Final Memory: 5M/141M remote: [INFO] ———————————————————————— remote: ~/git/jbpmmigration.git remote: Running .openshift/action_hooks/build remote: Running .openshift/action_hooks/deploy remote: Starting application… remote: Done remote: Running .openshift/action_hooks/post_deploy To ssh://email@example.com/~/git/jbpmmigration.git/410a1c9..7ea0003 master -> master
As you can see we have now pushed our content to the rhcloud instance we created, it deployed the content and started our instance. Now we should be able to find our application online at http://jbpmmigration-inthe.rhcloud.com/jbpmmigration_upload-0.2.
The final step would then be that you are finished working on this application and want to free it up for a new application. You can then make a backup with the rhc-snapshot> client tool and then remove your instance with rhc-ctl-app client tool.
# Ready to get rid of our application now. # $ rhc-ctl-app -a jbpmmigration -l eschabell -c destroy
Contacting https://openshift.redhat.com !!!! WARNING !!!! WARNING !!!! WARNING !!!! You are about to destroy the jbpmmigration application.
This is NOT reversible, all remote data for this application will be removed. Do you want to destroy this application (y/n): y
Contacting https://openshift.redhat.com API version: 1.1.1 Broker version: 1.1.1
RESULT: Successfully destroyed application: jbpmmigration
As you can see, it is really easy to get started with the five free instances you have to play with for your application development.
This completes our tour of the OpenShift project where we provided you with a glimpse of the possibilities that await you and your applications. It was a breeze to create your domain, define your applications needs and import your project into the provided git project. After pushing your changes to the new instance you are off and testing your application development in the cloud. This is real. This is easy. Now get out there and raise your code above the cloud hype.