<p>I have a <code>StorageClass</code> with <code>provisioner: kubernetes.io/aws-ebs</code></p>
<p>And I have a deployment where I need to mount some volumes. So, I need to use this <code>StorageClass</code></p>
<p>Here's my <code>sc.yaml</code></p>
<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
  name: gp2
parameters:
  fsType: ext4
  type: gp2
provisioner: kubernetes.io/aws-ebs
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
</code></pre>
<p>Here's my <code>deployment.yaml</code></p>
<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres
  namespace: var.namespace
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: &quot;IfNotPresent&quot;
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      # how can I specify my storage class here?
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
</code></pre>
<p>I need to specify the storage class in my deployment. Can someone help me?</p>

Jananath Banuka
<blockquote>
<p>I need to specify the storage class in my deployment. Can someone help me?</p>
</blockquote>
<p>Before we answer, first let's explain how <code>StroageClass</code>, <code>PersistentVolume</code>, <code>Persistent Volume Claim</code>.</p>
<h3>Terminology</h3>
<h4><code>StroageClass</code></h4>
<ul>
<li>We can look at <code>StorageClass</code> as a driver (Software).</li>
<li>A driver which responsible for the &quot;communication&quot; with the storage hardware.</li>
<li>Usually but not a must, the <code>StorageClass</code> is supplied by the Storage provider (hardware or virtual)</li>
</ul>
<h4><code>PersistentVolume</code></h4>
<ul>
<li>A <code>PersistentVolume</code> (PV) is a <strong>piece of storage</strong> in the cluster <strong>that has been provisioned</strong> by an administrator or dynamically provisioned using <code>Storage Classes</code>.</li>
</ul>
<h4><code>PersistentVolumeClaim</code></h4>
<ul>
<li>A <code>PersistentVolumeClaim</code> (PVC) is a <strong>request for storage</strong> by a user (Usually Pod)</li>
</ul>
<hr />
<h3>General Diagram (describing K8 Storage objects)</h3>
<p><a href="https://i.stack.imgur.com/53Bxl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/53Bxl.png" alt="K8 Storage Diagram" /></a></p>
<h2>TL;DR; Explanation</h2>
<ul>
<li>You have physical storage (Disk, SSD, virtual, etc)</li>
<li>Someone (usually the storage or the cloud provider) supplied you with the <code>StorageClass</code> object. By the way, you don't need to define/declare it most of the time and K8S will supply default storage for you (<a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">emptyDir</a>).</li>
<li>Then you define <code>PersistentVolume</code> (PV) which will &quot;create&quot; storage based upon the type (<code>StorageClass</code>) you require.</li>
<li>Next step is to define <code>PersistentVolumeClaim</code> (PVC). The PVC is the allocation of the &quot;physical&quot; storage mounted from the (PV) which you defined in the previous step.</li>
<li>The last step is to &quot;assign&quot; volume to your execution (<code>Pod</code>, <code>Deployment</code>, <code>StatefulSet</code>, etc) which is done using <code>volumes</code>.</li>
</ul>
<p>** Notes</p>
<ul>
<li>As mentioned above most of the time you can simply use volumes without the need to define <code>StorageClass</code> or <code>PV</code>/<code>PVC</code>. Simply use a volume in the required  Resources and K8S will take care of that for you.</li>
<li>There are some exceptions (without getting into too many details here like <code>StaefulSet</code>).
If no StorageClass is specified, then the default StorageClass will be used</li>
</ul>
<hr />
<p>Now let's answer your question</p>
<ul>
<li>You have defined a <code>StorageClass</code></li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
...
provisioner: kubernetes.io/aws-ebs
</code></pre>
<ul>
<li>In your deployment, you specified <code>volumes</code> (identation left as as)</li>
</ul>
<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
...
        
        # --&gt; Here you define the actual mount path you need for your pods
        #     The name (PVC) is corresponding to the one you 
        #     defined below under volumes
        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      # -&gt;&gt;&gt;&gt; how can I specify my storage class here?
      # You don't need to specify storage class, you need to define PVC,
      # This is the missing piece in your code.
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
</code></pre>
<h3>The missing piece ...</h3>
<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim ### &lt;&lt;&lt; The name as mentioned above in your Deploymnet
  labels:
    app: postgres
spec:
  # The name of the storage class u defined earlier
  storageClassName: gp2

  # The access modes are:
  #   ReadWriteOnce - The volume can be mounted as read-write by a single node
  #   ReadWriteMany - The volume can be mounted as read-write by a many nodes
  #   ReadOnlyMany  - The volume can be mounted as read-only  by many nodes
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
</code></pre>
<hr />
<p>Hope it helped you out.</p>
