How to pre-populate a ReadOnlyMany Persistent Volume

9/2/2019

I am trying to create a deployment in GKE that uses multiple replicas. I have some static data which I want to have available in every pod. This data will not be updated, no write is required.

I decided to use a PV with a corresponding PVC with the ReadOnlyMany storage class. The thing is, I do not know how to actually transfer my data to the volume - since it is read-only. I tried using

gcloud compute scp /local/path instance:/remote/path

but of course, I get a permission error. I then tried creating a new PV via the console. I attached it to a VM with

gcloud compute instances attach disk

mounted and formatted the disk, transfered my data, unmounted the disk, detached it from the VM and finally created a PVC following the documentation. I changed the storage class to ReadOnlyMany, the only difference.

But still, when I'm trying to scale my deployment to more than one replicas I get an error saying the disk is already attached to another node.

So, how can I create a volume that is to be used in ReadOnlyMany and populate the disk with data? Or is there a better approach since no write is required?

Thanks in advance

-- Nikolaos Paschos
google-kubernetes-engine
kubernetes
persistent-storage

2 Answers

9/2/2019

Hello Nikolaos,

The approach you are following depends heavily on your use case.

The approach you are following is very common when you are using a distributed file system as CEPH, GlusterFS or GCP Cloud Filestore or remote file systems as NFS.

When using distributed FS or remote FS the approach is:

1.- Create a PV with the AccessMode set to RWO and with the Reclaim Policy set to RETAIN.

2.- Create the PVC

3.- Attach the PVC to a POD

4.- Transfer the data to the volume via the POD.

5.- Delete the pod, the pvc and the pv.

6.- Create a new PV with the AccessMode set to ROX and with the Reclaim Policy set to RETAIN for EACH Deployment or POD you want to attach the data. This not applies if the POD replica number is greater than 1 because pod will attach the same PV.

7.- Create a PVC for each PV. The relationship PV and PVC is 1 : 1 8.- Attach the PVC to each POD or Deployment you want to use.

Your issue seems to be that you are trying to attach the same PV to multiple PVC and that is not allowed, the relationship PVC <--> PV is one-on-one.

Regarding your other question if there is a better approach, that depends heavily on your use case. Google Cloud Platform offers a lot of storage options [1]. For example, if you are using objects you can use Google Cloud Storage [2] instead of Persistent Disks.

[1] https://cloud.google.com/kubernetes-engine/docs/concepts/storage-overview

[2] https://cloud.google.com/filestore/docs/accessing-fileshares

-- Armando Cuevas
Source: StackOverflow

1/27/2020

Worked for me. Have you specified readOnly: true when using persistent volume claim in the Pod template?

volumes:
- name: my-volume
  persistentVolumeClaim:
    claimName: my-readonly-pvc
    readOnly: true

See details here https://cloud.google.com/kubernetes-engine/docs/how-to/persistent-volumes/readonlymany-disks

-- Ievgen Goichuk
Source: StackOverflow