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.
    • Manually mount the disk to a VM and format it. 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 to a VM, detach the disk using gcloud compute instances detach-disk.
  4. Create Pods that access the volume as ReadOnlyMany as shown below.

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
metadata:
  name: my-readonly-pv
spec:
  storageClassName: ""
  capacity:
    storage: 10Gi
  accessModes:
    - ReadOnlyMany
  claimRef:
    namespace: default
    name: my-readonly-pvc
  gcePersistentDisk:
    pdName: my-test-disk
    fsType: ext4
    readOnly: true
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-readonly-pvc
spec:
  # Specify "" as the storageClassName so it matches the PersistentVolume's StorageClass.
  # A nil storageClassName value uses the default StorageClass. For details, see
  # https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
  storageClassName: ""
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      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
metadata:
  name: pod-pvc
spec:
  containers:
  - image: gcr.io/google_containers/busybox
    name: busybox
    command:
      - "sleep"
      - "3600"
    volumeMounts:
    - mountPath: /test-mnt
      name: my-volume
      readOnly: true
  volumes:
  - name: my-volume
    persistentVolumeClaim:
      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.