Your team is deploying a stateful application (a PostgreSQL database) that requires its data to be preserved even if the pod is restarted or deleted. To achieve this, you need to provide persistent storage to the database pod.
Your task is to manually create a Persistent Volume (PV) that represents a piece of storage on a node, create a Persistent Volume Claim (PVC) to request that storage, and then mount the PVC into the database pod.
hostPath PV).SSH Access to Worker Nodes:
| Cluster Type | Access Method |
|---|---|
| minikube | minikube ssh |
| kind | docker exec -it <kind-container-name> bash(find container name with docker ps) |
| Cloud Providers | Use provider-specific SSH methods (e.g., gcloud compute ssh for GKE nodes) |
| Bare Metal/VMs | Standard SSH with cluster setup credentials |
Note: hostPath volumes are NOT recommended for production use due to security and portability concerns. This exercise uses them for educational purposes only.
Create a Directory on the Node:
/mnt/data to serve as the storage for the PV.Create a Persistent Volume (PV):
my-pv with a capacity of 1Gi.ReadWriteOnce.hostPath volume type, pointing to the /mnt/data directory on the node.storageClassName to manual.Create a Persistent Volume Claim (PVC):
my-pvc that requests 500Mi of storage.ReadWriteOnce.storageClassName as manual to bind to the PV you created.Create a Pod that Uses the PVC:
db-pod using the postgres:16-alpine image.my-pvc PVC into the container at the path /var/lib/postgresql/data./mnt/data directory exists on the worker node.my-pv PV is created and has a status of Available.my-pvc PVC is created and has a status of Bound to my-pv.db-pod is running and has the PVC mounted correctly./var/lib/postgresql/data inside the pod persists in the /mnt/data directory on the node.For Creating the Directory on the Node:
For Securing PostgreSQL Password:
valueFrom.secretKeyRef.For Creating the PV and PVC:
kubectl apply -f.storageClassName matches in both the PV and PVC to ensure they bind.For Creating the Pod:
volumes section to define a volume that uses the PVC.volumeMounts section in the container definition to mount the volume.PGDATA environment variable to avoid PostgreSQL initialization conflicts.For Verification:
kubectl get pv and kubectl get pvc to check the status of the PV and PVC.kubectl describe pod db-pod to verify that the volume is mounted correctly.kubectl exec to create a file in the mounted directory inside the pod, and then access the node to verify that the file exists.Persistent Volumes and Claims are the foundation of running stateful applications in Kubernetes. While hostPath is useful for development and testing on a single node, in a production environment, you would typically use a more robust storage solution like a cloud provider's block storage (e.g., AWS EBS, GCP Persistent Disk) or a network file system (NFS). The PV/PVC abstraction allows you to decouple the application's storage needs from the underlying storage infrastructure, making your applications more portable and easier to manage. For a CKA, mastering PVs and PVCs is essential for managing stateful workloads.
Security Note: This exercise demonstrates the proper use of Kubernetes Secrets for sensitive data like passwords. Never store credentials in plain text within YAML manifests in real-world scenarios.