Using persistent disks with multiple readers

This page explains how to add a persistent disk to your cluster using the ReadOnlyMany access mode. This mode allows multiple Pods on different nodes to mount the disk for reading.

For more information on this mode, refer to persistent volume access modes.

Formatting the persistent disk

Before using a persistent disk in read-only mode, you must format it.

To format your persistent disk:

  1. Create a persistent disk manually or by using dynamic provisioning.

  2. Format the disk and populate it with data. To format the disk, you can:

    • Reference the disk as a ReadWriteOnce volume in a Pod. Doing this results in GKE automatically formatting the disk, and enables the Pod to pre-populate the disk with data. When the Pod starts, make sure the Pod writes data to the disk.
    • Manually mount the disk to a VM and format it. Write any data to the disk that you want. For details, see Persistent disk formatting.
  3. Unmount and detach the disk:

    • If you referenced the disk in a Pod, delete the Pod, wait for it to terminate, and wait for the disk to automatically detach from the node.
    • If you mounted the disk to a VM, detach the disk using gcloud compute instances detach-disk.
  4. Create Pods that access the volume as ReadOnlyMany as shown in the following section.

Creating a PersistentVolume and PersistentVolumeClaim

To mount a pre-populated disk as ReadOnlyMany, create a PersistentVolume and PersistentVolumeClaim for the disk with the accessModes fields set to ReadOnlyMany and the gcePersistentDisk readOnly field set to true.

Following is an example named read-only-pd.yaml:

apiVersion: v1
kind: PersistentVolume
  name: my-readonly-pv
  storageClassName: ""
    storage: 10Gi
    - ReadOnlyMany
    namespace: default
    name: my-readonly-pvc
    pdName: my-test-disk
    fsType: ext4
    readOnly: true
apiVersion: v1
kind: PersistentVolumeClaim
  name: my-readonly-pvc
  # Specify "" as the storageClassName so it matches the PersistentVolume's StorageClass.
  # A nil storageClassName value uses the default StorageClass. For details, see
  storageClassName: ""
    - ReadOnlyMany
      storage: 10Gi

To create the PersistentVolume and PersistentVolumeClaim use kubectl apply -f read-only-pd.yaml.

Using the PersistentVolumeClaim in a Pod

When using this PersistentVolumeClaim in your workloads, you need to specify readOnly: true in the volumeMounts and volumes sections of your Pod specification, as shown in the following example:

apiVersion: v1
kind: Pod
  name: pod-pvc
  - image:
    name: busybox
      - "sleep"
      - "3600"
    - mountPath: /test-mnt
      name: my-volume
      readOnly: true
  - name: my-volume
      claimName: my-readonly-pvc
      readOnly: true

Now, you can have multiple Pods on different nodes that can all mount this PersistentVolumeClaim in read-only mode. However, you can't attach persistent disks in write mode on multiple nodes at the same time. For more information, see Deployments vs. StatefulSets.

What's next