Saturday, 18 October 2025

Deploying a Web App with Persistent Storage in Kubernetes: PV, PVC, Pod & NodePort Explained

Our course you can check :-   Udemy course  


Ques:- 

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-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 .

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

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).

Create a node port type service named web-xfusion using node port 30008 to expose the web server running within the pod.


Ans:-

Here's a complete Kubernetes YAML template that fulfills all the requirements you've listed:


Note:- We have used Init container for:-

This container will automatically create an index.html file inside the mounted volume before the Nginx container starts.
 

  • The initContainer runs first and writes an HTML file to the volume.
  • The Nginx container then serves this file from /usr/share/nginx/html.

  • ---

    #PersistentVolume (pv-xfusion)

    apiVersion: v1

    kind: PersistentVolume

    metadata:

      name: pv-xfusion

    spec:

      storageClassName: manual

      capacity:

        storage: 3Gi

      accessModes:

        - ReadWriteOnce

      hostPath:

        path: /mnt/security


    ---

    #PersistentVolumeClaim (pvc-xfusion)

    apiVersion: v1

    kind: PersistentVolumeClaim

    metadata:

      name: pvc-xfusion

    spec:

      storageClassName: manual

      accessModes:

        - ReadWriteOnce

      resources:

        requests:

          storage: 3Gi


    ---

    #Pod (pod-xfusion) with volume mounted at web server root and Init container 

    apiVersion: v1

    kind: Pod

    metadata:

      name: pod-xfusion

      labels:

        app: pod-xfusion

    spec:

      initContainers:

        - name: init-xfusion

          image: busybox

          command: ['sh', '-c', 'echo "<h1>Hello from Raj Gupta!</h1>" > /mnt/security/index.html']

          volumeMounts:

            - mountPath: /mnt/security

              name: xfusion-storage

      containers:

        - name: container-xfusion

          image: nginx:latest

          volumeMounts:

            - mountPath: /usr/share/nginx/html

              name: xfusion-storage

      volumes:

        - name: xfusion-storage

          persistentVolumeClaim:

            claimName: pvc-xfusion


    ---

    #NodePort Service (web-xfusion) on port 30008

    apiVersion: v1

    kind: Service

    metadata:

      name: web-xfusion

    spec:

      type: NodePort

      selector:

        # Ensure this matches the pod's labels if you add any

        # For now, we assume no labels, so selector will match pod name

        # Alternatively, you can add a label to the pod and match it here

        app: pod-xfusion

      ports:

        - protocol: TCP

          port: 80

          targetPort: 80

          nodePort: 30008


    Conclusion:-

    Learn how to deploy a web application on a Kubernetes cluster using PersistentVolumes and PersistentVolumeClaims to store application data. This hands-on tutorial walks you through:

    • Creating a PersistentVolume (pv-xfusion) using hostPath
    • Defining a PersistentVolumeClaim (pvc-xfusion) with storage class manual
    • Deploying an Nginx pod (pod-xfusion) with volume mounted at the web server root
    • Automating content creation using an initContainer
    • Exposing the pod using a NodePort service (web-xfusion) on port 30008

    By the end of this video, you'll understand how to manage persistent storage and expose services in Kubernetes effectively.

    No comments:

    Post a Comment