'Kubernetes Deployment with StorageClass
I have a StorageClass with provisioner: kubernetes.io/aws-ebs
And I have a deployment where I need to mount some volumes. So, I need to use this StorageClass
Here's my sc.yaml
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
Here's my deployment.yaml
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: "IfNotPresent"
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
I need to specify the storage class in my deployment. Can someone help me?
Solution 1:[1]
I need to specify the storage class in my deployment. Can someone help me?
Before we answer, first let's explain how StroageClass, PersistentVolume, Persistent Volume Claim.
Terminology
StroageClass
- We can look at
StorageClassas a driver (Software). - A driver which responsible for the "communication" with the storage hardware.
- Usually but not a must, the
StorageClassis supplied by the Storage provider (hardware or virtual)
PersistentVolume
- A
PersistentVolume(PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned usingStorage Classes.
PersistentVolumeClaim
- A
PersistentVolumeClaim(PVC) is a request for storage by a user (Usually Pod)
General Diagram (describing K8 Storage objects)
TL;DR; Explanation
- You have physical storage (Disk, SSD, virtual, etc)
- Someone (usually the storage or the cloud provider) supplied you with the
StorageClassobject. By the way, you don't need to define/declare it most of the time and K8S will supply default storage for you (emptyDir). - Then you define
PersistentVolume(PV) which will "create" storage based upon the type (StorageClass) you require. - Next step is to define
PersistentVolumeClaim(PVC). The PVC is the allocation of the "physical" storage mounted from the (PV) which you defined in the previous step. - The last step is to "assign" volume to your execution (
Pod,Deployment,StatefulSet, etc) which is done usingvolumes.
** Notes
- As mentioned above most of the time you can simply use volumes without the need to define
StorageClassorPV/PVC. Simply use a volume in the required Resources and K8S will take care of that for you. - There are some exceptions (without getting into too many details here like
StaefulSet). If no StorageClass is specified, then the default StorageClass will be used
Now let's answer your question
- You have defined a
StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
...
provisioner: kubernetes.io/aws-ebs
- In your deployment, you specified
volumes(identation left as as)
apiVersion: extensions/v1beta1
kind: Deployment
...
# --> 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
# ->>>> 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
The missing piece ...
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pv-claim ### <<< 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
Hope it helped you out.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CodeWizard |

