How to use same EFS Filesystem as two static Persistent Volumes?

66 Views Asked by At

I want to use same efs as two static volumes.

For that, I have created the below two PersistentVolume Yamls:

Two PVs

apiVersion: v1
kind: PersistentVolume
metadata:
 name: pv001
spec:
 capacity:
  storage: 5Gi
 volumeMode: Filesystem
 accessModes:
 - ReadWriteMany
 persistentVolumeReclaimPolicy: Delete
 csi:
  driver: efs.csi.aws.com
  volumeHandle: fs-0fd788366150f8667.efs.us-east-1.amazonaws.com
  volumeAttributes:
   path: "/logs"


apiVersion: v1
kind: PersistentVolume
metadata:
 name: pv002
spec:
 capacity:
  storage: 5Gi
 volumeMode: Filesystem
 accessModes:
 - ReadWriteMany
 persistentVolumeReclaimPolicy: Delete
 csi:
  driver: efs.csi.aws.com
  volumeHandle: fs-0fd788366150f8667.efs.us-east-1.amazonaws.com
  volumeAttributes:
   path: "/data"

Two PVCs

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: pvc001
spec:
 resources:
  requests:
   storage: 3Gi
 volumeMode: Filesystem
 accessModes:
 - ReadWriteMany

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
 name: pvc002
spec:
 resources:
  requests:
   storage: 6Gi
 volumeMode: Filesystem
 accessModes:
 - ReadWriteMany

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
 name: test
spec:
 replicas: 3
 selector:
  matchLabels:
   app: test
 template:
  metadata:
   labels:
    app: test
  spec:
   containers:
   - image: nginx
     name: nginx-cont
     volumeMounts:
     - name: logs
       mountPath: /usr/share/nginx/html
     - name: data
       mountPath: /var/www/html
   volumes:
   - name: logs
     persistentVolumeClaim:
      claimName: pvc001
   - name: data
     persistentVolumeClaim:
      claimName: pvc002

However, this is throwing below error:

Unable to attach or mount volumes: unmounted volumes=[logs], unattached volumes=[], failed to process volumes=[logs]:
1

There are 1 best solutions below

3
On

I usually do this a little different. I create the pv:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: efs-pv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  persistentVolumeReclaimPolicy: Retain
  csi:
    driver: efs.csi.aws.com
    volumeHandle: fs-0000000000000000e

Then I create the pvc:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: efs-claim
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi

And then I use it like this (just as an example):

apiVersion: v1
kind: Pod
metadata:
  name: upperlower-pod
  labels:
    app: upperlower
spec:
  containers:
    - name: upperall
      image: dm/upperall:latest
      imagePullPolicy: Always
      volumeMounts:
        - name: persistent-storage
          mountPath: /data
  volumes:
    - name: persistent-storage
      persistentVolumeClaim:
        claimName: efs-claim

Inside the application then something like this:

...
# Configure the logger
log_dir = '/data/logs'
log_filename = 'app.log'
log_path = os.path.join(log_dir, log_filename)

logging.basicConfig(
    filename=log_path,
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

incoming_dir = '/data/incoming'
processed_dir = '/data/processed'
...

More info: https://aws.amazon.com/blogs/storage/persistent-storage-for-kubernetes/