kubernetes-sigs / kind

Kubernetes IN Docker - local clusters for testing Kubernetes
https://kind.sigs.k8s.io/
Apache License 2.0
13.02k stars 1.51k forks source link

PV-PVC binding remains stuck #3615

Closed thlr1 closed 1 month ago

thlr1 commented 1 month ago

Hi, I can't seem to figure out PV / PVC binding. I have been trying to use a local volume in a pod but I can't get the volumes to consistently bind together. Here is a basic setup to try:

# kind.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    image: kindest/node:v1.27.11@sha256:681253009e68069b8e01aad36a1e0fa8cf18bb0ab3e5c4069b2e65cafdd70843
    extraMounts:
      - hostPath: ./local-volume
        containerPath: /data
# pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: test-pv
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 5Gi
  hostPath:
    path: /data
    type: Directory
#pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
  namespace: default
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

The volume shows Available but the pvc won't ever bind.

I have tried:

Does this have something to do with the WaitForFirstConsumer mode of the rancher storage class? I have tried to throw in a pod to use the pvc but it has not worked:

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: test-pvc
  containers:
  - name: test
    image: alpine
    command: ["sleep", "3600"]
    volumeMounts:
      - mountPath: /var/lib/data
        name: data

What am I missing?

Thank you very much for your help

thlr1 commented 1 month ago

After days of experimenting and looking around, I get it the moment I actually ask for help ^^'.

As I found out in the source code, storage classes must match for a PV-PVC bind to happen.

When no storageClassName is specified for the PVC, it will use the default storage class (called standard) but it turns out that this doesn't happen with PVs meaning they are left with no storage classes, hence the mismatch. I manually added the standard storageClass to the PV and the volumes bounded beautifully.

Also, I confirm that the pod is important because of the WaitForFirstConsumer binding mode.