Persistent Volumes in Kubernetes

This is part four of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.

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:

  1. Create a PersistentVolume named as pv-xfusion. Configure the spec as storage class should be manual, set capacity to 3Gi, set access mode to ReadWriteOnce, volume type should be hostPath and set path to /mnt/security (this directory is already created, you might not be able to access it directly, so you need not to worry about it).
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-xfusion
spec:
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  hostPath:
    path: /mnt/security
  1. Create a PersistentVolumeClaim named as pvc-xfusion. Configure the spec as storage class should be manual, request 1Gi of the storage, set access mode to ReadWriteOnce.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-xfusion
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  resources:
    requests:
      storage: 1Gi
  1. Create a pod named as pod-xfusion, mount the persistent volume you created with claim name pvc-xfusion at document root of the web server, the container within the pod should be named as container-xfusion using image nginx with latest tag only (remember to mention the tag i.e nginx:latest).
apiVersion: v1
kind: Pod
metadata:
  name: pod-xfusion
  labels:
    app: pv-nginx
spec:
  volumes:
    - name: storage-xfusion
      persistentVolumeClaim: # Mount persistent volume
        claimName: pvc-xfusion # Claim storage
  containers:
    - name: container-xfusion
      image: nginx:latest
      ports:
        - containerPort: 80
      volumeMounts:
        - name: storage-xfusion
          mountPath:  /usr/share/nginx/html
  1. Create a node port type service named web-xfusion using node port 30008 to expose the web server running within the pod.
apiVersion: v1 
kind: Service 
metadata: 
  name: web-xfusion
spec: 
   type: NodePort 
   selector: 
     app: pv-nginx
   ports: 
     - port: 80 
       targetPort: 80 
       nodePort: 30008 # Expose the web server

Finally Cleanup

$ kubectl delete svc web-xfusion
service "web-xfusion" deleted
$ kubectl delete pod pod-xfusion
pod "pod-xfusion" deleted
$ kubectl delete pvc pvc-xfusion
persistentvolumeclaim "pvc-xfusion" deleted