Prerequisites:- Kubernetes Cluster should be up and running.
- Inside our Host Machine, we are setting up the Kubernetes Cluster.
- Then we have to set up the node,
- Inside the node, we have three components:- POD, Persistent Volume Claim, and Persistent Volume.
- These three components will sit inside the node
- The fourth one is STORAGE which will sit on your host machine i.e. UBUNTU.
- The PV and PVC are used to store the information on external storage so that when the pod lifecycle is over you don’t lose the information or data which is present inside a particular pod.
For Example:- like a database, if your application is having a database then it is better to have this external storage for Kubernetes Cluster so that data is still present.
ABOUT PERSISTENT VOLUME(PV)
- It is a piece of storage.
- Assume it is like a Hard Disk where you store all kinds of documents, images, or any important stuff, so whenever you shut down your system or reboot your system, all things were there on Hard Disk.
- Persistent Volume is the same concept in Kubernetes Cluster,
- Once you shut down your Pod or restart it, you don’t lose your data it is always there, you just need to connect to your PV.
- PV can be on your LOCAL STORAGE means on your laptop or desktop or you can allocate a certain volume of your local storage to the Kubernetes cluster.
- Or, you can use CLOUD STORAGE like GCP, AWS.
- You can connect your cloud storage to the Kubernetes cluster so that instead of storing on the local system you can store on cloud storage.
WHY DO WE NEED PERSISTENT VOLUMES?
- As there are different storage volumes available such as AmazonEBS, GCE Persistent Disk, etc. So, we use these storage types for different purposes based on the data we are storing on it and the way we access it.
- This storage type falls under Block Storage, NFS, Object Storage, others.
- The problem here is if you want to attach and manage these storage types manually then we have to download different plugins such as Disk Capacity, Monitoring Disk, etc.
- To overcome this problem, we need one storage type for all durable storage options
The solution for this is PERSISTENT VOLUMES
- Persistent Volumes provides a standard API for developers and administrators.
- It allows developers and administrators to abstract the details of how storage is provided and how it is consumed.
- This helps them to focus on storage capacity and storage types where their application is present.
- It is a storage request.
- This means you are going to claim your storage.
- PVC is like a claim for that storage that we have created.
- We write a YAML file for claiming storage and this will claim from your local or cloud storage.
QUICK OVERVIEW ON HOW TO USE PV, PVC, AND PODS
- First, create PV either on a local or on a cloud machine.
- Define your PVC and POD.
- Now, how do these three integrate:- create a YAML configuration,
- Then you need to connect all three like – PVC is going to call PV as shown in the above Diagram.
- The pod is going to connect to PVC.
- Whatever you are going to deploy on your Pod is stored in your PVC which is your storage.
Provisioning of storage can be done in two ways:-
- STATIC VOLUME PROVISIONING– In static provisioning, we need to create the Persistent Volume in advance.
Imagine- we have a storage infrastructure with different types of storage like NFS, Object Storage, SSD Hard Disks, etc.
- The first step is to create the Persistent Volume which is done by the administrator.
- The administrator creates storage chunks with different capacities (these chunks are known as PV).
- So, as and when needed, the Developer can create PVC to request storage.
For Example:- Developer requests for 2G of storage by creating the PVC, then PVC bounds with the 2G PV, now the developer can make use of this bounded PVC inside the Pod.
- DYNAMIC VOLUME PROVISIONING– In dynamic provisioning, there is no need to create the Persistent Volume in advance, they are created parallelly when PVC is created.
Assume- we have storage infrastructure with different types of storage. In this we do not create the PV manually instead we create the STORAGE CLASSES (these are the tags given to a storage based on the type of medium in the backend).
- First, create Persistent Volume YAML
apiVersion: v1 kind: PersistentVolume metadata: name: persistv labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
- KIND is PersistentVolume(kind defines what we want to create).
- Inside metadata, we define the NAME of our PV.
- Inside spec we define storageClassName, the capacity of storage(how much storage we want), accessModes, and hostPath for PV.
2. Create Persistent Volume Claim YAML
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: persistvclaim spec: storageClassName: manual accessModes: - ReadWriteOnce resources: requests: storage: 3Gi
- it is similar to the PV YAML file except for the name and hostPath.
3. Create Pod YAML
apiVersion: v1 kind: Pod metadata: name: pod-pv spec: containers: - name: debian image: debian:9-slim command: ["/bin/sleep"] args: [ "3600" ] volumeMounts: - mountPath: /mnt name: storage volumes: - name: storage persistentVolumeClaim: claimName: persistvclaim
- In the Pod YAML file, the volume is mounted in /mnt inside the pod.
- A Debian image is used for creating the container. You can use any image.
- The main highlight inside this is claimName which is the same as in the PVC file.
- The pod is going to connect to this PVC and then PVC requests for storage.
Now, using execute command we can write anything inside the container or store our data for example:-
- I simply wrote “hello world” inside the inside /mnt/helloworld,
- If I delete this pod and restart it again, the content will still persist.
-Using Persistent Volume, you can store your data on local as well as cloud storage by just connecting your storage with Kubernetes Cluster and for a certain reason, if you delete your pod, the data will still persist as shown in the above demo.
-Using Persistent Volume Claim for storing your data, you can request storage(like 5G, 10G, etc).