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:
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.
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.
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
fields set to
ReadOnlyMany and the
readOnly field set to
Following is an example named
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
volumes sections of your Pod
specification, as shown in the following example:
apiVersion: v1 kind: Pod metadata: name: pod-pvc spec: containers: - image: k8s.gcr.io/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.
- Learn more about Using preexisting persistent disks as PersistentVolumes.
- Learn more about Compute Engine persistent disks.