This tutorial we will deploy a containerized java application with its mysql database. In this setup you will 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 will involves a complete setup and teardown of an application in openshift.
If an application is going to run ifn 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 this 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 learning how to deploy openshift 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 setup 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 setup your docker environment to use the docker client/socket on the mini shift virtual machine. This has some benefits including using the already allotted resources for mini shift, uses 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.
Building and Deploying
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 to be used 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
So that’s all there is to it. With just a few shell commands, you’re deploying a multi-component application to openshift within minutes. Hope you enjoyed that tutorial. If you have any thoughts or questions, please comment below or tweet at @liatrio or @bjaminstein.
Liatrio is a collaborative DevOps consulting firm that helps enterprises drive innovation, expedite world-class software delivery and evolve their culture, tools and processes.
We work as “boots on the ground change agents,” helping our clients improve their development practices, get better at delivering value from conception to deployment and react more quickly to market changes. Our tech veterans have the experience needed to execute on DevOps philosophies and Continuous Integration (CI) and Continuous Delivery (CD) core practices, including automation, sharing, and feedback. Through proven tools and processes and a collaborative culture of shared responsibilities, we unite our clients’ technology organizations to help them become high-performing teams. Reach out — let’s start the conversation.
Liatrio is also hiring! If you want to be a part of a team that solves challenges around software delivery automation, deployment pipelines and large-scale transformations, contact us!