Q-12 — Recreate MariaDB Deployment Using Existing PV (CKA-2025)

First, understand the question

The question says:

  • The MariaDB Deployment was deleted.
  • But the PersistentVolume (PV) STILL exists.
  • That PV already has your database data.
  • You must:

  • Create a PVC that binds to that PV.

  • Edit the provided file ~/mariadb-deploy.yaml to attach that PVC.
  • Apply it.
  • Ensure database comes up and becomes stable.

Trap #1 — You MUST reuse the existing PV

They wrote:

“A PersistentVolume already exists and is retained for reuse. Only one PV exists.”

Meaning:

  • You cannot create a new PV
  • You must bind to the existing one
  • PVC must match:

  • storageClassName (if the PV uses one OR is empty)

  • accessModes
  • capacity (PVC ≤ PV)

Trap #2 — PVC MUST bind successfully

So you must check:

kubectl get pv

Example PV might look like:

capacity:
  storage: 250Mi
accessModes:
- ReadWriteOnce
storageClassName: manual

Your PVC must match this exactly.


Trap #3 — Deployment must point to the PVC, NOT a hostPath

Some candidates mistakenly patch:

hostPath:

But here the database will NOT recover data.

You MUST use the PVC.


Step-by-step solution (What YOU will do in exam)


STEP 1 — Check the PV

kubectl get pv
kubectl describe pv <pv-name>

Note the values:

  • storage capacity
  • accessModes
  • storageClassName

STEP 2 — Create PVC named mariadb in namespace mariadb

Create the file:

kubectl create ns mariadb  # only if NS does not exist
kubectl create -n mariadb -f - <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb
  namespace: mariadb
spec:
  volumeName: <>            # put the pv name here, mandatory
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 250Mi
  storageClassName: manual    # IF PV has this
EOF

💡 If PV has empty storageClassName, you must remove this line.


STEP 3 — Edit the Deployment (~/mariadb-deploy.yaml)

Open the file:

vi ~/mariadb-deploy.yaml

Add under spec.template.spec.containers.volumeMounts:

volumeMounts:
- name: mariadb-storage
  mountPath: /var/lib/mysql

Add under spec.template.spec.volumes:

volumes:
- name: mariadb-storage
  persistentVolumeClaim:
    claimName: mariadb

STEP 4 — Apply the deployment

kubectl apply -f ~/mariadb-deploy.yaml

STEP 5 — Verify

kubectl get pods -n mariadb
kubectl describe pod -n mariadb <pod-name>
kubectl get pvc -n mariadb
kubectl get pv

PVC status must be:

Bound

Pod must be:

Running
Ready

COMPLETE FINAL YAML (What your Deployment must look like)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mariadb
  namespace: mariadb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
      - name: mariadb
        image: mariadb:10.6
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: mypass
        volumeMounts:
        - name: mariadb-storage
          mountPath: /var/lib/mysql

      volumes:
      - name: mariadb-storage
        persistentVolumeClaim:
          claimName: mariadb

⭐ EXAM NOTES (Add to your notebook)

✔ If PV exists → NEVER create a new PV

✔ PVC must match PV’s:

  • StorageClass
  • AccessModes
  • Capacity

✔ If you mismatch → PVC stays Pending → Pod fails → You lose marks

✔ Deployment must mount the PVC to the correct MariaDB data path:

/var/lib/mysql

✔ After applying → ALWAYS check:

get pv
get pvc
get pods