100-Days-Of-DevOps-Challenge-KodeKloud

Persistent Volumes in Kubernetes

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:

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

Steps

  1. Create a persistent volume k3s-pv.yaml with this content:

     apiVersion: v1
     kind: PersistentVolume
     metadata:
     name: pv-xfusion
     spec:
     capacity:
         storage: 3Gi
     accessModes:
         - ReadWriteOnce
     storageClassName: manual
     hostPath:
         path: /mnt/security
    

    Create the volume:

     kubectl apply -f k3s-pv.yaml
    
  2. Create the persistent volume claim k3s-pvc.yaml with this following content:

     apiVersion: v1
     kind: PersistentVolumeClaim
     metadata:
     name: pvc-xfusion
     spec:
     accessModes:
         - ReadWriteOnce
     resources:
         requests:
         storage: 3Gi
     storageClassName: manual
    

    To create the pvc:

     kubectl k3s-pvc.yaml
    
  3. Create the pod k3s-pod.yaml:

     apiVersion: v1
     kind: Pod
     metadata:
     name: pod-xfusion
     labels:
         app: xfusion
     spec:
     containers:
         - name: container-xfusion
         image: httpd:latest
         volumeMounts:
             - name: xfusion-volume
             mountPath: /mnt/security
         resources:
             limits:
             memory: "128Mi"
             cpu: "500m"
             requests:
             memory: "64Mi"
             cpu: "250m"
     volumes:
         - name: xfusion-volume
         persistentVolumeClaim:
             claimName: pvc-xfusion
    

    run the pod

     kubectl apply -f k3s-pod.yaml
    
  4. And finally, create the service k3s-svc.yaml:

     apiVersion: v1
     kind: Service
     metadata:
     name: web-xfusion
     labels:
         app: xfusion
     spec:
     type: NodePort
     ports:
         - port: 80
         targetPort: 80
         nodePort: 30008
     selector:
         app: xfusion
    
     kubectl apply -f k3s-svc.yaml
    
  5. We can put all in one file k3s-pod.yaml

Good to Know?

Persistent Volumes (PV)

Persistent Volume Claims (PVC)

Volume Types

Best Practices