Skip to content

Latest commit

 

History

History
 
 

Persistent Volumes

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Kubernetes persistent volumes and persistent volume claim on Minikube Cluster

In this demo, we will see how to persist data of a kubernetes pods using persistent volume on minikube cluster.

Pre-requisites to implement this project:

  • Create 1 virtual machine on AWS with 2 CPU, 4GB of RAM (t2.medium)
  • Setup minikube on it Minikube setup.

What we are going to implement:

  • In this demo, we will create persistent volumes (PV) and persistent volume claim (PVC) to persist the data of an application so that it can be restored if our application crashes.

Steps to implement ingress:

1) Create minikube cluster as mentioned in pre-requisites :

2) Check minikube cluster status and nodes :

minikube status
kubectl get nodes

3) Create persistent volume yaml file :

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
  labels:
    env: dev
spec:
  storageClassName: standard
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/ubuntu/data"

4) Apply persistent volume :

kubectl apply -f PersistentVolume.yaml

image

5) Create one more yaml file for persistent volume claim :

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-pv-claim
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi

6) Apply persistent volume claim :

kubectl apply -f PersistentVolumeClaim.yaml

image

7) Create a simple nginx pod yaml to attach volume :

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  volumes:
    - name: nginx-storage
      persistentVolumeClaim:
        claimName: nginx-pv-claim
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: 80
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: nginx-storage

8) Apply Pod yaml file :

kubectl apply -f Pod.yaml

image

9) Now exec into the above the created pod i.e. nginx :

kubectl exec -it <pod> -- sh

image

10) Go to the path /usr/share/nginx/html and do ls -lrt :

cd /usr/share/nginx/html

image

In the above screenshot, there is no files under /usr/share/nginx/html path

11) Now let's create a file under /usr/share/nginx/html path :

echo "Hello from nginx pod" > nginx-pod.txt

image

12) Now exit from the pod and ssh into the minikube host :

exit
minikube ssh

image

13) Now go the path which you mentioned in PersistentVolume.yaml. In our case it is /home/ubuntu/data and check if the file is present or not :

cd /home/ubuntu/data
ls -lrt

image

14) Now let's create one more file under /home/ubuntu/data inside minikube host :

echo "Hello from minikube host pod" > minikube-host-pod.txt

image

15) At last, go to nginx pod and check if minikube-host-pod.txt file is present or not :

kubectl exec -it <pod> -- sh
cd /usr/share/nginx/html
ls -lrt

image

Congratulations, you have done it Happy Learning :)