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?
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
.
StroageClass
StorageClass
as a driver (Software).StorageClass
is supplied by the Storage provider (hardware or virtual)PersistentVolume
PersistentVolume
(PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes
.PersistentVolumeClaim
PersistentVolumeClaim
(PVC) is a request for storage by a user (Usually Pod)StorageClass
object. By the way, you don't need to define/declare it most of the time and K8S will supply default storage for you (emptyDir).PersistentVolume
(PV) which will "create" storage based upon the type (StorageClass
) you require.PersistentVolumeClaim
(PVC). The PVC is the allocation of the "physical" storage mounted from the (PV) which you defined in the previous step.Pod
, Deployment
, StatefulSet
, etc) which is done using volumes
.** Notes
StorageClass
or PV
/PVC
. Simply use a volume in the required Resources and K8S will take care of that for you.StaefulSet
).
If no StorageClass is specified, then the default StorageClass will be usedNow let's answer your question
StorageClass
apiVersion: storage.k8s.io/v1
kind: StorageClass
...
provisioner: kubernetes.io/aws-ebs
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With