Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Openshift: Lab 13 – How to deploy a Java application on Kubernetes

Prerequisites

What you will learn

In this learning path, you will:

  • Deploy a MySQL database with configured environment variables
  • Deploy the Spring Pet Clinic application from source code with configured environment variables

How long will this learning path take?

  • About 30-45 minutes

In this lesson, you will:

  • Create a deployment of an ephemeral instance of MySQL
  • Set the environment variables for credentials and database name


Creating the deployment

The Pet Clinic application uses a MySQL database to store data, and this lesson creates that MySQL installation and the necessary database (petclinic) in the same OpenShift project as the Pet Clinic app.

Info alert:While this lesson is specific to the Pet Clinic application, the same steps are used for any application for which you wish to use a MySQL database in OpenShift.

  1. Begin by logging into the Developer Sandbox. The dashboard will appear in your browser.
  2. Select the Developer perspective (Figure 1, Callout 1), then click the +Add option (Figure 1, Callout 2), then click the Database panel (Figure 1, Callout 3).Figure 1: Prepare to install the database using the web-based dashboard.
  3. The Databases section of the Developer Catalog is displayed. Click on the MySQL (Ephemeral) panel (Figure 2).Figure 2: Selecting the MySQL Ephemeral database.
  4. The description of the MySQL Ephemeral database is displayed. Click the Instantiate Template button to continue (Figure 3).Figure 3: Brief description of MySQL Ephemeral database instance.
  5. The Instantiate Template panel is displayed. There are several options. 
  6. Supply the following four values (Figure 4):
    • MySQL Connection Username: petclinic
    • MySQL Connection Password: petclinic
    • MySQL root user Password: petclinic
    • MySQL Database Name: petclinic
       
    • Figure 4: Preparing to create the MySQL instance.
  7. Click Create and the installation will happen. You will see the icon in your dashboard (Figure 5).Figure 5: MySQL is up and running.

That’s it. The ephemeral instance of MySQL is up and running as an OpenShift service (named mysql). In the next lesson, you will create the Spring Pet Clinic application that uses this database.

Deploy the Spring Pet Clinic application with configured environment variables

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.. 

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x