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

1

7

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 
Walston answered 16/9, 2020 at 23:58 Comment(0)
E
4

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.

Eliathas answered 17/9, 2020 at 0:41 Comment(2)
Thanks for your answer. I do have a separate PV and PVC for both the application. I even tried to add the path as you suggested here, but i am getting an error like Event : templates Pod templates-559775f8b7-rzb5w dev MountVolume.SetUp failed for volume "efs-pv-1" : kubernetes.io/csi: mounter.SetupAt failed: rpc error: code = Internal desc = Could not mount "fs-XXXX:/etc/fstab/templates" at "/var/lib/kubelet/pods/XXXXXXXXX/volumes/kubernetes.io~csi/efs-pv-report-dev/mount": mount failed: exit status 32Walston
Looks like i need to create a sub directory while provisioning EFS, after that i need to add the sub directory path to the volumeHandle: [FileSystemId]:[Path].Walston

© 2022 - 2024 — McMap. All rights reserved.