Persistent Volumes in Kubernetes

Task :

The Nautilus DevOps team is working on a Kubernetes template to deploy a web application on the cluster. There are some requirements to create/use persistent volumes to store the application code, and the template needs to be designed accordingly. Please find more details below:

We already created a directory /mnt/finance and a file index.html under the same on node01 (you need not to access node01), that location should be mounted within the container to web server’s document root (keep in mind doc root can be different for Apache and Nginx web servers).

Create a PersistentVolume named as pv-devops. Configure the spec as storage class should be manual, set capacity to 9Gi, set access mode to ReadWriteOnce , volume type should be hostPath and set path to /mnt/finance.

Create a PersistentVolumeClaim named as pvc-devops. Configure the spec as storage class should be manual, set request storage to 2Gi, set access mode to ReadWriteOnce.

Create a pod named as pod-devops, set volume as storage-devops, and persistent volume claim to be named as pvc-devops. Container name should be container-devops, use image nginx with latest tag only and remember to mention tag i.e nginx:latest , container port should be default port 80, mount the volume to mount path to default doc root of web server and should be named storage-devops.

You can check your static website, exec into the pod and use curl command, i.e curl http://localhost.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

Solution :

thor@jump_host /$ cd /home/thor/
thor@jump_host ~$ vi persistentvolumes.yml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-devops
spec:
  capacity:
    storage: 9Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  hostPath:
    path: /mnt/finance
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-devops
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-devops
spec:
  volumes:
    - name: storage-devops
      persistentVolumeClaim:
        claimName: pvc-devops
  containers:
    - name: container-devops
      image: nginx:latest
      ports:
        - containerPort: 80
      volumeMounts:
        - name: storage-devops
          mountPath: /usr/share/nginx/html

thor@jump_host ~$ kubectl apply -f persistentvolumes.yml 
persistentvolume/pv-devops created
persistentvolumeclaim/pvc-devops created
pod/pod-devops created

thor@jump_host ~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
pod-devops 1/1 Running 0 12s

thor@jump_host ~$ kubectl get persistentvolume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv-devops 9Gi RWO Retain Bound default/pvc-devops manual 26s

thor@jump_host ~$ kubectl get persistentvolumeclaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-devops Bound pv-devops 9Gi RWO manual 40s

thor@jump_host ~$ kubectl exec --stdin --tty pod-devops -- /bin/bash
root@pod-devops:/# curl http://localhost
Welcome to xFusionCorp Industries
root@pod-devops:/# exit
exit

thor@jump_host ~$

Leave a Reply

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