How to deploy a Java application on Kubernetes in minutes

Move your legacy Java application into a container and deploy it to Kubernetes. The Developer Sandbox for Red Hat OpenShift (Developer Sandbox) is a free OpenShift cluster that gives you access to the cutting-edge technologies built on Kubernetes. A quick sign-up gets you a cluster and access to a set of developer tools and services. 

In this learning path created by Don Schenck, you will learn how to create a set of microservices and move this Spring Pet Clinic example application into a container using the Source-to-Image (s2i) feature.

Camels, pythons, and cats — oh my! In this scenario, the line of animals and their owners has surrounded the building, and the employees at the Pet Clinic can’t keep up with demand. The clinic needs an application to help them run the clinic more efficiently. Management wants to keep track of the pets, their owners, and the veterinarians that work at the Pet Clinic. You’ll need to create a set of microservices that lets employees view the list of veterinarians and their specialties, add new pets and owners, plus update information about existing pets and owners, and manage their visits. Of course, the application needs to be delivered yesterday.

In order to get full benefit from taking this lesson, you need to:

  • Have access to the OpenShift dashboard
  • Have the MySQL database service available

In this lesson, you will:

  • Create a deployment of the Spring Pet Clinic application with the necessary environment variables

Deploy from source using S2I

One of the ways to create an application is by using the Source-To-Image (S2I) feature. The S2I feature allows you to import source code from a git repository and let OpenShift automatically build and start an image. In this lesson, you will create the Spring Pet Clinic app from source.

Begin by logging into your sandbox; the dashboard will appear in your browser.

Select the Developer perspective (Figure 1, Callout 1), then click the +Add option (Figure 1, Callout 2), then click the Git Repository panel (Figure 1, Callout 3).

Select the Developer perspective then click the +Add option then click the Git Repository panel
Figure 1: Prepare to create the app using source code.

When the Import from Git panel is displayed, enter the following value for the git repo (Figure 2, Callout 1): https://github.com/redhat-developer-demos/spring-petclinic

You will see the Import is not possible error message. When you specify a Git repo, OpenShift will inspect it and try to determine the best way to build the source code.

The three options are:

  • Build from source as-is using a build image for the programming language, e.g. Java
  • Use any Dockerfile in the source code to build the app
  • Use any development configuration file (devfile) to build the app

In this case, the source code contains a development configuration file (devfile) that is outdated, causing the error. You need to override this by clicking on the Edit Import Strategy link (Figure 2, Callout 2).

Override the import error by clicking on the Edit Import Strategy link.
Figure 2: Specify the Git repo to Import.

After selecting the Edit Import Strategy link, choose the Builder Image option (Figure 3).

Choose the Builder Image option
Figure 3: Choosing the Java builder image.

Scroll down and open the Resource type dropdown list. Select the Deployment option (Figure 4).

Scroll down and open the Resource type dropdown list then select the Deployment option
Figure 4: Resource types for an application.

Select the link to access the advanced options for the Build configuration (Figure 5).

Select the link to access the advanced options for the Build configuration
Figure 5: Preparing to add environment variables to the Build configuration.

Enter the two necessary Environment variables:

  • SPRING_PROFILES_ACTIVE mysql
  • MYSQL_URL jdbc:mysql://mysql:3306/petclinic

Click Create to build the application (Figure 6).

Enter the two necessary Environment variables and click Create to build the application
Figure 6: Adding environment variables prior to the build.

The build will begin. After a few minutes, you will see the app icon showing the completed build. You will know the build is complete when the outer ring turns dark blue. When it's complete, click the Open URL link to display the app (Figure 7).

Click the Open URL link to display the app
Figure 7: View the app in your browser.

Your browser will open to the Spring Petclinic app (Figure 8).

Your browser will open to the Spring Petclinic app
Figure 8: The app is up and running.

Summary

Congratulations. You've done it. We have walked you through a simplistic example that demonstrates the power of OpenShift S2I.  This lesson has walked you through entering required parameters and creating the Spring Petclinic app from source code.. 

Want to learn more? Try these:

Previous resource
Deploy a MySQL database with configured environment variables