2

I want to share multiple volumes using PersistentVolume reqource of kubernetes.

I want to share "/opt/*" folders in pod. But not the "/opt":

kind: PersistentVolume
apiVersion: v1
metadata:
  name: demo
  namespace: demo-namespace
  labels:
    app: myApp
    chart: "my-app"
    name: myApp
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: "myApp-data"
  hostPath:
    path: /opt/*

But in pod I am not able to see shared volume. If I share only "/opt" folder then it goes shown in pod.

Is there anything I am missing?

1 Answers1

1

If you want to share a folder among some pods or deployments or statefulsets you should create PersistentVolumeClaim and it's access mode should be ReadeWriteMany.So here is an example of PersistentVolumeClaim which has ReadeWriteMany mode

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
accessModes:
  - ReadWriteMany
resources:
  requests:
    storage: 3Gi

then in your pods you should use it as below ...

    apiVersion: v1
    kind: Pod
    metadata:
      name: mypod01
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: c01
          image: alpine
          volumeMounts:
            - mountPath: "/opt"
              name: task-pv-storage
    apiVersion: v1
    kind: Pod
    metadata:
      name: mypod02
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: task-pv-claim
      containers:
        - name: c02
          image: alpine
          volumeMounts:
            - mountPath: "/opt"
              name: task-pv-storage