Limit persistent volume claim content folder size using hostPath

1/26/2020

I created a persistent volume claim with 20Mi storage limits and 17 Mi storage requests but I can upload a 50 Mi file in the hostPath folder. But what I want is to limit the folder content size. If the folder has more than 20Mi content in it so Kubernetes should say it does not allow new files.

Persistent volume:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
  namespace: limit-range
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  hostPath:
    path: /home/me/kubernetes/web
    type: DirectoryOrCreate

Persistent volume claim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: pvc
    namespace: limit-range
spec:
    storageClassName: local-storage
    accessModes:
        - ReadWriteOnce
    resources:
        limits:
            storage: 20Mi
        requests:
            storage: 17Mi

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
    name: upload-preview-2
    namespace: limit-range
spec:
    selector:
        matchLabels:
            app: upload-preview-2
    template:
        metadata:
            labels:
                app: upload-preview-2
        spec:
            containers:
                - image: upload-preview:latest
                  imagePullPolicy: Never
                  name: web
                  ports:
                    - containerPort: 80
                  volumeMounts:
                    - mountPath: /var/www/html/web
                      name: localpath
                      subPath: dev

            volumes:
              - name: localpath
                persistentVolumeClaim:
                    claimName: pvc

With that configuration it does not give me what I expected. So I tried Limit range and Quota and still without the expected behavior.

Limit range:

apiVersion: v1 kind: LimitRange metadata:   name: storagelimits   namespace: limit-range spec:   limits:
  - type: PersistentVolumeClaim
    max:
      storage: 18Mi
    min:
      storage: 10Mi

Quota:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: storagequota
  namespace: limit-range
spec:
  hard:
    persistentvolumeclaims: "5"
    requests.storage: "18Mi"

I tried kubectl describe quota -n limit-range and the result is:

Name:                   storagequota
Namespace:              limit-range
Resource                Used  Hard
--------                ----  ----
persistentvolumeclaims  1     5
requests.storage        17Mi  18Mi

My thought is maybe it does not work because I'm using hostPath.

-- user3502626
kubernetes

1 Answer

1/26/2020

There is no limit on how much space an emptyDir or hostPath volume can consume, and no isolation between Containers or between Pods.

In the future, we expect that emptyDir and hostPath volumes will be able to request a certain amount of space using a resource specification, and to select the type of media to use, for clusters that have several media type.

Official link for reference

-- DT.
Source: StackOverflow