Kubernetes Persistent Volumes Tutorial

Kubernetes Persistent Volumes Tutorial

Kubernetes Persistent Volume:

Kubernetes Persistent Volumes is nothing but a storage in the kubernetes cluster. We can use this storage for pods, So the pods data will be persisted in this Persistent Volumes even if the pods dies.

Create A Kubernetes Persistent Volume

Using kubernetes Persistent Volume manifest file we can create the Persistent Volume.

cat mypv.yml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mypv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/devops/data"
  • kind: PersistentVolume → We have defined the kind as PersistentVolume which tells kubernetes that the yaml file being used is to create the Persistent Volume.
  • name: mypv→ Name of PersistentVolume that we are creating..
  • storage: 5Gi → This tells that we are trying to create 5Gi space of Persistent Volume.
  • ReadWriteOnce → the volume can be mounted as a read/write volume only by a single node.
  • path: “/devops/data” → This tells that we are trying to create volume under this path.

lets create kubernetes Persistent Volume using kubectl apply command.

master $ kubectl apply -f mypv.yml

persistentvolume/mypv created
master $ kubectl get pv

NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mypv   10Gi       RWO            Retain           Available

Kubernetes Persistent Volume Claim

In order for pods to start using these persistent volumes, they need to be claimed (via a persistent volume claim). To make PVs available to pods in the Kubernetes cluster, we should explicitly claim them using a PersistentVolumeClaim  (PVC). With a persistent volume claim we are telling to the cluster that we need some amount of storage in the available persistent volumes.

Now that our persistent volume is in available state, we can claim it by creating persistent volume claim policy.

list the available persistent volumes.

master $ kubectl get pv

NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
mypv   10Gi       RWO            Retain           Available

Here you can see the status is available, once any persistent volume claim is attached to this pv status will be changed to ‘bound’.

Create A Kubernetes Persistent Volume claim

using yml manifest file we can create A kubernetes persistent volume claim.

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Here we mentioned storage of 1Gi.  that means this claim will use 1gb of space from available persistent volumes. And here we are having total 5gb of persistent volume.

lets create persistent volume claim

master $ kubectl apply -f mypvc.yml

persistentvolumeclaim/mypvc created

list the kubernetes persistent volume claims

master $ kubectl get pvc

NAME    STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mypvc   Bound    mypv     10Gi       RWO                           7s

verify is pvc is attached to pv or not by verifying the pv status

master $ kubectl get pv

NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM           STORAGECLASS   REASON   AGE
mypv   10Gi       RWO            Retain           Bound    default/mypvc                           88s

Here you can see pv status is changed from available to bound.

Using Kubernetes Persistent Volume Claims in pods

to use kubernetes volume claim in the pod, it involves 3 steps

  • create PersistentVolume
  • Create PersistentVolumeClaim
  • create a Pod that refers to our PersistentVolumeClaim

already we have completed first two steps. So let’s create some pods using deployment.

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mydep
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: Always
        volumeMounts:
        - name: myvolume
          mountPath: /var/log/nginx/
        ports:
        - containerPort: 80
      volumes:
      - name: myvolume
        persistentVolumeClaim:
          claimName: mypvc

Here in the volumes section we are giving the claim name as ‘mypvc’ which is created in the above steps. in this way we can add claim in any manifest file. Using volume mounts section we are mounting /var/log/nginx directory into pvc. Here we mounted /var/log/nginx on pvc called ‘mypvc’. So whatever the data that generated in /var/log/nginx that will be available in the node persistent volume, i,e /devops/data

 

master $ kubectl apply -f mydep.yml

deployment.extensions/mydep created

list the pods using kubectl get command.

master $ kubectl get pods

NAME                  READY   STATUS    RESTARTS   AGE
mydep-9445cc4-tlk4v   1/1     Running   0          53s

Enter into Pod

kubectl exec -it mydep-9445cc4-tlk4v bash

after logging into container install curl command to verify nginx is up or not

apt-get update
apt-get install curl -y

Verify Nginx

root@mydep-9445cc4-tlk4v:/# curl localhost
 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
......
.....

when you access nginx it will generate some access logs access.log file. this file is located in /var/log/nginx/. But already we mounted this directory in persistent volumes. lets verify this access logs in the node.

Verify the nginx logs in persistent volume

we created persistent volume in /devops/data directory in the node. So lets cross verify the /var/log/nginx files of container, in /devops/data directory in the node.

node01 $ ls /devops/data/
access.log  error.log
 
node01 $ cat /devops/data/access.log
 
127.0.0.1 - - [25/Sep/2019:06:38:24 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.0" "-"
127.0.0.1 - - [25/Sep/2019:06:38:31 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.0" "-"

Here you can see we got access.log and error.log files in /devops/data directory. So we have successfully created persistent volume, persistent volume claim and used the this claim in the pod.

Leave a Reply

Your email address will not be published. Required fields are marked *