local-path-provisioner icon indicating copy to clipboard operation
local-path-provisioner copied to clipboard

How to perminantly bind a pv to a pvc after creation?

Open gbarton opened this issue 4 years ago • 1 comments

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.

  1. Create a pvc, pod and deploy into k8s
  2. Wait for pv to be created from csi driver
  3. Create a new pv in my kustomize referencing the volume that has been created
  4. Modify pvc to reference volumeName
  5. 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

gbarton avatar Jun 23 '21 22:06 gbarton

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 claimRef in your PV to pre-bind it to your PVC.
  • use the volumeName and a blank storageClassName in the PVC to ensure it uses the volume as is.

Let me know if that helps.

gunzy83 avatar Jul 06 '22 02:07 gunzy83