9.1.2 Persistent volumes and claims

View in the book. Buy the book.
9.2 A Pod that references a PersistentVolumeClaim that gets bound to a PersistentVolume, which references a disk
Figure 9.2 A Pod that references a PersistentVolumeClaim that gets bound to a PersistentVolume, which references a disk
9.3  The lifecycle of a PersistentVolumeClaim and PersistentVolume in a dynamically provisioned system
Figure 9.3 The lifecycle of a PersistentVolumeClaim and PersistentVolume in a dynamically provisioned system
$ kubectl create -f Chapter09/9.1.2_PersistentVolume/pvc-mariadb.yaml
pod/mariadb-demo created
persistentvolumeclaim/mariadb-pv-claim created

Wait for the Pod, and the PVC to be created

$ kubectl get pvc -w
NAME               STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mariadb-pv-claim   Pending                                      standard-rwo   36s
mariadb-pv-claim   Pending                                      standard-rwo   68s
mariadb-pv-claim   Pending                                      standard-rwo   68s
mariadb-pv-claim   Pending   pvc-45271397-f998-4ba4-9657-8df3fa0b1427   0                         standard-rwo   87s
mariadb-pv-claim   Bound     pvc-45271397-f998-4ba4-9657-8df3fa0b1427   2Gi        RWO            standard-rwo   87s

View the associate PV

$ kubectl get -o yaml pv pvc-45271397-f998-4ba4-9657-8df3fa0b1427
apiVersion: v1
kind: PersistentVolume
metadata:
  annotations:
    pv.kubernetes.io/provisioned-by: pd.csi.storage.gke.io
    volume.kubernetes.io/provisioner-deletion-secret-name: ""
    volume.kubernetes.io/provisioner-deletion-secret-namespace: ""
  creationTimestamp: "2023-11-12T21:12:46Z"
  finalizers:
  - kubernetes.io/pv-protection
  - external-attacher/pd-csi-storage-gke-io
  name: pvc-45271397-f998-4ba4-9657-8df3fa0b1427
  resourceVersion: "583596"
  uid: 9afc9344-16d1-4a25-8edb-bcc4a0df959f
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 2Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: mariadb-pv-claim
    namespace: default
    resourceVersion: "583340"
    uid: 45271397-f998-4ba4-9657-8df3fa0b1427
  csi:
    driver: pd.csi.storage.gke.io
    fsType: ext4
    volumeAttributes:
      storage.kubernetes.io/csiProvisionerIdentity: 1699782108094-9721-pd.csi.storage.gke.io
    volumeHandle: projects/gke-autopilot-test/zones/us-west1-b/disks/pvc-45271397-f998-4ba4-9657-8df3fa0b1427
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: topology.gke.io/zone
          operator: In
          values:
          - us-west1-b
  persistentVolumeReclaimPolicy: Delete
  storageClassName: standard-rwo
  volumeMode: Filesystem
status:
  phase: Bound
9.4 The PersistentVolumeClaim and PersistentVolume after the latter was provisioned, and they were bound together
Figure 9.4 The PersistentVolumeClaim and PersistentVolume after the latter was provisioned, and they were bound together

Now the fun part, try it out:

kubectl port-forward pod/mariadb-demo 3306:3306
docker run --net=host -it --rm mariadb mariadb -h localhost -P 3306 \
-u root -p

The password, as shown in the deployment is your database password.

Run an example query:

MariaDB [(none)]> SELECT user, host FROM mysql.user;
+-------------+-----------+
| User        | Host      |
+-------------+-----------+
| root        | %         |
| healthcheck | 127.0.0.1 |
| healthcheck | ::1       |
| healthcheck | localhost |
| mariadb.sys | localhost |
| root        | localhost |
+-------------+-----------+
6 rows in set (0.007 sec)

MariaDB [(none)]> CREATE DATABASE foo;
Query OK, 1 row affected (0.004 sec)

MariaDB [(none)]> exit
Bye

Cleanup

kubectl delete pod/mariadb-demo
kubectl delete pvc/mariadb-pv-claim