⎈ k8s knowledge compiler

Configure a Pod to Use a PersistentVolume for Storage [page]deterministic

tutorialsstorage

This page shows you how to configure a Pod to use a [PersistentVolumeClaim](#gloss:persistent-volume-claim) for storage. Here is a summary of the process:

1. You, as cluster administrator, create a PersistentVolume backed by physical storage. You do not associate the volume with any Pod.

1. You, now taking the role of a developer / cluster user, create a PersistentVolumeClaim that is automatically bound to a suitable PersistentVolume.

1. You create a Pod that uses the above PersistentVolumeClaim for storage.

##

* You need to have a Kubernetes cluster that has only one Node, and the [kubectl](#gloss:kubectl) command-line tool must be configured to communicate with your cluster. If you do not already have a single-node cluster, you can create one by using [Minikube](https://minikube.sigs.k8s.io/docs/).

* Familiarize yourself with the material in [Persistent Volumes](/docs/concepts/storage/persistent-volumes/).

## Create an index.html file on your Node

Open a shell to the single Node in your cluster. How you open a shell depends on how you set up your cluster. For example, if you are using Minikube, you can open a shell to your Node by entering `minikube ssh`.

In your shell on that Node, create a `/mnt/data` directory:

```shell # This assumes that your Node uses "sudo" to run commands # as the superuser sudo mkdir /mnt/data ```

In the `/mnt/data` directory, create an `index.html` file:

```shell # This again assumes that your Node uses "sudo" to run commands # as the superuser sudo sh -c "echo 'Hello from Kubernetes storage' > /mnt/data/index.html" ```

> Note: If your Node uses a tool for superuser access other than `sudo`, you can usually make this work if you replace `sudo` with the name of the other tool.

Test that the `index.html` file exists:

```shell cat /mnt/data/index.html ```

The output should be:

``` Hello from Kubernetes storage ```

You can now close the shell to your Node.

## Create a PersistentVolume

In this exercise, you create a *hostPath* PersistentVolume. Kubernetes supports hostPath for development and testing on a single-node cluster. A hostPath PersistentVolume uses a file or directory on the Node to emulate network-attached storage.

In a production cluster, you would not use hostPath. Instead a cluster administrator would provision a network resource like a Google Compute Engine persistent disk, an NFS share, or an Amazon Elastic Block Store volume. Cluster administrators can also use [StorageClasses](/docs/reference/generated/kubernetes-api//#storageclass-v1-storage-k8s-io) to set up [dynamic provisioning](/docs/concepts/storage/dynamic-provisioning/).

Here is the configuration file for the hostPath PersistentVolume:

The configuration file specifies that the volume is at `/mnt/data` on the cluster's Node. The configuration also specifies a size of 10 gibibytes and an access mode of `ReadWriteOnce`, which means the volume can be mounted as read-write by a single Node. It defines the [StorageClass name](/docs/concepts/storage/persistent-volumes/#class) `manual` for the PersistentVolume, which will be used to bind PersistentVolumeClaim requests to this PersistentVolume.

> Note: This example uses the `ReadWriteOnce` access mode, for simplicity. For production use, the Kubernetes project recommends using the `ReadWriteOncePod` access mode instead.

Create the PersistentVolume:

```shell kubectl apply -f https://k8s.io/examples/pods/storage/pv-volume.yaml ```

View information about the PersistentVolume:

```shell kubectl get pv task-pv-volume ```

The output shows that the PersistentVolume has a `STATUS` of `Available`. This means it has not yet been bound to a PersistentVolumeClaim.

``` NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE task-pv-volume 10Gi RWO Retain Available manual 4s ```

## Create a PersistentVolumeClaim

The next ste …(trimmed)

Sources

tutorials/configuration/configure-persistent-volume-storage.md · docConfigure a Pod to Use a PersistentVolume for Storage

Related (15)

references Persistent Volume ClaimPersistentVolumeClaim conf=1
references Kubectlkubectl conf=1
part_of {{% heading "prerequisites" %}}describes conf=1
part_of Create an index.html file on your Nodedescribes conf=1
part_of Create a PersistentVolumedescribes conf=1
part_of Create a PersistentVolumeClaimdescribes conf=1
part_of Create a Poddescribes conf=1
part_of Clean updescribes conf=1
part_of Access controldescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Create a new nginx.conf filedescribes conf=1
part_of Referencedescribes conf=1
api_for PersistentVolumedocuments API object conf=1

← all Docs