local-path-provisioner
local-path-provisioner copied to clipboard
How to perminantly bind a pv to a pvc after creation?
When I am in AWS and using the aws ebs csi driver I can do a 3 step deploy to get my pv's really to be pv's such that when a deployment is removed (or redeployed/updated) that I bind the same pv to the pvc.
- Create a pvc, pod and deploy into k8s
- Wait for pv to be created from csi driver
- Create a new pv in my kustomize referencing the volume that has been created
- Modify pvc to reference volumeName
- Re-deploy everything.
Two questions, is there an easier way, and if not, what goes in the csi section (and any other pertinent sections) below to get me the same behavior?
# define the data pv (having spun one up before and hard-coding it in here)
apiVersion: v1
kind: PersistentVolume
metadata:
name: registry-data-vol
spec:
storageClassName: warm
capacity:
storage: 50Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
csi:
driver: ebs.csi.aws.com
fsType: ext4
volumeHandle: vol-xxxx
This is how I did it:
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: rancher.io/local-path
name: pv-retain-vault
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 10Gi
claimRef:
apiVersion: v1
kind: PersistentVolumeClaim
name: pv-retain-vault
namespace: vault
hostPath:
path: /var/lib/rancher/k3s/storage/pv-retain-vault
type: DirectoryOrCreate
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- capital
storageClassName: local-path
volumeMode: Filesystem
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-retain-vault
namespace: vault
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: "" # Empty string must be explicitly set otherwise default StorageClass will be set
volumeName: pv-retain-vault
The key points are from this link:
- ensure there is a
claimRefin your PV to pre-bind it to your PVC. - use the
volumeNameand a blankstorageClassNamein the PVC to ensure it uses the volume as is.
Let me know if that helps.