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

5/2/2019

My pod declares two different volumes.

I use some definition templating, and dependending on the environment in some cases I would like to use 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]
  • It works fine with two different claims.
  • It is possible to use the same claim in several pods.

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?

-- egt
kubernetes
persistent-volume-claims
persistent-volumes

2 Answers

5/2/2019

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.

-- cookiedough
Source: StackOverflow

5/2/2019

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 ?

-- machine424
Source: StackOverflow