Usage
Storage in Kubernetes¶
The following items are related to data storage in Kubernetes:
- Volume : shares data between containers in a Pod
- PersistentVolume (PV) : storage resource
- PersistentVolumeClaim (PVC) : request for storage
- StorageClass : dynamique storage provisioning
- StatefulSet : used to manage stateful applications
PersistentVolume / PersistentVolumeClaim¶
A PersistentVolume provides the storage, it can be provisioned either statically (by an administrator) or dynamically (by a StorageClass).
A PersistentVolumeClaim is a request for storage, it decouples an application from the storage solution and allows storage to be consumed by a Pod
StorageClass¶
A StorageClass is a resource which allows PV to be dynamically created. It is referenced within the specification of a PVC.
Provisioning with a StorageClass¶
The cluster we created already has a storage class, it is based on the csi.exoscale.com provisioner thus allows the creation of block storage on the Exoscale infrastructure. The specification of this StorageClass is the following one:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: exoscale-sbs
provisioner: csi.exoscale.com
reclaimPolicy: Delete
volumeBindingMode: Immediate
allowVolumeExpansion: true
In order to use this StorageClass, we define a PVC (PersistentVolumeClaim) referencing the StorageClass:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
storageClassName: exoscale-sbs
resources:
requests:
storage: 10Gi
Next we reference this PVC in our application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
Creating the Deployment and the PVC triggers the creation of a PV giving access to some block storage.
Using Storage in the VotingApp¶
The previous exercise left the app with the following components:
We will now use the existing StorageClass and define PVCs to request storage for both databases used in the VotingApp: