Kubernetes Deployment populates wrong Persistent Volume Kubernetes Deployment populates wrong Persistent Volume docker docker

Kubernetes Deployment populates wrong Persistent Volume


It's expected behavior in Kubernetes. PVC can bind to any available PV, given that storage class is matched, access mode is matched, and storage size is sufficient. Names are not used to match PVC and PV.

A possible solution for your scenario is to use label selector on PVC to filter qualified PV.

First, add a label to PV (in this case: app=mysql)

kind: PersistentVolumeapiVersion: v1metadata:  name: mysql-volume  labels:    app: mysql

Then, add a label selector in PVC to filter PV.

kind: PersistentVolumeClaimapiVersion: v1metadata:  namespace: my-namespace  name: mysql-volume-claimspec:  storageClassName: manual  accessModes:    - ReadWriteOnce  resources:    requests:      storage: 2Gi  selector:     matchLabels:       app: mysql