<p>I'm setting up my application with Kubernetes. I have 2 Docker images (Oracle and Weblogic). I have 2 kubernetes nodes, Node1 (20 GB storage) and Node2 (60 GB) storage.</p>

<p>When I run <code>kubectl apply -f oracle.yaml</code> it tries to create oracle pod on Node1 and after few minutes it fails due to lack of storage. How can I force Kubernetes to check the free storage of that node before creating the pod there?</p>

<p>Thanks</p>

<p>First of all, you probably want to give Node1 more storage. </p>

<p>But if you don't want the pod to start at all you can probably run a check with an <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/" rel="nofollow noreferrer"><code>initContainer</code></a> where you check how much space you are using with something like <a href="https://en.wikipedia.org/wiki/Du_(Unix)" rel="nofollow noreferrer"><code>du</code></a> or <a href="https://en.wikipedia.org/wiki/Df_(Unix)" rel="nofollow noreferrer"><code>df</code></a>. It could be a script that checks for a threshold that exits unsuccessfully if there is not enough space. Something like this:</p>

<pre><code>#!/bin/bash 

# Check if there are less than 10000 bytes in the &lt;dir&gt; directory
if [ `du &lt;dir&gt; | tail -1 | awk '{print $1}'` -gt "10000" ]; then exit 1; fi
</code></pre>

<p>Another alternative is to use a <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/" rel="nofollow noreferrer">persistent volume</a> (PV) with a <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims" rel="nofollow noreferrer">persistent volume claim</a> (PVC) that has enough space together with the default <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/" rel="nofollow noreferrer">StorageClass</a> <a href="https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#defaultstorageclass" rel="nofollow noreferrer">Admission Controller</a>, and you do allocate the appropriate space in your volume definition.</p>

<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 40Gi
  storageClassName: mytype
</code></pre>

<p>Then on your Pod:</p>

<pre><code>kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
    - name: mycontainer
      image: nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim
</code></pre>

<p>The Pod will not start if your claim cannot be allocated (There isn't enough space)</p>
