One of the great things about Red Hat OpenShift is the ability to develop both Cloud Native and traditional applications. Often times, when thinking about traditional applications, the first thing that comes to mind is the ability to store things on the file system. This could be media, metadata, or any type of content that your application relies on but isn’t stored in a database or other system.
To illustrate the concept of persistent storage (i.e. storage that will persist even when a container is stopped or recreated), I created a sample application for tracking my electronic books that I have in PDF format. The library of PDF files can be stored on the file system, and the application relies on this media directory to present the titles to the user. The application is written in Java using the Spring Boot framework and scans the media directory for PDF files. Once a suitable title is found, the application generates a thumbnail image of the book and also determines how many pages it contains. This can be seen in the following image:
Don’t worry if you don’t have any electronic books as the application will actually download a couple of OpenShift-related books when you first run the application. One of which is written by yours truly.
Let’s go ahead and get started in deploying the application to an OpenShift cluster.
Step 1 – Creating a project
The first thing we need to do is create a project where we can deploy the application that we will be working with. After you have authenticated to your OpenShift cluster, select to create a new project and name it pdfrack as shown in the following image:
If you are more of a command line person, the command would be:
$ oc new-project pdfrack –display-name=”PDF Rack” –description=”My book library in pdf format”
Step 2 – Deploy the application
For this sample application, we are going to take advantage of the Source-to-Image project that is included with OpenShift. In a nutshell, the S2I project allows a developer to simply pass in their source code repository to the OpenShift platform and the system will build the container image and run it.
Browse the catalog and select to use the Red Hat OpenJDK 8 builder as shown in the following image:
After you select Red Hat OpenJDK 8, you will be presented with a wizard to define the configuration of your deployment. On the second step of the wizard, select advanced options as shown here:
This will present a screen that will allow you to define some more options for the deployment. Enter the following information:
Application Name: pdfrack
Git Repository: https://github.com/gshipley/pdfrack.git
Scroll down to the environment variables section and provide the following environment variable:
JAVA_ARGS with a value of –media=/media
This environment variable tells the application where to look for the media to display to the user.
The last configuration option we need to specify is the amount of memory to use for the application. Given that this application is generating images from PDF files and calculating page counts and other data, I have decided to specify that is needs 2GB of RAM. Perform this change as shown below:
Once you have entered all of the configuration options, click on create.
Step 3 – Adding persistent storage
Now that our application has been deployed, we want to add persistent storage so that our PDF files are not lost between container restarts. To do this, select storage and then click on create storage.
On this page, you can define your storage requirements. For this application, I am going to name my storage pdfrackbooks and specify that I want to use 2 GB of total storage. I plan on adding a lot of books so I want to make sure I have enough disk space to hold them.
Once the storage has been allocated, you will see the following confirmation screen:
Step 4 – Attaching storage to deployment using a persistent volume claim
At this point, all we have done is reserved some persistent storage on the cluster that we can use in the future. However, to actually made use of this persistent volume we need to make a claim against for our deployment of the pdfrack application.
To do this, browse to your deployment and select the pdfrack deployment:
Once of this screen, click on pdfrack. This will present the deployment configuration for your application. Select the Actions button in the top right of the screen and then select to add storage:
On this page, you should see the persistent volume you created in step 3 of this post. At this point, all we need to do is provide the path that we want the persistent volume we are claiming to be mounted at. If you recall from a previous step, we provided the location of the media directory as /media. Given that, we want this claim to be mounted at that location as seen here:
Once you are satisfied with your configuration, click on Add. This will create a new deployment of your application as the configuration has changed.
Step 5 – Read your library of books
Congratulations, you have now deployed a Spring Boot application that contains a persistent volume with all your books. At this point, there is nothing left to do other than grab a cup of coffee, sit back, and enjoy one of the OpenShift books in your library.