> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Storage

> Demo showing mounting and using persistent storage in OpenShift via a Flask app that reads /data/testfile.txt, inspects pod filesystems, and configures PVCs

Welcome — this demo covers the basics of using storage in an OpenShift cluster. You will see how a simple Flask app can read a file from a mounted volume at `/data/testfile.txt`, how to inspect pod filesystems, and how to create and mount a PersistentVolumeClaim (PVC) via the OpenShift web console.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/EeHAFJo7ohYv6ASi/images/OpenShift-3-for-the-Absolute-Beginners/Storage-Templates-and-Catalog/Demo-Storage/red-hat-openshift-storage-slide-code.jpg?fit=max&auto=format&n=EeHAFJo7ohYv6ASi&q=85&s=15529f4c404392c3b879860f3a2ed446" alt="A presentation-style slide titled &#x22;Red Hat OpenShift Demo&#x22; with the heading &#x22;Storage.&#x22; A vertical photo inset shows a person typing at a keyboard with code visible on a monitor." width="1920" height="1080" data-path="images/OpenShift-3-for-the-Absolute-Beginners/Storage-Templates-and-Catalog/Demo-Storage/red-hat-openshift-storage-slide-code.jpg" />
</Frame>

## What this app does

* Renders a home page (`/`) that shows the pod hostname and a selected color.
* Changes the displayed color via the route `/color/<new_color>`.
* Reads and displays the contents of `/data/testfile.txt` when a user visits `/read_file`.

If the file `/data/testfile.txt` is not present (e.g., because no volume is mounted at `/data`), the `/read_file` route returns a clear error message. This is useful for verifying whether a PVC is correctly mounted into a pod.

## Flask application (complete example)

Below is the corrected Flask application used in this demo. It implements the routes described above and uses `with open(...)` for safe file handling.

```python theme={null}
# app.py
from flask import Flask, render_template
import socket

app = Flask(__name__)

# Example color map
color_codes = {
    "red": "#ff0000",
    "green": "#00ff00",
    "blue": "#0000ff",
    "default": "#ffffff"
}

@app.route('/')
def index():
    color = color_codes.get("default")
    return render_template('hello.html', name=socket.gethostname(), color=color, contents=None)

@app.route('/color/<new_color>')
def new_color(new_color):
    color = color_codes.get(new_color, color_codes["default"])
    return render_template('hello.html', name=socket.gethostname(), color=color, contents=None)

@app.route('/read_file')
def read_file():
    contents = None
    try:
        with open("/data/testfile.txt", "r", encoding="utf-8") as f:
            contents = f.read()
    except FileNotFoundError:
        contents = "Error: /data/testfile.txt not found on this pod."
    except Exception as e:
        contents = f"Error reading file: {e}"
    return render_template('hello.html', name=socket.gethostname(), color=color_codes["default"], contents=contents)

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)
```

### Color mapping quick reference

| Color name | Hex code |
| - | - |
| red | `#ff0000` |
| green | `#00ff00` |
| blue | `#0000ff` |
| default | `#ffffff` |

## Inspecting the pod filesystem

If `/data` is not mounted or the file does not exist, you can open a terminal for the pod in the OpenShift web console and inspect the root filesystem and mounts.

Example root directory listing from inside a container:

