Tuesday, 4 August 2020

Kubernetes: Configure PV Storage

Persistent Volume (PV) object is used to connect to external storage.

 

Step 1: Create a Persistent Volume.

 

persistedVolume.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/pv1-data"

Create a Persisted volume using the above definition file.

$kubectl create -f persistedVolume.yml 
persistentvolume/my-pv created

Query all the persisted volumes.

$kubectl get pv
NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
my-pv   5Gi        RWO            Retain           Available                                   9s

From the above output, it is confirmed that the Persisted Volume ‘my-pv’ is created.

 

Step 2: Configuring PVC (Persistent Volume Claim).

 

Using Persistent Volume Claim, we can request access to Persistent Volume.

 

persistentVolumeClaim.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

Let’s create a Persistent Volume Claim using the above definition file by executing the below command.

 

kubectl create -f persistentVolumeClaim.yml

$kubectl create -f persistentVolumeClaim.yml 
persistentvolumeclaim/my-pvc created

Query Persistent Volumes.

$kubectl get pvc
NAME     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
my-pvc   Bound    pvc-0f55016a-c661-40b4-88f7-1ceade1bdb97   2Gi        RWO            standard       59s

Step 3: Let’s create a Pod with two containers that use this PV.

 

podPvDemo.yml

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: hello-world
    author: krishna
    serviceType: user-service
spec:
  volumes:
    - name: pv-storage
      persistentVolumeClaim:
        claimName: my-pvc
  containers:
    - name: container1
      image: busybox
      command:
        - sleep 
        - "300"
      volumeMounts:
        - mountPath: /container1-mount
          name: pv-storage

‘spec.containers.volumeMounts.name’ refers to the ‘claimName’ of matched ‘spec.volumes.name’.

 

Let’s create a pod using the above definition file.

$kubectl create -f podPvDemo.yml 
pod/my-pod created

Let’s create a file in the mounted path.

kubectl exec -it my-pod -c container1 -- touch /container1-mount/hello.txt

 

Let’s list the files in the mounted path.

$kubectl exec -it my-pod -c container1 -- ls -l /container1-mount
total 0
-rw-r--r--    1 root     root             0 Jun  8 10:19 hello.txt

Let’s delete the pod.

$kubectl delete pod my-pod
pod "my-pod" deleted

Query the pods to confirm the Pod is deleted.

$kubectl get pods
No resources found in default namespace.

Let’s recreate the pod again.

$kubectl create -f podPvDemo.yml 
pod/my-pod created

Let’s list the files at path ‘/container1-mount’ to make sure that the file ‘hello.txt’ is persisted.

$kubectl exec -it my-pod -c container1 -- ls -l /container1-mount
total 0
-rw-r--r--    1 root     root             0 Jun  8 10:19 hello.txt

Previous                                                    Next                                                    Home

No comments:

Post a Comment