Store persistent data in Red Hat OpenShift using PVCs

Without data, software has no value. Data needs to be created, stored, updated, and retrieved. One way to store persistent data in OpenShift is by using a Persistent Volume Claim (PVC). In this learning path, created by Don Schenck, you will learn how to create a PVC and access it with your pod.

At the end of the last lesson, your app was still failing. In this lesson, you will learn how to use PVC and create a new deployment to solve your issue.

What you need

What you will do

  • Find your pod ID
  • Create a read-only deployment
  • View your results

Add the file to your PVC

This step involves copying a file into the pod. First, you need the pod ID where the app is running. Use the following command to discover the pod ID:

PowerShell:

oc get pods | Select-String filecontents

Bash:

oc get pods | grep filecontents

As as example:

oc get pods | Select-String filecontents

filecontents-85c497c9f-cl6w7   1/1     Running   0          146m

Inside the directory that was created in the prerequisites by downloading or cloning the Github repo , you will find the file mytext.txt. Move into the directory and run the following command to copy the file to the PVC, using your own pod ID:

oc cp .\mytext.txt {pod_id_goes_here}:/mypvc

As an example:

oc cp .\mytext.txt filecontents-85c497c9f-cl6w7:/mypvc

View your results in a curl loop

In the command-line session running the curl loop, you will now see the following results:

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

…

Delete deployment and objects

Deleting the deployment will not delete the PVC. At your command line, run the following command to delete the deployment and all associated objects:

oc delete all -l filecontents=backend

Once again, the application will fail because the application no longer exists.

Create a new deployment using PVC as read-only

Run the following two commands to create a new deployment and new route for the app:

oc new-app quay.io/rhdevelopers/filecontents:1.0.0 --labels tier=backend,language=java,filecontents=backend,sandbox=pvcs
oc create route edge --service=filecontents --port=8080 --labels tier=backend,filecontents=backend,sandbox=pvcs

The curl loop should once again show the Hello World output.

Finally, change the environment variables for the deployment to reference the file mytext.txt at the file path /mypvc. The path and file already exist inside the PVC that was created earlier. Can you predict what will happen?

 oc set volumes deploy/filecontents --add –claim-name=mypvc --mount-path=/mypvc --name=mypvc --type=persistentVolumeClaim --read-only=true

View results using curl command

OpenShift will perform a rolling update and you will be able see the new output in the curl loop window:

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

Hello World from the file 'mytext.txt' inside the PVC

…

Developer notes

Move it, Remove it, Improve it

Move

You can download all of the YAML files associated with this application and use them to move the application to another OpenShift instance by selecting Export Application in the upper right corner of the OpenShift dashboard.

Remove

You can remove the objects for this activity with the following command:

  • oc delete all -l sandbox=pvcs

Improve

Some ideas to improve or alter this activity:

  • Write your own back-end function in a different language.
  • Use an initContainer to load the file, mytext.txt.
Previous resource
Create a filecontents application and update environment variables