A blog icon.

OpenShift Deployments

In this tutorial, I’ll deploy a containerized java application with its mysql database. In this setup, you’ll be able to build and quickly redeploy the java app.
The Docker, Kubernetes, and Openshift logos side-by-side.

In this tutorial, I’ll deploy a containerized java application with its mysql database. In this setup, you’ll be able to build and quickly redeploy the java app. This is what I use locally when working on applications that will run in OpenShift. This involves a complete setup and teardown of an application in OpenShift.

If an application is going to run in OpenShift, it’s important for developers to be able to run it locally with settings as similar to production as possible. The best way to accomplish OpenShift deployments is to run your app inside an actual OpenShift cluster.

Minishift

Minishift is a single node (vm) Kubernetes cluster with some wrapper functionality to make certain tasks easier. This provides all the functionality one would need to develop on the OpenShift platform. This excludes OpenShift deployments of clusters — I wouldn’t recommend installing Minishift anywhere except on your dev box.

The built-in features on top of the normal installation:

  • Default users and a projecteval $(minishift oc-env)
  • Running the above command will set up the oc-cli to point your OpenShift cluster. Since it is a real OpenShift cluster, you can use the same commands that you’d use in production.
eval $(minishift docker-env)
  • Running the above commands will set up your docker environment to use the docker client/socket on the Minishift virtual machine. Benefits include using the already allotted resources for Minishift and using the same docker version as the OpenShift version that you’re using (should match production).

Installing and Running Minishift

Install Minishift according to your OS requirements. I recommend using Homebrew for Mac and probably downloading the tar for Windows and Linux. Run this command to start it up:

minishift start --disk-size 30GB --memory 3GB

If running Jenkins and Nexus as well as your app, feel free to bump up the memory. I usually run it with 6GB, and depending on my image sizes, 30GB or 50GB. With `minishift delete` everything is cleared out including the specified disk space, so feel free to go nuts.

OpenShift Builds and OpenShift Deployments

Setting up local build and deployments for spring-petclinic

This application is just a spring application that compiles into a war and requires a connection to a mysql database to work. I made some alterations to the application to work this way. That way, if the site comes up it means it successfully connected the mysql db.

  1. Fork spring-petclinic-framework so that you can make changes and check in your changes
  2. Alter the Dockerfile
FROM jboss/wildfly:11.0.0.Final
COPY target/petclinic.war /opt/jboss/wildfly/standalone/deployments/petclinic.war
ENV JDBC_PASSWORD=petclinic \
	DB_SCRIPT=mysql \
  JDBC_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver \
  JPA_DATABASE=MYSQL \
  JDBC_USERNAME=root \
  JDBC_URL=jdbc:mysql://fefewfewfewffewfew:3306/petclinic
  • Set database defaults in Dockerfile
  • All those vales are specific to petclinic. Other apps will have different environment variables to set values such as these
  • All database configurations are consumed via environment variable, so they can be set in the docker file, or in any environment that they run.
  • As you can see in the last line of the docker file, I set the JDBC URL to be something it would never be. I expect to override this in every-runtime.
  • The same should probably be said for the password. It’s fine to keep it simple, and it can be used easily have the app run via docker-compose.
  • There’s a docker-compose file in the repo showing how to accomplish this.
  • For non-secret configurations, ideally set those as early as possible. So just set them in the dockerfile.

3.     Run update.sh

  • Essentially this will build your app into a container and deploy it to OCR, making it available for use immediately by OpenShift without requiring anything outside of your dev box.
  • This will run the oc-env and docker-env evaluate scripts so that the rest of the script can use the oc cli, as well as use the docker damon on the OpenShift vm.
  • Login to local OCR with docker so images can be pushed to it
  • Run mvn install to build a war
  • Run docker build to build the container specified in the dockerfile
  • Run docker push to push the container to OCR
eval $(minishift oc-env) eval $(minishift docker-env)  docker login -u developer -p $(oc whoami -t) $(minishift openshift registry) mvn clean install -DskipTests docker build -t $(minishift openshift registry)/myproject/petclinic:latest -f dockerfiles/Jboss . docker push $(minishift openshift registry)/myproject/petclinic:latest

4.     Edit and Run createApps.sh

  • Line 6 specifies the name of the app.
  • Edit app name in the scripts
  • Call the app whatever you want, or just keep it as is
  • Set configurations in config-map
  • The current setup will create a random password and set it for petclinic and the mysql db.
#!/usr/bin/env bash

eval $(minishift oc-env) oc login -u developer -p $(oc whoami -t)

APP_SVC_NAME=petclinic DB_SV_NAME=$APP_SVC_NAME-mysql  
# set projectname
oc project myproject

# create mySql service
MYSQL_PASSWORD=$(openssl rand -base64 12) oc new-app -e MYSQL_USER=petclinic -e MYSQL_PASSWORD=$MYSQL_PASSWORD -e MYSQL_DATABASE=petclinic --image-
stream=myproject/mysql-55-centos7 --name
$DB_SV_NAME

# create configMap for petclinic
oc create configmap $APP_SVC_NAME-db-config --from-
literal=JDBC_URL=jdbc:mysql://$DB_SV_NAME.myproject.svc:3306/petclinic --from-
literal=JDBC_USERNAME=petclinic ## create secret for petclinicoc create secret generic $APP_SVC_NAME-secret --from-
literal=JDBC_PASSWORD=$MYSQL_PASSWORD

# create petclinic service
oc new-app --image-
stream=myproject/petclinic:latest --
name=$APP_SVC_NAME
oc expose svc/$APP_SVC_NAME
oc set env --from=configmap/$APP_SVC_NAME-db-
config dc/$APP_SVC_NAME
oc set env --from=secret/$APP_SVC_NAME-secret
dc/$APP_SVC_NAME

# get hostname
HOST_NAME=$(oc get route/$APP_SVC_NAME -o custom-columns=HOST:spec.host | tail -n +2) open http://$HOST_NAME/petclinic   

5.     Continue work on your project, and run update.sh at any point to build and redeploy your application.

6.     Once you want to tear everything down that this script has created, run deleteApps.sh

OpenShift Deployments – Final Thoughts

That’s all there is to it. With just a few shell commands, you’re deploying a multi-component application to OpenShift within minutes. If you have any thoughts or questions about OpenShift deployments, please reach out.


Share This Article
Have a question or comment?
Contact uS

Related Posts

Terraform 6 ways
What Is Terraform Used for? 6 Ways to Use Terraform

Terraform is an open-source infrastructure as code (IaC) tool that enables users to define and manage their cloud infrastructure in a declarative and reproducible manner.

Markdown examples floating on top of a laptop computer.
Better Markdown Means Better DevOps

Your Github READMEs and Pull Requests don't have to be boring. Here are 5 markdown features to help level-up repo docs and dev workflow.

Github Actions workflow diagrams
Github Actions For Everything: It Does More Than Build Code

Github Actions has a large set of “workflow triggers” that can be used to kick off new pipeline runs. Used in tandem with the Github Actions and APIs, workflows can be used to automate many parts of the SDLC beyond building and deploying code.

An illustration of a tree with a large trunk and numerous small branches, against a green gradient background.
GitOps: Defining the Best Infrastructure Pattern For You

A trunk-based GitOps approach enables users to deliver software more quickly and efficiently.

The Liatrio logo mark.
About Liatrio

Liatrio is a collaborative, end-to-end Enterprise Delivery Acceleration consulting firm that helps enterprises transform the way they work. We work as boots-on-the-ground change agents, helping our clients improve their development practices, react more quickly to market shifts, and get better at delivering value from conception to deployment.