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 if 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 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 project
eval $(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.
- Fork spring-petclinic-framework so that you can make changes and check in your changes
- Alter the Dockerfile
COPY target/petclinic.war /opt/jboss/wildfly/standalone/deployments/petclinic.war
ENV JDBC_PASSWORD=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 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
mvn install to build a war
docker build to build the container specified in the dockerfile
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.
eval $(minishift oc-env) oc login -u developer -p $(oc whoami -t)
# 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
# create configMap for petclinic
oc create configmap $APP_SVC_NAME-db-config --from-
literal=JDBC_USERNAME=petclinic ## create secret for petclinic oc create secret generic $APP_SVC_NAME-secret --from-
# create petclinic service
oc new-app --image-
oc expose svc/$APP_SVC_NAME
oc set env --from=configmap/$APP_SVC_NAME-db-
oc set env --from=secret/$APP_SVC_NAME-secret
# 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
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 comment below or tweet at @liatrio or @bjaminstein.