```console theme={null}
(app-root) sh-4.2$ ls /
anaconda-post.log  bin  boot  dev  etc  help.1  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
(app-root) sh-4.2$ ls -l /
total 72
-rw-r--r--    1 root     root      11976 Apr   2 18:39 anaconda-post.log
lrwxrwxrwx    1 root     root          7 Apr   2 18:38 bin -> usr/bin
drwxr-xr-x    3 root     root        4096 Apr  18 14:56 boot
drwxr-xr-x    5 root     root         380 May   1 12:39 dev
drwxr-xr-x   83 root     root        4096 May   1 12:39 etc
-rw-r--r--    1 root     root         9994 Apr  18 14:56 help.1
drwxr-xr-x    2 root     root        4096 Nov   5  2016 home
lrwxrwxrwx    1 root     root          7 Apr   2 18:38 lib -> usr/lib
lrwxrwxrwx    1 root     root          9 Apr   2 18:38 lib64 -> usr/lib64
drwxr-xr-x    2 root     root        4096 May   5  2016 media
drwxr-xr-x    2 root     root        4096 Nov   5  2016 mnt
drwxr-xr-x   13 root     root        4096 May   1 12:39 opt
dr-xr-xr-x  204 root     root           0 May   1 12:39 proc
dr-xr-x---    2 root     root        4096 Apr   2 18:39 root
drwxr-xr-x   13 root     root        4096 May   1 12:39 run
lrwxrwxrwx    1 root     root          8 Apr   2 18:38 sbin -> usr/sbin
drwxr-xr-x    2 root     root        4096 Nov   5  2016 srv
dr-xr-xr-x   13 root     root           0 May   1 02:01 sys
drwxrwxrwt    9 root     root        4096 May   1 12:15 tmp
drwxr-xr-x   42 root     root        4096 Apr  11 08:07 usr
drwxr-xr-x   34 root     root        4096 Apr   2 18:38 var
(app-root) sh-4.2$
```

If `/data` is missing or empty, `/read_file` will fail with the FileNotFound error message returned by the app.

## Add persistent storage with the OpenShift web console

To mount persistent storage at `/data` so the app can read `/data/testfile.txt`, follow these steps in the OpenShift web console:

1. Go to the Storage section and click Create Storage.
2. Create a PersistentVolumeClaim (PVC): provide a name and the requested size.
3. Open your DeploymentConfig (or Deployment) and click Add Storage under Volumes.
4. Select the PVC you created and set the Container Mount Path to `/data`.
5. Redeploy the workload so the pod mounts the PVC.

After redeploying, open the pod terminal and verify `/data` is present and writable.

Example commands you can run inside the pod to create and verify the test file:

```console theme={null}
# inside the pod terminal
(app-root) sh-4.2$ mkdir -p /data
(app-root) sh-4.2$ cat > /data/testfile.txt <<'EOF'
This is test contents
This is more contents
EOF
(app-root) sh-4.2$ cat /data/testfile.txt
This is test contents
This is more contents
```

Now visit `http://<route-or-pod>/read_file` in your browser — the app will read `/data/testfile.txt` and display its contents. If you edit the file inside the pod and refresh the page, the new contents will be displayed.

## Behavior when scaling replicas

* If you scale your application (increase replica count), each replica that mounts the same PVC at `/data` will see the same `testfile.txt`.
* Whether multiple pods can mount the same PVC concurrently depends on the storage class/provisioner and the PVC access mode (e.g., ReadWriteOnce vs ReadWriteMany).

<Callout icon="lightbulb" color="#1CB2FE">
  Make sure your storage class and PVC support the desired access mode (for example, `ReadWriteMany` if you need multiple pods to concurrently mount the same volume). Check the storage class documentation or your cloud provider’s guidance when configuring shared storage.
</Callout>

## Troubleshooting checklist

* Confirm the pod shows the PVC mounted at `/data`.
* Verify the file permissions allow the container user to read `/data/testfile.txt`.
* If `FileNotFoundError` appears, ensure the PVC is bound and the mount path is correct.
* Check pod events and deployment rollout logs for volume-related errors.

## Links and references

* OpenShift Storage concepts: [https://docs.openshift.com/](https://docs.openshift.com/) (search "OpenShift persistent storage" in the docs)
* Kubernetes Persistent Volumes and PersistentVolumeClaims: [https://kubernetes.io/docs/concepts/storage/persistent-volumes/](https://kubernetes.io/docs/concepts/storage/persistent-volumes/)
* Storage class access modes (ReadWriteOnce, ReadWriteMany): [https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes](https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes)

Thanks for following this demo — you should now be able to mount a PVC into an OpenShift pod and serve file contents from `/data/testfile.txt` in your application.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/openshift-3-for-the-absolute-beginners/module/b01015b4-ff27-4531-8507-ffb64893375d/lesson/75979045-e4e5-44ba-abd9-00202a654870" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.