Multiple persistent volumes in minikube

1/29/2019

I want to attach multiple volumeMounts to deployment in minikube on one persistent volume claim.

I have a tomcat deployed app that needs to preserve three different folders (/var/log/app, /var/lib/app, /etc/app). When the container starts I should get files in every catalog but only logs get populated and the app fails. Any idea if my yaml is correct or am I approaching this the wrong way?

K8s yaml:

apiVersion: v1

kind: PersistentVolume
metadata:
  name: pv-volume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 6Gi
  hostPath:
    path: /data/pv-volume
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: manual
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: app
spec:
  selector:
    matchLabels:
      app: app
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - image: app:latest
        name: app
        env:
        - name: JAVA_OPTS
          value: -Xms64M -Xmx512M
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1024Mi"
            cpu: "500m"
        volumeMounts:
          - name: pv-storage1
            mountPath: /var/log/app
            subPath: log
          - name: pv-storage1
            mountPath: /var/lib/app
            subPath: lib
          - name: pv-storage1
            mountPath: /etc/app
            subPath: etc
        tty: true
      volumes:
      - name: pv-storage1
        persistentVolumeClaim:
          claimName: pv-claim
      restartPolicy: Always
status: {}

v2:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-log
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data/pv-volume/log
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-log
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-lib
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data/pv-volume/lib
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-lib
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-etc
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: /data/pv-volume/etc
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-etc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: endpoint
  labels:
    app: endpoint
spec:
  selector:
    matchLabels:
      app: endpoint
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: endpoint
    spec:
      containers:
      - image: endpoint:latest
        name: endpoint
        env:
        - name: JAVA_OPTS
          value: -Xms64M -Xmx1024M
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "1024Mi"
            cpu: "250m"
          limits:
            memory: "2048Mi"
            cpu: "500m"
        volumeMounts:
          - name: pv-storage-log
            mountPath: /var/log
          - name: pv-storage-lib
            mountPath: /var/lib
          - name: pv-storage-etc
            mountPath: /etc
        tty: true
      volumes:
      - name: pv-storage-log
        persistentVolumeClaim:
          claimName: pv-claim-log
      - name: pv-storage-lib
        persistentVolumeClaim:
          claimName: pv-claim-lib
      - name: pv-storage-etc
        persistentVolumeClaim:
          claimName: pv-claim-etc
      restartPolicy: Always
status: {}
-- Jacek Mackiewicz
kubernetes
minikube
persistent-volumes

1 Answer

1/29/2019

With your configuration above, you try to mount one PV to multiple directory, which means they will carry same data with your hostpath on PV configuration.

If you want to use multiple different directory with different content, you need to create PV and PVC for each directory such as below:

kind: PersistentVolume
metadata:
  name: pv-volume-log
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 6Gi
  hostPath:
    path: /data/pv-volume/log
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-log
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: manual

---
kind: PersistentVolume
metadata:
  name: pv-volume-lib
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 6Gi
  hostPath:
    path: /data/pv-volume/lib
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-lib
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: manual
---
kind: PersistentVolume
metadata:
  name: pv-volume-etc
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 6Gi
  hostPath:
    path: /data/pv-volume/etc
  storageClassName: manual
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-etc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: manual

And in your Deployment yaml add those volumes and attach volumes to mountPath respectively:

...
      volumes:
      - name: pv-storage-log
        persistentVolumeClaim:
          claimName: pv-claim-log
      - name: pv-storage-lib
        persistentVolumeClaim:
          claimName: pv-claim-lib
      - name: pv-storage-etc
        persistentVolumeClaim:
          claimName: pv-claim-etc
      restartPolicy: Always
...

I hope it will be useful for you

-- coolinuxoid
Source: StackOverflow