DEV Community

Cover image for 4.Persistent Volumes in Kubernetes
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

4.Persistent Volumes in Kubernetes

Lab Information

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:

Create a PersistentVolume named as pv-devops. Configure the spec as storage class should be manual, set capacity to 5Gi, set access mode to ReadWriteOnce, volume type should be hostPath and set path to /mnt/sysops (this directory is already created, you might not be able to access it directly, so you need not to worry about it).

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

Create a pod named as pod-devops, mount the persistent volume you created with claim name pvc-devops at document root of the web server, the container within the pod should be named as container-devops using image httpd with latest tag only (remember to mention the tag i.e httpd:latest).

Create a node port type service named web-devops using node port 30008 to expose the web server running within the pod.
Enter fullscreen mode Exit fullscreen mode

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


Lab Solutions

Step 2: Create Configuration File
Create a file named devops-deployment.yaml:

# Define PersistentVolume for storage
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-devops
spec:
  storageClassName: "manual"
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/sysops
---
# Define PersistentVolumeClaim to request storage
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-devops
spec:
  storageClassName: "manual"
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
# Define Pod with Apache web server
apiVersion: v1
kind: Pod
metadata:
  name: pod-devops
  labels:
    app: web-devops
spec:
  containers:
    - name: container-devops
      image: httpd:latest
      volumeMounts:
        - mountPath: /usr/local/apache2/htdocs
          name: web-storage
  volumes:
    - name: web-storage
      persistentVolumeClaim:
        claimName: pvc-devops
---
# Define NodePort service to expose the web server
apiVersion: v1
kind: Service
metadata:
  name: web-devops
spec:
  type: NodePort
  selector:
    app: web-devops
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30008
Enter fullscreen mode Exit fullscreen mode

Step 2: Apply Configuration

kubectl apply -f devops-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify PersistentVolume and Claim

kubectl get pv pv-devops 
Enter fullscreen mode Exit fullscreen mode

Verification: Confirm the PersistentVolume is bound to pvc-datacenter.

kubectl get pvc pvc-devops 
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Pod

kubectl get pod pod-devops
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify Service

kubectl get svc web-devops
Enter fullscreen mode Exit fullscreen mode

Verification: Confirm the service uses NodePort 30008.

Step 6: Verify Application Accessibility

Access the Application:

Click on the App or Website button in the lab interface.


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)