Kubernetes: Use two VolumeMount's in same PersistentVolume

1k views Asked by At

It is possible to use in AKS the same Persistent Volume / PVC for two different VolumeMounts, i This what i'm trying:

Pod Deploy / Here I am setting two VolumeMount's in same PersistentVolumeClaim

(...)
          volumeMounts:
            - name: data1
              mountPath: /opt/data/data1
              subPath: data1
            - name: data2
              mountPath: /opt/data/data2
              subPath: data2
      volumes:
        - name: data1
          persistentVolumeClaim:
            claimName: pv-data
        - name: data2
          persistentVolumeClaim:
            claimName: pv-data
(...)

Persistent Volume Claim used by data1 and data2 Volumes, defined in Pod deploy

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-data
spec:
  accessModes:
  - ReadWriteMany # or ReadWriteOnce
  storageClassName: pv-storage
  resources:
    requests:
      storage: 40Gi

Persistent Volume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pvdisk
spec:
  capacity:
    storage: 40Gi
  storageClassName: pv-storage
  azureDisk:
    kind: Managed
    diskName: disk1
    diskURI: /subscriptions/xxxxx/resourceGroups/xxxx/providers/Microsoft.Compute/disks/disk1
    fsType: ext4
  accessModes:
  - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  claimRef:
    name: pv-data
    namespace: default

When try to aplly this "idea" I've got the following error:

persistentvolumeclaim "data1" not found Warning FailedScheduling 1s (x2 over 5s) default-scheduler pod has unbound immediate PersistentVolumeClaims (repeated 2 times)

Thanks Tiago

EDIT kubectl describe pvc pv-data

Name:          sonar-data
Namespace:     default
StorageClass:  sonar-storage
Status:        Bound
Volume:        pvdisk
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      40Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Mounted By:    pod-69d6c8d89c-8t5lb
               pod-69d6c8d89c-8t5lb

kubectl describe pv pvdisk

Name:            pvdisk
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    sonar-storage
Status:          Bound
Claim:           default/data
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        40Gi
Message:
Source:
    Type:         AzureDisk (an Azure Data Disk mount on the host and bind mount to the pod)
    DiskName:     XXXXXX
    DiskURI:      XXXXXX
    Kind:         Managed
    FSType:       ext4
    CachingMode:  ReadWrite
    ReadOnly:     false
Events:           <none>

Pod describe

Volumes:
  data1:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  data1
    ReadOnly:   pv-data
  config-volume:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      data-config
    Optional:  false
  data2:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  pv-data
    ReadOnly:   false
    
(...)
    
Events:
  Type     Reason       Age                    From                                    Message
  ----     ------       ----                   ----                                    -------
Warning  FailedMount  2m21s (x63 over 142m)  kubelet, aks-pool1-19402761-vmss000002  Unable to mount volumes for pod "pod-69d6c8d89c-8t5lb_default(77795ef3-6c88-480a-b3ed-18b9453c742a)": timeout expired waiting for volumes to attach or mount for pod "default"/"pod-69d6c8d89c-8t5lb". list of unmounted volumes=[data1]. list of unattached volumes=[data1 config-volume data2 default-token-nhrrv]
1

There are 1 answers

0
Sakeeb Hossain On

For this to work, you only need to declare the volume once.

(...)
          volumeMounts:
            - name: data
              mountPath: /opt/data/data1
              subPath: data1
            - name: data
              mountPath: /opt/data/data2
              subPath: data2
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: pv-data
(...)