Deploying a Spring Boot Java Application to Red Hat OpenShift

For my latest application I wanted to deploy a Maven Spring Boot application into Red Hats OpenShift cloud.  Unfortunately this is not an off-the-shelf configuration :(.  After some digging around I managed to discover the steps required,  so this post is as much for me to remember the gotchas as to help others.

 

Step 1 - Creating your OpenShift Application

Create your new Application from the OpenShift console.  You will need to create a 'diy' application.  Once this has completed clone the repository as described on the displayed 'Getting Started' page.

Note: I had problems with authentication failure using standard git operations.  I found using the rhc tool (downloadable from the openshift site), worked

rhc git-clone {application name}

 

Step 2 - Pushing your application

Once that is done you can push your Spring application to your new repository.  You will need to pull the repository first to ensure that you get the required structure and base scripts.

I had various issues with branch '/origin/HEAD' each time I did this operation, but deleting the branch fixed the problems.

 

Step 4 - Fixing it all for OpenShift

I have skipped over the first two steps as they are fairly standard stuff.  The next bit is getting your application working in OpenShift, which requires a bit of 'fiddling'.

The 'fiddling' fixes a number of issues:
  1. Scripts to build,stop and start our application
  2. Execution privilege on files
  3. Bind to address of the virtual machine
  4. Move Maven repository to a writable location

Step 4.1 - Required files

The Openshift environment requires a number of files/scripts to operate.  I have given examples of these below

The scripts in '.openshift/action_hooks' tell OpenShift how to build, start and stop your application.

build
cd $OPENSHIFT_REPO_DIR
mvn -s $OPENSHIFT_DATA_DIR/settings.xml package
./scripts/create_yaml.sh


Update:
I changed the above file to call a properties creation file after problems with the yaml:
./scripts/create_properties.sh

start
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_DIY_IP:8080
cd $OPENSHIFT_REPO_DIR/target
nohup java -jar {jar name} server {myapplication}.yml &


Update
Removed .yml property on latest version as switched to using properties files

stop
#!/bin/bash
source $OPENSHIFT_CARTRIDGE_SDK_BASH

# The logic to stop your application should be put in this script.
if [ -z "$(ps -ef | grep java | grep -v grep)" ]
then
    client_result "Application is already stopped"
else
    kill `ps -ef | grep java | grep -v grep | awk '{ print $2 }'` > /dev/null 2>&1
fi


In the 'scripts' directory you will

create_yaml.sh
echo " server:
  applicationConnectors:
    - type: http
      port: 8080
      bindHost: " $OPENSHIFT_DIY_IP  "
  adminConnectors:
    - type: http
      port: 8081
      bindHost: " $OPENSHIFT_DIY_IP  > $OPENSHIFT_REPO_DIR/target/{myapplication}.yml



Update:
I had lots of problems getting the yaml working on my lastest application to switched back to good old properties files.  Same principle but you create an application.properties file instread.

create_properties.sh
echo " server.port=8080
server.address="$OPENSHIFT_DIY_IP  > $OPENSHIFT_REPO_DIR/target/application.properties

Step 4.2 - Test build

Now that we have all of the necessary scripts we can try a build on the remote server.

First login to the OpenShift Application using the rhc tool

rhc ssh {application name}

Try to build your application

gear build

You will probably get a message telling you that the scripts are not executable, helpfully it also tells you the git commands to fix this.

 On Windows run:   git update-index --chmod=+x .openshift/action_hooks/build
 On Linux/OSX run: chmod +x .openshift/action_hooks/build

You will need to change the execution permissions of the build,start,stop and create_yaml.sh files.

Once you have pushed this to the remote repository you will see a string of more interesting log messages.  Basically Maven is broken :( so we need to fix this.

Step 4.3 - Fixing Maven

Login to the remote server

rhc {application name} 
 
Executing the following statements will move the mavan repository to a location that it can write to and create a settings file.

mkdir -p ${OPENSHIFT_DATA_DIR}/m2/repository
echo -e "<settings><localRepository>${OPENSHIFT_DATA_DIR}m2/repository</localRepository>\n</settings>\n" > ${OPENSHIFT_DATA_DIR}/settings.xml 

You should notice that as you push commits to the remote repository, once fixed, will start automatically building and deploying your new application.  Just to double check you can try a build yourself on the server.
 
gear build
 
WOW - see that maven build log fly through the console :)

Step 5 - All done

You should now have a built and running Maven Spring application running in Red Hat OpenShift.

I'll admit to skipping over some of the smaller issues,mainly git related, but hopefully this is enough to get you up and running.  Once you get to this point every push to the remote repository will rebuild/deploy your application.
 
 

Comments

Popular Posts