Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
  • It works fine with two different claims.
  • It is possible to reuse 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?

like image 672
egt Avatar asked May 02 '19 17:05

egt


People also ask

What is a PVC object in Kubernetes?

Each of these Persistent Volumes is consumed by a Kubernetes Pod (or Pods) by issuing a PersistentVolumeClaim object -- a PVC. A PVC object lets pods use storage from Persistent Volumes. A Persistent Volume is an abstraction for the physical storage device that you have attached to the cluster.

Can a Kubernetes container work as a persistent volume?

It definitely can work in the sense that the container can restart but we are still kind of vulnerable to anything goes wrong at the pod level itself. The other types of objects present in Kubernetes are Persistent Volume claim and persistent volume. I have created a diagram to specify the differences between the PVC and PV.

How are PVS provisioned in Kubernetes?

Provisioning —a PV can be provisioned either manually, via an administrator, or dynamically, based on pre-configured PVCs. Binding —when a user applies a PVC to a pod, Kubernetes searches for a PV with the required amount of storage and other requested storage criteria, and binds it exclusively to the pod.

How do I bind a PV to a Kubernetes pod?

Binding —when a user applies a PVC to a pod, Kubernetes searches for a PV with the required amount of storage and other requested storage criteria, and binds it exclusively to the pod. Using —at this stage, the bound PV is reserved for a specific pod.


Video Answer


3 Answers

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.

  • AWS: EFS Provisioner - helm
  • GKE: GCE NFS Disk
  • Other cloud providers: NFS Server Provisioner

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.

like image 94
cookiedough Avatar answered Oct 08 '22 19:10

cookiedough


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 ?

like image 22
machine424 Avatar answered Oct 08 '22 21:10

machine424


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
like image 2
mirekphd Avatar answered Oct 08 '22 21:10

mirekphd