<p>I created a persistent volume claim with <code>20Mi storage limits</code> and <code>17 Mi storage requests</code> but I can upload a 50 Mi file in the <code>hostPath</code> 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.</p>

<p>Persistent volume:</p>

<pre><code>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
</code></pre>

<p>Persistent volume claim:</p>

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

<p>Deployment:</p>

<pre><code>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
</code></pre>

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

<p>Limit range:</p>

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

<p>Quota:</p>

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

<p>I tried <code>kubectl describe quota -n limit-range</code> and the result is:</p>

<pre><code>Name:                   storagequota
Namespace:              limit-range
Resource                Used  Hard
--------                ----  ----
persistentvolumeclaims  1     5
requests.storage        17Mi  18Mi
</code></pre>

<p>My thought is maybe it does not work because I'm using <code>hostPath</code>.</p>

user3502626
<p><strong>There is no limit on how much space an emptyDir or hostPath volume can consume</strong>, and no isolation between Containers or between Pods.</p>

<p>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.</p>

<p><a href="https://kubernetes.io/docs/concepts/storage/volumes/#resources" rel="noreferrer">Official link for reference</a></p>
