Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Latest commit

 

History

History
88 lines (65 loc) · 2.59 KB

adding-storage.adoc

File metadata and controls

88 lines (65 loc) · 2.59 KB

Adding Storage

By adding a separate PostgreSQL database server, the blog posts were retained across a restart of the web application. The database was saved in a persistent volume, so a restart of the database would also see the posts being retained.

Not all the data a web application collects may be stored in a database though.

Return to the blog site and edit an existing post. Upload an image to be displayed with the post.

Blog Post Image

Once again, kill the running instance of the web application so it is restarted, by running:

oc delete pod --selector app=blog

Refresh the blog post with the image and you will see that the image that was uploaded can no longer be found, even though the record of the post believed an image was attached.

Blog Post Broken

This was because the image is not being stored in the database, but in a directory in the file system. At this point that directory only exists within the container, and so when the web application was restarted by killing the container, the image was lost.

To ensure separate data such as images which are stored in the file system are retained across restarts, it is necessary to mount a persistent volume into the container for the web application.

You can claim a persistent volume and mount it into the web application container at the directory where uploaded images are being stored, by running the command:

oc set volume dc/blog --add --name=blog-images -t pvc --claim-name=blog-images --claim-size=1G -m /opt/app-root/src/media

This will output:

persistentvolumeclaims/blog-images
deploymentconfig "blog" updated

As this was a change to the deployment configuration, the web application will be automatically restarted.

Edit the post again and clear the link for the broken image and upload an image again. Delete the pod for the web application again by running:

oc delete pod --selector app=blog

and the image should this time still be present after the restart.

You can see what persistent volumes you have claimed and which are associated with your project by running:

oc get pvc

This should display output similar to:

NAME          STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
blog-db       Bound     pv0033    100Gi      RWO,ROX,RWX   3h
blog-images   Bound     pv0017    100Gi      RWO,ROX,RWX   2m

The output will show not only the persistent volume you just claimed to store upload images, but also that previously created when the PostgreSQL database was deployed.