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
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.
- Set database defaults in Dockerfile
- 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 installto build a war
docker buildto build the container specified in the dockerfile
docker pushto 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
- 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 petclinic oc 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
- Continue work on your project, and run update.sh at any point to build and redeploy your application.
- 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.
Liatrio is an Enterprise Delivery Acceleration consulting firm that helps enterprises transform into world-class technology delivery organizations through successful adoption of DevOps collaboration and Lean software delivery practices. We work as “boots on the ground change agents,” uniting enterprise technology organizations by uplifting culture, tools, and processes.
Want to learn more? Let’s have a conversation.
Liatrio is also hiring! To learn more about our hiring process, check out recent posts on hiring tips, our interview process, our hiring process, and hiring success the Liatrio Way. If you want to be a part of a team that solves challenges around software delivery automation, deployment pipelines and large-scale transformations, reach out!