A hostPath:
volume embedded directly in a Pod spec isn't a PersistentVolume. PersistentVolumes and PersistentVolumeClaims are different API objects, which showed up later in Kubernetes's timeline. You could have a Pod volumes:
to reference a persistentVolumeClaim:
.
In most cases you shouldn't use hostPath:
at all. It's an "escape hatch" that accesses content directly on the Node's filesystem, whichever Node that happens to be. If your Pod gets deleted and recreated on a different Node, you'll get that same directory path, but the content won't follow it and you'll start from a new empty directory. (Almost the only real use case is something like a log manager running as a DaemonSet, which runs on every Node.)
You also shouldn't usually directly create Pods, or PersistentVolumes, or PersistentVolumeClaims. For a stateful application like a database, use a StatefulSet to describe the application. Kubernetes will create both a Pod and a PersistentVolumeClaim for each replica, and then a storage provisioner within the cluster will create an underlying PersistentVolume. For the fragment you show in the question, the setup would look like:
apiVersion: apps/v1
kind: StatefulSet
spec:
template:
spec:
containers:
- name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumeClaimTemplates:
- metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi