How to use Same EFS for mounting multiple directories in Kubernetes deployment

9/16/2020

I am trying to find a solution to make use of the same Amazon EFS for mounting multiple directories in the Kubernetes deployment. Here is my use case

  • I have an application named app1 that needs to persist a directory named "/opt/templates" to EFS
  • I have another application named app2 that needs to persist a directory named "/var/logs" to EFS

We deploy the applications as a Kubernetes Pod in the Amazon EKS cluster. If i am using the same EFS for both the above mounts, i can see all the files from both the directories "/opt/templates" and "/var/logs" as i am using the same EFS.

How can i solve the problem of using same EFS for both the application without seeing app1 mounted files in app2 directory ? Is it even possible of using the same EFS ID for multiple applications ?

Here is the Kubernetes manifests i used for for one of the application which includes PersistentVolume, PVC and the Deployment

----
apiVersion: v1
kind: PersistentVolume
metadata:
  name: efs-pv-1
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: efs-sc-report
  csi:
    driver: efs.csi.aws.com
    volumeHandle: fs-XXXXX

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: efs-pvc-1
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: efs-sc
  resources:
    requests:
      storage: 2Gi
---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: deploy1
  template:
    metadata:
      labels:
        app: deploy1
    spec:
      containers:
      - name: app1
        image: imageXXXX
        ports:
        - containerPort: 6455
        volumeMounts:
          - name: temp-data
            mountPath: /opt/templates/  
      volumes:
      - name: shared-data
        emptyDir: {}
      - name: temp-data
        persistentVolumeClaim:
          claimName: efs-pvc-1 
-- babs84
amazon-efs
amazon-web-services
file-storage
kubernetes
persistent-volumes

1 Answer

9/17/2020

It looks like you can do that by including the path as part of the volume handle.

A sub directory of EFS can be mounted inside container. This gives cluster operator the flexibility to restrict the amount of data being accessed from different containers on EFS.

For example:

volumeHandle: [FileSystemId]:[Path]

I think you will need to create two separate PVs and PVCs, one for /opt/templates, and the other for /var/logs, each pointing to a different path on your EFS.

-- Brian Pursley
Source: StackOverflow