Persistent Storage
- In a typical IT environment storage is managed by the storage/system administrator. The end user will just get instructions to use the storage, but does not have to worry about the underlying storage management.
- in the containerized world, We would like to follow similar rules, but it becomes challenging given the many types we have seen earlier kubernetes resolves this problem with persistent volume (PV) system.
Persistent Volume (PV)
- A persistent volume (PV) is a cluster wide resource that you can use to store data in a way that it persists beyond the lifetime of a pod.
- The persistent volume is not backed by locally attached storage on a worker node but by networked storage system such as Elastic Block Storage (EBS) or Network File System (NFS) or a distributed file system like ceph.
- K8s provides API for users and administrator to manage and consume storage. To manage the volume it was the PersistentVolume API resource type and to consume it uses the PersistentVolumeClaim API resource type.
Persistent Volume Claim (PVC)
- In order to use a PV you need to claim it first, using a PV claim.
- The PVC requests a PV with your desired specification (size, access modes, and speed, etc.) from kubernetes And once a suitable persistent volume is found, it is bound to a persistent volume claim.
- After a successful bound to a pod, you can mount it as a Volume.
- Once a user finishes its works, the attached persistent volume can be released. The underlying PV can be reclaimed and recycled for future usage.
AWS EBS
An AWS EBS Volume mounts an AWS EBS volume into your pod unlike emptyDir which is erased when a pod is removed, the contents of an EBS volume are preserved and the volume is merely unmounted.
There are some restrictions-
- The nodes on which courts are running must be aws EC2 instance
- Those instances need to be in the same region and availability zone as the EBS volume.
- EBS only supports a single EC2 instance mounting a volume.
Implementation
Create a Volume (as per required storage) in the same AWS region as your EC2 running and copy the volume-id (vol-XXXXXXXXX).
Create 3 manifest files -
persistent_volume.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: myebsvol
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
awsElasticBlockStore:
volumeID: <YOUR_EBS_VOLUME_ID> #vol-094d8143b2ad46204
fsType: ext4
persistent_volume_claim.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: myebsvolclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
k8s_example_14.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-14
spec:
replicas: 1
selector: # tells the controller which pods to watch/belong to
matchLabels:
app: mypv
template:
metadata:
labels:
app: mypv
spec:
containers:
- name: shell
image: centos
command: ["/bin/bash", "-c", "sleep 10000"]
volumeMounts:
- name: mypd
mountPath: "/tmp/persistent"
volumes:
- name: mypd
persistentVolumeClaim:
claimName: myebsvolclaim