How to create a PV and PVC which is accessible by multiple pods and cronjobs in Kubernetes on a local setup

11/7/2020

I am trying to create a PV and PVC which is accessible by a pod and a cronjob too on the same node.

Currently I am using the following yaml file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: wwwroot
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "wwwroot"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: wwwroot
  name: wwwroot
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Gi
status: {}

in the statefulSet

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: web
  name: web
spec:
  selector:
    matchLabels:
      app: web
  serviceName: web
  replicas: 1
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: xy:latest
        ports:
        - containerPort: 80
        - containerPort: 443
        volumeMounts:
        - mountPath: /app/wwwroot
          name: wwwroot
          subPath: tempwwwroot
      imagePullSecrets:
        - name: xy
      restartPolicy: Always
      serviceAccountName: ""
  volumeClaimTemplates:
    - metadata:
        name: wwwroot
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 100Gi    

This works fine with the pods, but I have a cronjob that needs to access the same wwwroot PV.

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  labels:
    app: import
  name: import
spec:
  schedule: "09 16 * * *"
  jobTemplate:
    spec:
      template:
        spec:  
          containers:
          - name: import
            image: xy:latest
            imagePullPolicy: Always
            volumeMounts:
            - mountPath: /app/wwwroot
              name: wwwroot
              subPath: tempwwwroot
          imagePullSecrets:
           - name: xy
          restartPolicy: OnFailure
          volumes:
          - name: wwwroot
            persistentVolumeClaim:
              claimName: wwwroot

To the best of my knowledge, you cannot bind these PVs to a folder on the host machine like in Docker (which would be nice, but I can live with that). When I run a kubectl get pvc with the following setup, it seem to use the same PVC name wwwroot, but clearly it is not. I have /bin/bash-d into the pod created by the cronjob and the wwwroot folder does not contain the files from the web pod wwwroot folder.

Any idea what am I doing wrong? Also what is the best option the create PV/PVCs on a local 1 node Kubernetes setup? I was also thinking about to create a NFS share on the host for easier access to the files.

Also when first created the PV/PVC and deployed the web statefulSet, the web pod should populate the wwwroot PV with CSS and JS files, but seems like when it bounds to the PV, it deletes everything from that folder and I have to manually copy those files again. Any workaround on that?

-- Sugafree
kubernetes
persistent-volume-claims

1 Answer

11/8/2020

You are mounting pv on two pod created and using pv by setting accessModes: ReadWriteOnce

You can check them below. https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes

Also instant of local you can use storage class NFS volume which can give you read-write-many. I am not sure Storage class local provide it as it is not in official documents. Chk below https://kubernetes.io/docs/concepts/storage/persistent-volumes/

P.S. Never use storage class in actual deployment as node in k8s come and go.

-- yogesh kunjir
Source: StackOverflow