egt
egt

Reputation: 185

Is it possible to have the same PVC as two volumes in Kubernetes?

My pod declares two different volumes.

I use some definition templating, and depending on the environment in some cases I would like to reuse the same claim for the two volumes.

This results in an error:

    Unable to mount volumes for pod "task-pv-pod_<...>": timeout expired waiting for volumes to attach/mount for pod "<...>"/"task-pv-pod". list of unattached/unmounted volumes=[task-pv-storage1]

This is a simplified pod definition:

kind: Pod
apiVersion: v1
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage1
      persistentVolumeClaim:
       claimName: task-pv-claim
    - name: task-pv-storage2
      persistentVolumeClaim:
       claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage1
        - mountPath: "/usr/share/nginx/something-else"
          name: task-pv-storage2

So why is it not working?

Upvotes: 9

Views: 6289

Answers (3)

mirekphd
mirekphd

Reputation: 6743

The same PVC ("claim" from your question) for:

  • two (or more) container mountPaths- YES,
  • two volumes - NO.

If we are pointing to the same PVC:

  • there can be only one volume AND
  • both (all) volumeMounts should point to the same volume (task-pv-storage in your case, not task-pv-storage1 and task-pv-storage2).

My example (notice I'm using the same volume called fs-db-rwx for both mounts):

Mount: fs-db-rwx → /data read-write
Mount: fs-db-rwx → /opt/redis read-write

Deployment config (relevant parts):

              volumeMounts:
                - name: fs-db-rwx
                  mountPath: /data
                - name: fs-db-rwx
                  mountPath: /opt/redis
[..]
            volumes:
            - name: fs-db-rwx
              persistentVolumeClaim:
                claimName: fs-db-rwx

User's perspective: both container paths have the same contents:

$ ls -lan /data
total 818484
drwxr-x---    2 65534    65534          123 Mar 16 10:47 .
drwxr-xr-x    1 0        0               28 Mar 16 10:47 ..
-rw-r--r--    1 65534    65534    838103626 Mar 15 20:00 dump-rdb-20220315-bak
-rw-r--r--    1 65534    65534           93 Mar 16 10:47 dump.rdb
-rw-r--r--    1 65534    65534            5 Dec  4 12:45 local.txt
-rw-r--r--    1 65534    65534         2911 Mar 16 10:14 redis.conf
-rwxr-xr-x    1 65534    65534          333 Dec  4 13:05 upload-file-to-artifactory.sh

$ ls -lan /opt/redis/
total 818484
drwxr-x---    2 65534    65534          123 Mar 16 10:47 .
drwxr-xr-x    1 0        0               19 Mar 16 10:47 ..
-rw-r--r--    1 65534    65534    838103626 Mar 15 20:00 dump-rdb-20220315-bak
-rw-r--r--    1 65534    65534           93 Mar 16 10:47 dump.rdb
-rw-r--r--    1 65534    65534            5 Dec  4 12:45 local.txt
-rw-r--r--    1 65534    65534         2911 Mar 16 10:14 redis.conf
-rwxr-xr-x    1 65534    65534          333 Dec  4 13:05 upload-file-to-artifactory.sh

Upvotes: 2

cookiedough
cookiedough

Reputation: 3832

To answer your question "is it possible to use the same claim in several pods?", let's take a look at different claim attach access modes:

When you create a PVC with the default settings, you are creating a Persistent Volume and a Claim that sits on top of it, with the attach access mode ReadWriteOnce.

ReadWriteOnce – the volume can be mounted as read-write by a single node

So this claim can only be mounted on pods on the same node. There is a workaround to be able to mount this volume onto multiple pods. One is to schedule all of your pods on the same node, which technically defeats the purpose of using container orchestration. This could be achieved by assigning pods to nodes. Read the linked doc for details.

Another method is to use disk persistent volumes / NFS. Depending on the cloud provider you are using there are different FileSystem provisioners you can use.

This way you can change your access mode to ReadWriteMany:

ReadWriteMany – the volume can be mounted as read-write by many nodes

With this access policy you can mount your volume onto multiple pods in your cluster regardless of the node they are running on.

Upvotes: 6

machine424
machine424

Reputation: 155

According to the doc : "Once bound, PersistentVolumeClaim binds are exclusive, regardless of how they were bound. A PVC to PV binding is a one-to-one mapping." https://kubernetes.io/docs/concepts/storage/persistent-volumes/#binding Does it make sense ?

Upvotes: 3

Related Questions